Google Api не получает почтовый индекс каждый раз для данного города, штата, страны |Обратное геокодирование - PullRequest
0 голосов
/ 28 ноября 2018

Я пытался получить почтовый индекс с помощью обратного геокодирования, но для некоторого адреса, например: Сиань, Шэньси, Китай, Гуйлинь, Гуанси, Китай. Я не могу получить почтовый индекс.Есть ли способ убедиться, что Google Api вернет почтовый индекс для указанного адреса путем обратного геокодирования.

Пожалуйста, проверьте приведенный ниже код, который я использовал для получения латинского слова для города, штата, страны'и почтовый индекс с этим латинским.

Точка входа для получения почтового индекса - 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"
}

Любая помощь будет оценена.

...