【发布时间】:2016-04-27 16:17:43
【问题描述】:
我正在尝试使用谷歌位置服务获取经度和纬度的密码。我使用地理编码器来获取地址对象。我看到某些地址 getPostalCode 为 Null,但在地址行中可以看到邮政编码。
在这种情况下如何获取邮政编码?我应该使用正则表达式来获取它吗?
【问题讨论】:
标签: android google-maps geolocation
我正在尝试使用谷歌位置服务获取经度和纬度的密码。我使用地理编码器来获取地址对象。我看到某些地址 getPostalCode 为 Null,但在地址行中可以看到邮政编码。
在这种情况下如何获取邮政编码?我应该使用正则表达式来获取它吗?
【问题讨论】:
标签: android google-maps geolocation
基于此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);
}
});
}
你可以在 GitHub 上查看这个example。
【讨论】: