2014-03-06 46 views
0

我正尝试使用基于邮编的标记来显示Google地图。我已将latlng坐标转换为邮政编码,但问题在于地图根本不显示。无法使用基于邮编的标记显示Google地图

以下是我在尝试这样做:

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')的出现。

我在做什么错?

感谢

+1

你检查JavaScript控制台,有没有错误?如果没有显示这些警报,我怀疑在到达这些线路之前有问题。 –

+0

控制台没有错误,没有警报。我猜这些标记有问题,一张简单的地图确实很好,但是找不到该错误。 – user984621

+0

如果你删除标记创建和console.log这些拉特,它的工作原理? –

回答

0

你的问题是在这里,我们在调用初始化函数初始化功能!

function initialize() { 
    ... 
    google.maps.event.addDomListener(window, 'load', initialize); 
} 

将最后一行移出该函数,你会看到一些我认为的结果。

function initialize() { 
    ... 
} 
google.maps.event.addDomListener(window, 'load', initialize); 
0

2个问题:

  1. 您的onload事件监听器是初始化函数里面,所以它永远不会调用初始化
  2. 当我解决,我得到一个JavaScript错误

    Uncaught InvalidValueError: in property address: not a string 
    
  3. then:

    Uncaught ReferenceError: myOptions is not defined 
    

所以:

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