对于特定位置,gmap不会返回lat lon

时间:2016-12-15 13:52:12

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

对于这个特殊的地方,gmap不会返回lat / lon“Absa Center, Riebeek Street,南非开普敦“ 我正在使用此代码

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">

var geocoder = new google.maps.Geocoder();
var address = "Absa Centre, Riebeek Street, Cape Town, South Africa";

geocoder.geocode( { 'address': address}, function(results, status) {

if (status == google.maps.GeocoderStatus.OK) {
   var latitude = results[0].geometry.location.lat();
    var longitude = results[0].geometry.location.lng();
    alert(latitude+' '+longitude);
    } 
}); 
</script>

有人有建议吗?

1 个答案:

答案 0 :(得分:1)

&#34;南非开普敦Riebeek街Absa中心&#34;不是邮政地址,它是一个地方&#34;。

使用Places API查找,使用其地址(2 Riebeek St,开普敦市中心,开普敦,8001,南非),或在地理编码器中使用其PlaceId(&#34; ChIJx9Oa_2BnzB0RSoIzDuPzdrU&#34;)

Places API

Address

使用带有placeId的地理编码器的代码段:

&#13;
&#13;
function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var geocoder = new google.maps.Geocoder();
  var address = "Absa Centre, Riebeek Street, Cape Town, South Africa";
  var placeId = "ChIJx9Oa_2BnzB0RSoIzDuPzdrU";

  geocoder.geocode({
    // 'address': address
    placeId: placeId
  }, function(results, status) {

    if (status == google.maps.GeocoderStatus.OK) {
      var latitude = results[0].geometry.location.lat();
      var longitude = results[0].geometry.location.lng();
      map.setCenter(results[0].geometry.location);
      if (results[0].geometry.viewport) map.fitBounds(results[0].geometry.viewport);
      else if (results[0].geometry.bounds) map.fitBounds(results[0].geometry.bounds);
      var marker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location
      })
    } else alert("Not found, status=" + status);
  });
}
google.maps.event.addDomListener(window, "load", initialize);
&#13;
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<div id="map_canvas"></div>
&#13;
&#13;
&#13;