2016-12-15 27 views
-1

對於這個特殊的地方GMAP不歸經/緯度「南非聯合銀行中心, 利別克街,開普敦,南非」 我使用此代碼的特定地點GMAP不返回緯度經度

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

有人有建議嗎?

+0

你得到任何控制檯錯誤?並且您是否嘗試過其他任何位置 –

回答

1

「南非開普敦裏貝克街Absa中心」不是郵寄地址,它是一個「地點」。

使用Places API的找到它,使用它的地址(2利別克街,開普敦市中心,開普敦,8001,南非),或在地理編碼器使用其PlaceId(「ChIJx9Oa_2BnzB0RSoIzDuPzdrU」)。利用地理編碼與placeId

Places API

Address

代碼片段:

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);
html, 
 
body, 
 
#map_canvas { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script> 
 
<div id="map_canvas"></div>