簡體   English   中英

Google地圖 - 使用地理編碼器通過javascript獲取長/ lat

[英]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.maps.Geocoder()而不是GClientGeocoder()
  • 傳入一個帶有'address'屬性的對象,而不是字符串本身
  • 結果是一個JSON對象,這意味着你必須以不同的方式訪問lat / 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM