【发布时间】:2017-11-09 16:04:53
【问题描述】:
我有一个页面,我需要按地址加载地图以查看位于该点的属性。为此,我找到了Google Maps API,但我还不能这样做。我正在尝试一个我在文档中看到的示例,但它仍然无法正常工作并且不会引发任何异常。
Here 文档。
我该怎么做?
正在加载 Google Maps API
<script src="https://maps.google.com/maps/api/js?key=AIzaSyA8JZPv2N9bE0OQABj6hKO9QZb0kH32l"></script>
按地址加载地图的脚本
$(document).ready(function () {
initialize();
codeAddress();
});
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 8,
center: latlng
}
map = new google.maps.Map(document.getElementById('gmap'), mapOptions);
}
function codeAddress() {
var address = document.getElementById('myAddress').value;
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == 'OK') {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
HTML
<div id="gmap"></div>
<!--my address-->
@Html.HiddenFor(model => model.myAddress)
【问题讨论】:
-
您看到警报错误了吗?
-
您的代码看起来不错。现在发生了什么?你有任何脚本错误吗?
标签: asp.net-mvc google-maps-api-3