Javascript 谷歌地图地理编码器未定义
嗨,我得到地理编码器未定义的错误。 我尝试的是在页面加载时显示谷歌地图。 下面是我的代码Javascript 谷歌地图地理编码器未定义,javascript,google-maps,google-maps-api-3,map,Javascript,Google Maps,Google Maps Api 3,Map,嗨,我得到地理编码器未定义的错误。 我尝试的是在页面加载时显示谷歌地图。 下面是我的代码 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 g
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
)将在窗口之前启动。onload
(您创建geocoder
-实例的地方)
将
文档的代码追加到初始化中。准备好了吗
<script src="https://maps.google.com/maps?file=api&v=3&sensor=false"
type="text/javascript"></script>
在运行javascript代码之前
这就是让我搞砸的原因。谢谢Molle,map在使用$(document).ready(function(){initialize();})之后工作了;代替google.maps.event.addDomListener(窗口“加载”,初始化);Upvote,尽管现在传感器已被弃用,您必须提供API密钥