Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get latitude and longitude from zip code javascript

Im trying to write a function that will give return latitude and longitude from a zip code. I am attempting to use google maps api to do this.

I am seeing that geocoder seems to be good for this but I am struggling to know how to use it. What im looking to do is use a function to return a passed in zip code to return as an array with the latitude and longitude coordinates.

I created an account to get an API key. For example lets say my function is:

function getCoordinates (zipcode);

How can I write this out so that passed in zipcode would return the lat and long? Thanks for any responses!

like image 799
John Avatar asked Sep 06 '25 09:09

John


1 Answers

You're right, geocoding api is what you need. Basically you have to perform a get request to

https://maps.googleapis.com/maps/api/geocode/json?address=YOUR_ZIP&key=YOUR_API_KEY

You also may want to specify a country for search. To achieve this, just add region param with country code (for example region=us)

like image 115
Eugene Tsakh Avatar answered Sep 08 '25 11:09

Eugene Tsakh