Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving Postal Code with Google Maps Javascript API V3 Reverse Geocode

Tags:

I'm trying to submit a query using the postal code to my DB whenever the googlemaps viewport center changes. I know that this can be done with reverse geocoding with something like:

google.maps.event.addListener(map, 'center_changed', function(){ newCenter(); }); ... function newCenter(){ var newc = map.getCenter(); geocoder.geocode({'latLng': newc}, function(results, status){ if (status == google.maps.GeocoderStatus.OK) {   var newzip = results[0].address_components['postal_code'];   } }); }; 

Of course, this code doesn't actually work. So I was wondering how I would need to change this in order to extract the postal code from the results array. Thanks

like image 241
Romaine M. Avatar asked Mar 17 '11 15:03

Romaine M.


People also ask

How do I get the post code for Google Maps?

Open a contact or an organization with a postal address. Beside the postal address, click on the map icon. The postal address will automatically open on Google Maps.

Is Google Maps reverse geocoding free?

It's free as long as you credit them and you need fewer than 15000 lookups per day.

Is reverse geocoding API free?

Reverse Geocoding and Geolocation Service by Noggle is a free API that allows developers to embed the functionality to locate the largest city or nearest one to the latitude to longitude location. Another free reverse geocoding API is Opencage Geocoder by Opencage.


1 Answers

What I've realized so far is that in most cases the ZIPCODE is always the last value inside each returned address, so, if you want to retrieve the very first zipcode (this is my case), you can use the following approach:

var address = results[0].address_components; var zipcode = address[address.length - 1].long_name; 
like image 159
Tuco Avatar answered Sep 23 '22 16:09

Tuco