[英]Google Maps - using geocoder to get long/lat with javascript
我試圖從谷歌地圖api獲取經度和緯度值與地址,但它似乎沒有返回任何值。 我的語法有問題嗎?
var point = GClientGeocoder.getLatLng('1600 Pennsylvania Avenue NW Washington, DC 20500');
alert(point);
您似乎正在使用谷歌地圖的v2; 如果您對使用v3感興趣(因為Google已正式棄用版本2),您可以執行以下操作:
var mygc = new google.maps.Geocoder();
mygc.geocode({'address' : '1600 Pennsylvania Avenue NW Washington, DC 20500'}, function(results, status){
console.log( "latitude : " + results[0].geometry.location.lat() );
console.log( "longitude : " + results[0].geometry.location.lng() );
});
它與其他例子類似,除了:
Google API文檔提供了有關v3的更多詳細信息。 干杯!
這有效(前提是您擁有正確的API密鑰,受限於2500個請求/天的速率限制):
address_string = '1600 Pennsylvania Avenue NW Washington, DC 20500';
geocoder = new GClientGeocoder();
geocoder.getLatLng(
address_string,
function(point) {
if (point !== null) {
alert(point); // or do whatever else with it
} else {
// error - not found, over limit, etc.
}
}
);
您似乎在調用GClientGeocoder
的函數,您需要在其中創建new GClientGeocoder()
並調用其函數。
試試吧:
geocoder = new GClientGeocoder();
point = geocoder.getLatLng(address)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.