javascript - 谷歌地图地理编码器未定义

标签 javascript google-maps google-maps-api-3 dictionary

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

    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/

相关文章:

javascript - 使用 Ajax 和 JQuery 从 PHP 文件获取 JSON 数据

javascript - 触发器仅在单击时起作用,但在没有它的情况下加载时不起作用

google-maps - 在 Google Maps v3 中设置折线的颜色?

javascript - 谷歌地图 v3 : Unable to get value of the property 'setMap'

javascript - 谷歌地图API : Why we need to set title for Marker object?

javascript - 如何在不影响父按钮功能的情况下,给子按钮添加 "delete"功能?

javascript - 指挥官.js : passing isDefault option for command with action()

android - 如何在谷歌地图 Android 上显示多个标记?

java - 使用我当前的位置将 Google 地点实现到 Google map 中

css - 巨大的谷歌地图控件(可能是错误?)