2016-04-27 54 views
0

我想通過谷歌定位服務獲取經緯度pincode。我使用geocoder來獲取地址對象。我看到在某些地址getPostalCode爲空,但可以在地址行中看到郵政編碼。PinCode Null來自經緯度Android位置服務

如何在這種情況下獲得郵政編碼?我應該使用正則表達式來獲取它嗎?

回答

0

根據此documentation,您需要使用參數componentRestrictions指定限制,並且可以通過postalCodecountry進行過濾。

下面的示例函數演示使用componentRestrictions參數通過countrypostalCode過濾:

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上。