Javascript 无法显示带有基于邮政编码的标记的Google地图

Javascript 无法显示带有基于邮政编码的标记的Google地图,javascript,jquery,google-maps,google-maps-api-3,Javascript,Jquery,Google Maps,Google Maps Api 3,我正试图用一个基于邮政编码的标记来显示谷歌地图。我已经转换了lat和lng坐标上的邮政编码,但问题是地图根本不显示 以下是我如何尝试做到这一点: function initialize() { var lat = ''; var lng = ''; var geocoder = new google.maps.Geocoder(); geocoder.geocode( { 'address': 94301}, function(results, status)

我正试图用一个基于邮政编码的标记来显示谷歌地图。我已经转换了latlng坐标上的邮政编码,但问题是地图根本不显示

以下是我如何尝试做到这一点:

  function initialize() {
    var lat = '';
    var lng = '';
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': 94301}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        lat = results[0].geometry.location.lat();
        lng = results[0].geometry.location.lng();
        var mapOptions = {
          zoom: 8,
          center: new google.maps.LatLng(lat, lng)
        };
        var map = new google.maps.Map(document.getElementById("gmap"), myOptions);
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
        });
        alert('111');
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
    google.maps.event.addDomListener(window, 'load', initialize);
 }
甚至没有出现警报('111')

我做错了什么


谢谢

您的问题就在这里,您正在初始化函数中调用初始化函数

function initialize() {
    ...
    google.maps.event.addDomListener(window, 'load', initialize);
}
用这个函数把最后一行移到外面,我想你会看到一些结果

function initialize() {
    ...
}
google.maps.event.addDomListener(window, 'load', initialize);
2个问题:

  • onload事件的侦听器位于initialize函数内部,因此它从不调用initialize
  • 当我修复这个问题时,我得到了一个javascript错误

     Uncaught InvalidValueError: in property address: not a string 
    
  • 然后:

     Uncaught ReferenceError: myOptions is not defined 
    
  • 因此:


    您是否检查了javascript控制台是否没有错误?如果没有显示这些警报,我怀疑在到达这些线路之前有问题。控制台中没有错误,没有警报。我猜标记出了问题,一个简单的映射确实工作得很好,但是找不到错误。如果删除标记创建和console.log这些lat,它会工作吗?如何加载映射API?嘿,如果删除map.setCenter(结果[0].geometry.location)会怎么样;线,它是无用的,因为您已经设置了地图的中心在mapOptions?
         function initialize() {
            var lat = '';
            var lng = '';
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode( { 'address': "94301"}, function(results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                lat = results[0].geometry.location.lat();
                lng = results[0].geometry.location.lng();
                var mapOptions = {
                  zoom: 8,
                  center: new google.maps.LatLng(lat, lng)
                };
                var map = new google.maps.Map(document.getElementById("gmap"), mapOptions);
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                  map: map,
                  position: results[0].geometry.location
                });
                alert('111');
              } else {
                alert("Geocode was not successful for the following reason: " + status);
              }
            });
    
         }
          google.maps.event.addDomListener(window, 'load', initialize);