Convert ZIP to latitude and longitude (Javascript)

Asked

Viewed 34,056 times

6

I need a Javascript api that converts a typed zip code to latitude and longitude, someone knows one I can use for free?

  • Do you want to do this conversion so you can search for the position on google maps? If so, it would not be easier to search the maps directly by the zip code?

  • 1

    have tried this, but he does not recognize the zip code to add a marker

  • Hence the API code changes, has a specific ZIP code. Use Google’s own guide for this link, type the zip code and it will generate an example code, just need to have the google key to use.

2 answers

12


You can use the geocoding of the google api.
Add the Google API

<script src="http://maps.google.com/maps/api/js"></script>

Then just pass the zip code or address to get the latitude and longitude

var lat = '';
var lng = '';
var address = {cep} or {endereço};
geocoder.geocode( { 'address': address}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
     lat = results[0].geometry.location.lat();
     lng = results[0].geometry.location.lng();
  } else {
     alert("Não foi possivel obter localização: " + status);
  }
});
alert('Latitude: ' + lat + ' Logitude: ' + lng);
  • 2

    Now in 2018 that service is paid for. Someone still uses?

5

15 de April de 2019

Access the URL below:

https://maps.googleapis.com/maps/api/geocode/json?address=SEU_CEP&key=SUA_API_KEY

It will return a JSON containing the full address and latitude and longitude.

For this URL to work, you need to have an account on Google Developers Console and this account needs to have a registered credit card. The amount is approximately $7.00 per 1,000 requests.

Your API_KEY must have enabled Maps Javascript API and Geocode API

Browser other questions tagged

You are not signed in. Login or sign up in order to post.