2016-11-07 93 views
0

門牌號碼我有這樣的地理編碼器代碼:的PhoneGap geocoder.geocode得到結果

function codeLatLng(lat, lng) { 
geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(lat, lng); 
    geocoder.geocode({'latLng': latlng}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     console.log(results) 
     if (results[1]) { 
     //formatted address 
     alert(results[0].formatted_address)//this is a string of all location 

     } else { 
      alert("No results found"); 
     } 
     } else { 
     alert("Geocoder failed due to: " + status); 
     } 
    }); 
    } 

我想保存在不同的變量街道和門牌號碼,我該怎麼辦呢?

回答

1

根據the documentation,而不是獲得formatted_address你可以從地址解析API響應address_components,然後得到例如street_number

"address_components" : [ 
    { 
     "long_name" : "1600", 
     "short_name" : "1600", 
     "types" : [ "street_number" ] 
    }, 
    ... 
] 

就可以得到所需的組件遍歷address_components(我在這個例子中檢索street_number):

for (var i = 0; i < results[0].address_components.length; i++) { 
    var address_components = results[0].address_components[i]; 
    if (address_components.types[0] == 'street_number') { 
     console.log(address_components.long_name); 
    } 
} 
+0

你可以在JavaScript上添加代碼如何得到它? – foo

+0

謝謝!很多! – foo