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);







Geocoder request doesn't have property latLng, the correct name is location: developers.google.com/maps/documentation/javascript/…

– xomena
Jun 19 '17 at 20:29



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

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế