2012-01-20 63 views
2

我目前使用谷歌地圖API來在地圖上的地理位置地址,並返回該街道的地址。如何從Google地圖地理編碼返回地址類型?

目前,我用下面的代碼返回地址:

 function codeLatLng(markerPos) { 
      geocoder.geocode({'latLng': markerPos}, function(results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 
       if (results[1]) { 
       //Set markerAddress variable 
       var markerAddress = results[0].formatted_address; 
alert(markerAddress); 
... 

但如果我不想使用地址元件類型返回格式的地址,但更詳細的版本,我怎麼能返回某個地址值如:http://code.google.com/apis/maps/documentation/geocoding/#Types

幫助讚賞。

回答

3

請問,你爲什麼爲results[1]檢查,然後使用results[0](或者是它只是一個錯字我應該忽略)?

只要statusOK,就會有至少一個結果。否則,status將是ZERO_RESULTS

無論如何,你可以使用這樣的事情:

function codeLatLng(markerPos) { 
    geocoder.geocode({'latLng': markerPos}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     var markerAddress = results[0].address_components[0].long_name 
      + ' (type: ' + results[0].address_components[0].types[0] + ')'; 
     alert(markerAddress); 

你可以玩很多與整個address_components陣列,玩得開心! :)

更好玩,看看谷歌地圖API V3地理編碼工具(的源代碼)在http://gmaps-samples-v3.googlecode.com/svn/trunk/geocoder/v3-geocoder-tool.html

1

long_name是由Geocoder返回的地址組件的全文描述或名稱。

function codeLatLng(markerPos) { 
       geocoder.geocode({'latLng': markerPos}, function(results, status) { 
        if (status == google.maps.GeocoderStatus.OK) { 
        if (results[1]) { 
        //Set markerAddress variable 
        var markerAddress = results[0].long_name; 
    alert(markerAddress); 
... 
+0

嗯......這提醒爲「未定義」任何其他的想法 – hairynuggets

相關問題