Google Maps API: How to get Zip Code from lat/lng coordinates?
Google Maps API: How to get Zip Code from lat/lng coordinates?
How can I use Google Maps API to obtain the zipcode from lat/lng coordinates?
Here's what I have tried so far
I have the code below which I am using to get Zipcode of the person on the webpage. It loads the latitude and longitude but its failing creating geocode object, the alert for GeoCoderStatus is not firing. Not sure where I am doing wrong any pointers, please:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>
<script type="text/javascript">
function geoloc()
if (navigator.geolocation)
navigator.geolocation.getCurrentPosition(
function (pos)
alert(pos.coords.longitude);
GeoToZip(pos.coords.latitude, pos.coords.longitude);
, function (err)
alert(err.message);
);
else
alert('Geolocation unsupported!');
function GeoToZip(lat, lng)
var latlng = new google.maps.LatLng(lat, lng);
var geocoder = new google.maps.Geocoder();
try
geocoder.geocode( 'latLng': latlng , function (res, status)
**alert(google.maps.GeocoderStatus);**
if (status == google.maps.GeocoderStatus.OK && typeof res[0] !== 'undefined')
var zip = res[0].formatted_address.match(/,sw2s(d5)/);
if (zip)
alert(zip[1]);
else fail('Failed to parse');
else
fail('Failed to reverse');
);
catch (err)
alert(err);
latLng
location
Possible duplicate of Latitude and longitude can find zip code?
– Alex R
Sep 17 '18 at 6:29
0
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
Geocoder request doesn't have property
latLng
, the correct name islocation
: developers.google.com/maps/documentation/javascript/…– xomena
Jun 19 '17 at 20:29