我试图通过反向地理编码来获取邮政编码,但要针对某些地址,例如: 陕西西安 中国广西桂林 我无法提取邮政编码。有什么方法可以确保Google Api通过反向地理编码返回给定地址的邮政编码。
请检查以下我用来从lat-lng中获取“城市,州,国家/地区”和邮政编码的邮政编码。
获取邮政编码的入口点- searchPostalCode
const Geocoder = geocoder => {
const extendedGeocoder = {
searchLatLang: address => {
const google = window.google
return new Promise((resolve, reject) => {
geocoder.geocode({
address
}, (addressResults, addressStatus) => {
if (addressStatus === google.maps.GeocoderStatus.OK) {
const addressResult = addressResults[0]
if (addressResult) {
const {
location: {
lat, lng
}
} = addressResult.geometry
resolve({
lat: lat(),
lng: lng()
})
} else {
reject()
}
} else {
reject()
}
})
})
},
extractPostalCode: results => {
return results.reduce((currentPostalCode, address) => {
const newPostalCode = address.address_components.reduce(
(postalCodeValue, component) => {
return component.types[0] === 'postal_code' && !postalCodeValue ? component.long_name : postalCodeValue
},
null
)
return newPostalCode && !currentPostalCode ? newPostalCode : currentPostalCode
}, null)
},
searchPostalCode: address => {
const google = window.google
return new Promise((resolve, reject) => {
return extendedGeocoder
.searchLatLang(address)
.then(location => {
geocoder.geocode({
location
}, (latLngResults, latLngStatus) => {
if (latLngStatus === google.maps.GeocoderStatus.OK) {
resolve(extendedGeocoder.extractPostalCode(latLngResults))
} else {
reject()
}
})
})
.catch(reject)
})
}
}
return extendedGeocoder
}
回应中国陕西西安
{
"plus_code" : {
"compound_code" : "2222+22 Meixian, Baoji, Shaanxi, China",
"global_code" : "8P6C2222+22"
},
"results" : [
{
"address_components" : [
{
"long_name" : "Zhouzhi",
"short_name" : "Zhouzhi",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "Xi'an",
"short_name" : "Xi'an",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Shaanxi",
"short_name" : "Shaanxi",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "China",
"short_name" : "CN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Zhouzhi, Xi'an, Shaanxi, China",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 34.2351243,
"lng" : 108.5176627
},
"southwest" : {
"lat" : 33.6938767,
"lng" : 107.6637527
}
},
"location" : {
"lat" : 34.163669,
"lng" : 108.222162
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 34.2351243,
"lng" : 108.5176627
},
"southwest" : {
"lat" : 33.6938767,
"lng" : 107.6637527
}
}
},
"place_id" : "ChIJ_-LaQlDQYzYRqO9YiA5nA7s",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"address_components" : [
{
"long_name" : "Xi'an",
"short_name" : "Xi'an",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Shaanxi",
"short_name" : "Shaanxi",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "China",
"short_name" : "CN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Xi'an, Shaanxi, China",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 34.7431036,
"lng" : 109.8224325
},
"southwest" : {
"lat" : 33.6938767,
"lng" : 107.6637527
}
},
"location" : {
"lat" : 34.341574,
"lng" : 108.93977
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 34.7431036,
"lng" : 109.8224325
},
"southwest" : {
"lat" : 33.6938767,
"lng" : 107.6637527
}
}
},
"place_id" : "ChIJuResIul5YzYRLliUp_1m1IU",
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Shaanxi",
"short_name" : "Shaanxi",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "China",
"short_name" : "CN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Shaanxi, China",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 39.5870104,
"lng" : 111.2480539
},
"southwest" : {
"lat" : 31.7050596,
"lng" : 105.4919489
}
},
"location" : {
"lat" : 35.3939908,
"lng" : 109.1880047
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 39.5870104,
"lng" : 111.2480539
},
"southwest" : {
"lat" : 31.7050596,
"lng" : 105.4919489
}
}
},
"place_id" : "ChIJn6frE2B6YzYRFldhy29uyUE",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"address_components" : [
{
"long_name" : "China",
"short_name" : "CN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "China",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 53.5609739,
"lng" : 134.7754563
},
"southwest" : {
"lat" : 17.9996,
"lng" : 73.4994136
}
},
"location" : {
"lat" : 35.86166,
"lng" : 104.195397
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 53.5609739,
"lng" : 134.7754563
},
"southwest" : {
"lat" : 17.9996,
"lng" : 73.4994136
}
}
},
"place_id" : "ChIJwULG5WSOUDERbzafNHyqHZU",
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}
任何帮助将不胜感激。