0
我想通過谷歌定位服務獲取經緯度pincode。我使用geocoder來獲取地址對象。我看到在某些地址getPostalCode
爲空,但可以在地址行中看到郵政編碼。PinCode Null來自經緯度Android位置服務
如何在這種情況下獲得郵政編碼?我應該使用正則表達式來獲取它嗎?
我想通過谷歌定位服務獲取經緯度pincode。我使用geocoder來獲取地址對象。我看到在某些地址getPostalCode
爲空,但可以在地址行中看到郵政編碼。PinCode Null來自經緯度Android位置服務
如何在這種情況下獲得郵政編碼?我應該使用正則表達式來獲取它嗎?
根據此documentation,您需要使用參數componentRestrictions
指定限制,並且可以通過postalCode
或country
進行過濾。
下面的示例函數演示使用componentRestrictions
參數通過country
和postalCode
過濾:
function codeAddress() {
geocoder.geocode({
componentRestrictions: {
country: 'AU',
postalCode: '2000'
}
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
window.alert('Geocode was not successful for the following reason: ' + status);
}
});
}
您可以檢查此example在GitHub上。