简体   繁体   中英

google map get postal code from latLng

been trying find a way to extract postcode from latlng position but im not entirely sure how to do it. can someone point me to the right direct please, im using google api autocomplete.

I tried using address_components but it only gives me short/long names and types.

You will need to use another service for this. postcodes.io is free, open source and uses open data. The request you are looking for is:

GET api.postcodes.io/postcodes?lon=:longitude&lat=:latitud e

Get the long lat from google maps and then make a separate request to a postcode service to get the postcode you are looking for.

The service above covers UK postcodes only. You may need to use multiple services for different countries.

See this related question on GIS Stack Exchange

if the data returned were called "addresses"...

var arr = addresses.results[0].address_components,
    i = 0,
    len = arr.length;
for (i; i < len; i++) {
    if (arr[i].types[0] === "postal_code") {
        alert(arr[i].long_name)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM