簡體   English   中英

Geocoder Geocode-Google Maps JavaScript API

[英]Geocoder Geocode - Google Maps javascript API

我對以下功能有疑問:

function geo(address) {
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            return results[0].geometry.location;
        }
    });
}

它返回我“未定義”。 任何幫助將不勝感激。 非常感謝!

實際上,您的地理編碼代碼是正確的。 問題是“ geocoder.geocode”是異步的,並且geo函數在獲取地理編碼結果之前完成了執行。 作為概念證明,請嘗試以下操作:

function geo(address) {
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {

            alert(results[0].geometry.location); //should have valid info
            return results[0].geometry.location;                    
        }
    });

    alert("I bet you were not expecting this alert to go first");                
}

因此,您有兩個選擇。 您可以在geo函數中處理地理編碼位置,也可以提供一個回調函數來處理該位置。

我用一個例子更新了你的小提琴。 在這里檢查

好,謝謝。

我找到了其他解決方案-最好將results [0] .geometry.location中的值保存到cookie並在需要的地方使用。 不管怎么說,還是要謝謝你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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