MrNew
MrNew

Reputation: 1424

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.

Upvotes: 1

Views: 235

Answers (2)

lucas
lucas

Reputation: 1505

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)
    }
}

Upvotes: 0

br3w5
br3w5

Reputation: 4593

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=:latitude

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

Upvotes: 1

Related Questions