简体   繁体   English

Google Maps Geocoder未定义

[英]Google maps geocoder is undefined

Hi i am getting geocoder undefined error. 嗨,我收到地址解析器未定义的错误。 What i trying is to show google map on page load. 我想要的是在页面加载时显示Google地图。 Below is my code 下面是我的代码

    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); 

Any help will be appreciated!!! 任何帮助将不胜感激!!!

document.ready (where you try to access geocoder ) will fire before window.onload (where you create the Geocoder -instance). document.ready (尝试访问geocoder )将在window.onload (创建Geocoder -instance的位置)之前触发。

Append the code for document.ready to initialize 附加代码以进行document.ready initialize

Also make sure you have 同时确保你有

<script src="https://maps.google.com/maps?file=api&amp;v=3&amp;sensor=false"
 type="text/javascript"></script>

prior to running the javascript code. 在运行javascript代码之前。

That's what messed me up. 那就是让我困惑的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM