Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Latitude and longitude can find zip code?

I am using Google geocoder for lat and lon and my question is, is there a way you can find out zipcode with latitude and longitude?

like image 953
Nick Kahn Avatar asked Jul 20 '11 16:07

Nick Kahn


1 Answers

It's good to note that Google Maps has a new version since this solultion was presented.

Reference: https://developers.google.com/maps/documentation/geocoding/?csw=1#ReverseGeocoding

Here's an updated example for Google Maps v3. It makes use of the Address Components that JIssak mentions above. I should note that there is no fallback. If it fails to find a zip code, it does nothing. This may or may not be important to your script.

var latlng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);
geocoder = new google.maps.Geocoder();

    geocoder.geocode({'latLng': latlng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[0]) {
                for (j = 0; j < results[0].address_components.length; j++) {
                    if (results[0].address_components[j].types[0] == 'postal_code')
                        alert("Zip Code: " + results[0].address_components[j].short_name);
                }
            }
        } else {
            alert("Geocoder failed due to: " + status);
        }
    });
like image 58
HBlackorby Avatar answered Oct 26 '22 14:10

HBlackorby