[英]Google maps geocoder is undefined
嗨,我收到地址解析器未定義的錯誤。 我想要的是在頁面加載時顯示Google地圖。 下面是我的代碼
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
-instance的位置)之前觸發。
附加代碼以進行document.ready
initialize
同時確保你有
<script src="https://maps.google.com/maps?file=api&v=3&sensor=false"
type="text/javascript"></script>
在運行javascript代碼之前。
那就是讓我困惑的地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.