2011-05-02 56 views
1

谷歌地理編碼迴應我使用谷歌的服務geocding這裏(http://code.google.com/apis/maps/documentation/javascript/services.html#Geocoding)記載輸出從谷歌地圖API

我想要得到的結果爲外部變量,但不斷收到「未定義」。

這裏是我的代碼:

localPoint = new google.maps.Geocoder(); 

output = localPoint.geocode({ 'address': "1009 south 10th Ave, Kelso WA 98626"}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     return results[0].geometry.location; 
    } else { 
     alert("Geocode was not successful for the following reason: " + status); 
    } 
}); 
alert(output) 

*(更新) - 在理想情況下,我將能夠把它包起來的功能,所以我可以返回的結果是這樣的:

localPoint = new google.maps.Geocoder(); 

function codeAddress(this_address) { 
    localPoint.geocode( 
     { 'address': this_address}, 
      function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       return results[0].geometry.location; 
      } else { 
       alert("Geocode was not successful for the following reason: " + status); 
      } 
     } 
    ); 
} 

回答

4

你是在回調中返回一個變量 - 但這個回報在哪裏呢? output是未定義的,因爲您在調用localPoint.geocode()之後會立即發出警報,而這可能不會完成。因此,您需要將您的警報(在一般情況下,依賴於結果的任何代碼)INSIDE回調:

localPoint = new google.maps.Geocoder(); 

localPoint.geocode({ 'address': "1009 south 10th Ave, Kelso WA 98626"}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     var output=results[0].geometry.location; 
     alert(output); 
    } else { 
     alert("Geocode was not successful for the following reason: " + status); 
    } 
}); 

現在,如果你有需要使用地理編碼結果的其它代碼,請務必使函數調用內部的回調:

function processResults(location){ 

    //do stuff with a successful geocode here 

} 

localPoint = new google.maps.Geocoder(); 

localPoint.geocode({ 'address': "1009 south 10th Ave, Kelso WA 98626"}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     processResults(results[0].geometry.location); 
    } else { 
     alert("Geocode was not successful for the following reason: " + status); 
    } 
}); 

processResults(results)如果地址解析成功的纔會被調用。

更新:好的,我看了一下你鏈接的代碼。您正嘗試在循環中進行地理編碼。可以通過對代碼進行一些修改來完成。具體來說,你需要做以下修改:

function codeAddress(this_address,index,callback) { 
    geocoder.geocode({ 'address': this_address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      callback.call(window,index,results[0].geometry.location) 
     } else { 
      alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 
} 

循環將是這樣的:

for (var i = 0; i < businesses.length; i++) { 
     //var point = new google.maps.LatLng(businesses[i].lat,businesses[i].lng); 
     codeAddress(businesses[i].address,i,function(i,point){ 
      var description = businesses[i].description; 

      if(businesses[i].business_type == "Wine"){ 
       //http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=A|00CC99|000000 
       var icon = 'http://google-maps-icons.googlecode.com/files/wineyard.png'; 
      }else if(businesses[i].business_type == "Golf"){ 
       var icon = 'http://google-maps-icons.googlecode.com/files/golf.png'; 
      }else{ 
       var icon = 'http://google-maps-icons.googlecode.com/files/festival.png'; 
      } 
      var marker = createMarker(point,businesses[i].name,description,icon); 


     }); 
    } 

Here is a working example

在環路一切都必須被包裹在回調它只有在地理編碼成功後才能使用。

基本上,我們將當前索引和回調傳遞給地理編碼函數。在地理編碼回調中,我們在全局上下文中調用回調函數(原始循環中的所有內容)(第一個變量是函數將在內部運行的上下文 - 我們使用window,以便它可以訪問所有全局 - 定義的變量)。我們也將該點傳遞給回調函數和當前索引,因爲我們不知道回調何時執行,因此我們需要確保它在運行時具有所需的所有內容。如果我們沒有通過索引循環將完成,然後不管i結束將用於依賴它的語句。

+0

問題是我希望能夠將輸出的值傳遞給localPoint.geocode()外的代碼的其他部分。我只是使用alert()作爲我希望能夠顯示輸出變量的示例。 – bigmike7801 2011-05-02 20:58:09

+0

然後調用你的函數,這取決於回調的位置INSIDE。我會更新答案以表明我的意思。 – ampersand 2011-05-02 21:01:15

+0

如果我在函數中包含t,那麼我可以通過這種方式返回結果呢?我會怎麼做?我將在上面更新我的代碼,以便您明白我的意思。 – bigmike7801 2011-05-02 21:11:55