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
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.
It's free as long as you credit them and you need fewer than 15000 lookups per day.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With