Getting Postal Code from latitude and longitude

6,586 views
Skip to first unread message

Wathek LOUED

unread,
Sep 1, 2010, 6:12:13 PM9/1/10
to Google Maps JavaScript API v3
Hi everyone,

I need some help I'd like to get the postal code of some regions using
the latitude and the longitude of that place I know I've to use the
reverse geocoding and that's what I've been using but the problem is
to find the postal code from the returned answer here's the code I'm
using :

function codeLatLng(latlng) {
var input = latlng;
var latlngStr = input.split(",",2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
document.getElementById("airports").innerHTML+="<div
id='airport'><b>Lattitude, Longitude:</b>" + latlng + "<b>Country :</
b>" + results[0].address_components[6].long_name + "<b>Postal Code : </
b>" + results[0].address_components[7].long_name + "</div>";
}
} else {
alert("Geocoder failed due to: " + status);
}
}

but the problem is that not always
results[0].address_components[7].long_name contains the postal code.

So do you know how can I retrieve the postal code ?

Thank you
Sincerely
Wathek

Dom

unread,
Sep 30, 2010, 9:37:32 AM9/30/10
to Google Maps JavaScript API v3
Hi,

I have same problem. Will this isue be corrected ?

Kind regards,
Dominique

geoco...@gmail.com

unread,
Sep 30, 2010, 10:37:46 AM9/30/10
to Google Maps JavaScript API v3
On Sep 30, 6:37 am, Dom <dominique.chou...@gmail.com> wrote:
> Hi,
>
> I have same problem. Will this isue be corrected ?

What issue?
Does the result with address_type postal_code (if it exists), not work
for you?

-- Larry


>
> Kind regards,
> Dominique

Rossko

unread,
Sep 30, 2010, 11:00:07 AM9/30/10
to Google Maps JavaScript API v3
Depending what you are doing, you might be expecting too much of the
reverse geocoder ; it provides a postal address - if it can.
Not every postal address on the planet is known to the geocoder, so
which country the query relates to can matter a lot.
Not every location has a postal address at all. The original poster
mentioned 'regions' ; his code mentions 'airports'. I wouldn't expect
to get a postcode for a mountain or a runway.
Examples showing the problem always help.

Esa

unread,
Sep 30, 2010, 12:05:06 PM9/30/10
to Google Maps JavaScript API v3
You assume that postal code is located in

results[0].address_components[7].long_name

Actually it can be in results[1] as well and in any
address_components. You should scan through the results and
address_components to find one that has property

types: ["postal_code"]


Esa

unread,
Sep 30, 2010, 12:27:43 PM9/30/10
to Google Maps JavaScript API v3
See this thread.
http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/c212d58c5617ca0c/6fa29c724a6ec71d

It is about finding country name that is technically quite similar
problem. The last message presents a function that loops through the
address details and returns an object that has properties like
'country', 'postal_code' etc. The author of the function almost forgot
the existence of such a function and its demo page.
Reply all
Reply to author
Forward
0 new messages