您好,我收到地理编码器未定义错误。 我试图在页面加载时显示谷歌地图。 下面是我的代码
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('map-canvas'), mapOptions);
}
$(document).ready(function(){
var address = document.getElementById('shipAddress').value;
//alert(address);
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.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);
}
});
});
google.maps.event.addDomListener(window, 'load', initialize);
任何帮助将不胜感激!!!
最佳答案
document.ready
(您尝试访问 geocoder
的位置)将在 window.onload
(您创建 Geocoder 的位置)之前触发
-实例)。
将document.ready
的代码附加到initialize
关于javascript - 谷歌地图地理编码器未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22329441/