【问题标题】:Google maps geocoder is undefined谷歌地图地理编码器未定义
【发布时间】:2014-04-15 06:49:17
【问题描述】:

您好,我收到地理编码器未定义错误。 我试图在页面加载时显示谷歌地图。 下面是我的代码

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

任何帮助将不胜感激!!!

【问题讨论】:

    标签: javascript google-maps google-maps-api-3 map


    【解决方案1】:

    document.ready(您尝试访问geocoder)将在window.onload(您创建Geocoder-instance 的位置)之前触发。

    document.ready 的代码附加到initialize

    【讨论】:

    • 感谢 Molle,使用 $(document).ready(function() { initialize(); });代替 google.maps.event.addDomListener(window, 'load', initialize);
    【解决方案2】:

    还要确保你有

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

    在运行 javascript 代码之前。

    这就是让我搞砸的原因。

    【讨论】:

    • 赞成,尽管现在sensor 已被弃用,您必须提供 API 密钥
    猜你喜欢
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    • 2018-11-30
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多