Javascript – Get City/State from Latitude/Longitude via jquery/ajax with google maps geocode

google mapsgoogle-maps-api-3javascriptjquery

I've seen a few jscript/jquery implementations of this concept in reverse, where you can enter a zip code and get a long/lat from the google maps api.

However, in my case, I already have a set of coordinates and was wondering if its possible to dynamically get a textual City, State result from the API when we feed it the long/latitude via jquery?

Thanks!

Best Answer

This is a process called reverse geocoding, and Google have quite extensive documentation on it.

An example would be:

$.ajax({ url:'http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true',
         success: function(data){
             alert(data.results[0].formatted_address);
             /*or you could iterate the components for only the city and state*/
         }
});