如果用戶輸入地址,我想轉換爲等效的LatLng。使用谷歌地圖API v3如何使用給定的地址獲得LatLng?
我讀過文檔,我想我可以使用Geocoder類來做到這一點,但無法弄清楚如何實現它。
感謝您的幫助!
如果用戶輸入地址,我想轉換爲等效的LatLng。使用谷歌地圖API v3如何使用給定的地址獲得LatLng?
我讀過文檔,我想我可以使用Geocoder類來做到這一點,但無法弄清楚如何實現它。
感謝您的幫助!
上有https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple
一個很好的例子要縮短了一點:
geocoder = new google.maps.Geocoder();
function codeAddress() {
//In this case it gets the address from an element on the page, but obviously you could just pass it to the method instead
var address = document.getElementById('address').value;
geocoder.geocode({ 'address' : address }, function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
//In this case it creates a marker, but you can get the lat and lng from the location.LatLng
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);
}
});
}
我不認爲location.LatLng
工作,但這個工程:
results[0].geometry.location.lat(), results[0].geometry.location.lng()
在探索Get Lat Lon源代碼時發現它。
謝謝! – cpcdev 2016-07-15 22:58:47
如果你需要做這在後端,您可以使用下列網址結構:使用curl
https://maps.googleapis.com/maps/api/geocode/json?address=STREET_ADDRESS
示例PHP代碼:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://maps.googleapis.com/maps/api/geocode/json?address=' . rawurlencode($address));
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($curl);
curl_close ($curl);
有關詳細信息,請參見附加documentation。
該文檔提供了示例輸出,並將幫助您獲得自己的API key,以便能夠向Google地圖地理編碼API發出請求。
你知道這是什麼限制嗎。我的意思是我可以在一個小時或一天內撥打多少電話? – 2015-08-24 08:17:32
免費的API限制是每24小時2500個請求,每秒5個請求...請參閱https://developers.google.com/maps/documentation/geocoding/intro#Limits – farinspace 2015-08-25 03:07:02
來到這裏尋找這個。 PHP用戶可能希望使用'$ arr = json_decode($ json,true);'來訪問數據。 – 2016-01-20 19:52:34
難道我們不希望所有的文檔都和jQuery一樣好。我對Google的一些文檔感到很困難。 – b729sefc 2014-05-25 15:32:17