Reverse geocoding - getting street, city, country and zip code

1,310 views
Skip to first unread message

THE_AI

unread,
Jun 8, 2011, 8:52:18 AM6/8/11
to Google Maps JavaScript API v3
Hey guys!
I'm trying to use the reverse geocoder to update the fields in a
form.
so the user has a marker, he moves this marker and the street,city,
country and zip fields get automatically populated if the geocoder is
able to resolve this address.

I have this code here:
geocoder.geocode({
'latLng': latlng
}, function(results, status) {
var streetNumber = '';
var streetName = '';
var country = '';
var postalCode = '';
var city = '';
if (status == google.maps.GeocoderStatus.OK) {

results[0].address_components.each(function(el) {

el.types.each(function(type) {
if(type == 'street_number') {
streetNumber = el.long_name;
}

if(type == 'route') {
streetName = el.long_name;
}

if(type == 'country') {
country = el.long_name;
}

if(type == 'postal_code') {
postalCode = el.long_name;
}

if(type == 'locality') {
city = el.long_name;
}

})
});

document.id('street').set('value', streetName + streetNumber);
document.id('zip').set('value', postalCode);
document.id('town').set('value', city);
document.id('country').set('value', country);
}
});

Now I'm using the first result in the response, because I think that
it is always most accurate. Then I look for the type of each
address_components and map it to my variables.

Moving the marker around in spain seems to give me the proper results.
For example if I place the marker over Palma de mallorca - the
locality is Palma de Mallorca. If I go over Barcelona, then the
locality is Barcelona.

Now when I go over London, England I get really confusing results.
Instead of the city being London, the geocoder tells me that it is
either great london, lambeth or another boroughs of london.

There is one area of 1-2 square km that is mapped to London,
everything else is not london for the geocoder.

So my question here is - am I supposed to look for the city/zip/street/
country the way I do it or is there another way that will give me the
results I want?

Rossko

unread,
Jun 8, 2011, 2:46:33 PM6/8/11
to Google Maps JavaScript API v3
> Now when I go over London, England I get really confusing results.
> Instead of the city being London, the geocoder tells me that it is
> either great london, lambeth or another boroughs of london.
>
> There is one area of 1-2 square km that is mapped to London,
> everything else is not london for the geocoder.

That's postal addresses for you. The City of London is famously one
square mile, right next door to the City of Westmisnster. All are
within Greater London which has many other boroughs etc. etc.

> So my question here is - am I supposed to look for the city/zip/street/
> country the way I do it or is there another way that will give me the
> results I want?

Don't know what you want ; it isn't the postal address I guess ?

THE_AI

unread,
Jun 8, 2011, 4:22:41 PM6/8/11
to Google Maps JavaScript API v3
Hey Rossko,
thank you for the reply.
Maybe I'm just confused...


On Jun 8, 8:46 pm, Rossko <ros...@culzean.clara.co.uk> wrote:
> > Now when I go over London, England I get really confusing results.
> > Instead of the city being London, the geocoder tells me that it is
> > either great london, lambeth or another boroughs of london.
>
> > There is one area of 1-2 square km that is mapped to London,
> > everything else is not london for the geocoder.
>
> That's postal addresses for you.  The City of London is famously one
> square mile, right next door to the City of Westmisnster.  All are
> within Greater London which has many other boroughs etc. etc.
>


I'm reading the api docs and it says:
locality indicates an incorporated city or town political entity.

So I moved my marker to Islington and the response from google
geocoder contains Islington as the locality. Greater London is set as
the administrative_area_level_2.

Shouldn't the locality be London instead of Islington?
Islington is borough of London and not the city itself(locality)...

I went to a city in Germany that I know very well and started to move
the marker around. When I was moving the marker to suburbs of the city
the locality returned from the google geocoder was still the name of
the city.


> > So my question here is - am I supposed to look for the city/zip/street/
> > country the way I do it or is there another way that will give me the
> > results I want?
>
> Don't know what you want ; it isn't the postal address I guess ?

I want to identify the street, city, zip and country from the geocoder
result and store them in my DB of course :)

I don't understand this paragraph from the API doc:
"Generally, the more exact address is the most prominent result, as it
is in this case. Note that we return different types of addresses,
from the most specific street address to less specific political
entities such as neighborhoods, cities, counties, states, etc. If you
wish to match a more general address, you may wish to inspect the
results[].types field."

Does this mean that I should get all results and browse trough them
and look if there is a result containing just a postal code, street,
locality and country? Or is it ok to just go with the first returned
result as it is the one that is most accurate???

en4ce

unread,
Jun 8, 2011, 5:59:13 PM6/8/11
to Google Maps JavaScript API v3
"I want to identify the street, city, zip and country from the
geocoder
result and store them in my DB of course :) " i am not so sure but i
think thats against the tos anyway ;) you have to geocode it in real
time on the fly

Rossko

unread,
Jun 8, 2011, 9:00:19 PM6/8/11
to Google Maps JavaScript API v3
> So I moved my marker to Islington and the response from google
> geocoder contains Islington as the locality. Greater London is set as
> the administrative_area_level_2.

You might not like the way it works, but that sounds correct to me. A
peculiarity of London's administrative and postal office histories.
'Islington' is treated as a "town" within the metropolitan "county" of
'Greater London'.
It's all very pedantic but there is some background here
http://en.wikipedia.org/wiki/London#Scope

> I want to identify the street, city, zip and country from the geocoder
> result and store them in my DB of course :)

I think you already have; 22 Acacia Avenue, Islington, N1 9XY, UK is
an acceptable address form here.

As said, be wary of the terms of use.

There are other geocoders which may have different terms and/or may
return results more pleasing to your eye.

Reply all
Reply to author
Forward
0 new messages