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
types
array, which may be last or first or anywhere in between. Besides, which element? You may want to take the postal code fromresults[0].address_components
or fromresults
itself: try with both and see what works best in the area you care about. In general, I'd recommendresults[0].address_components
if you care about places with a full address, andresults
if you case about strict containment of your latlng in the postal code you get. – Studied