Preferred reverse geocoding solutions to get state and city for a point?

68 views
Skip to first unread message

Niklas Rosencrantz

unread,
Dec 6, 2011, 11:39:26 AM12/6/11
to google-map...@googlegroups.com, alex.h...@gmail.com, kim...@gmail.com, rober...@gmail.com, shan...@gmail.com, hol...@gmail.com, staffan....@gmail.com, g...@eddaconsult.se, kyriakos.ko...@gmail.com
I wanted output with links like these for my objects:

Brazil > São Paulo > São Bernardo do Campo

or

India > Maharashtra > Mumbai

I think that serverside works better than javascript and I'd like you to comment this. I used javascript reverse geocoding for some time and found it unfavorable since it renders after the page is loaded. I'd like to see a serverside solution using JSON or XML to my problem, I asked 2 questions about it on stackoverflow and this week it appears solved and I wonder if you can comment or answer eg. why these functions are not directly available from the API. Since I'm using the grouping /<region>/<city>/<category>? to retrieve my objects combined with a db.geopt for every object I liked to see a solution
and we solved it at SO where you may compare 2 solutions: http://stackoverflow.com/questions/8395252/how-to-reverse-geocode-serverside-with-python-json-and-google-maps
I will reward the answer a bounty since this was important for me and the answer is very neat (I also published a solution that "works" but is not as pythonistic as the one from the answer.)

The answer I was sent that does it is

import json
import urllib2

def get_geonames(lat, lng, types):
    url
= 'http://maps.googleapis.com/maps/api/geocode/json' + \
           
'?latlng={},{}&sensor=false'.format(lat, lng)
    jsondata
= json.load(urllib2.urlopen(url))
    address_comps
= jsondata['results'][0]['address_components']
    filter_method
= lambda x: len(set(x['types']).intersection(types))
   
return filter(filter_method, address_comps)

lat
, lng = 59.3, 18.1
types
= ['locality', 'administrative_area_level_1']

# Display all geographical names along with their types
for geoname in get_geonames(lat, lng, types):
    common_types
= set(geoname['types']).intersection(set(types))
   
print '{} ({})'.format(geoname['long_name'], ', '.join(common_types))


IMHO the google geocoding API is missing these functions:
  • admin_area.get_localities
  • country.get_admin_areas
  • point.get_admin_area_level_1 (state/region)
  • point.get_locality (city)
A link to the javascript question about this is http://stackoverflow.com/questions/8394759/how-to-get-the-region-city-from-a-geocoding-object/8394840#8394840
And a link to my map using this technology (now with the server code instead of javascript) is http://www.montao.com.br/vi/4414192.html
where it clearly works printing the state (Sao Paulo) and the city (Sao Bernando do Campos) and there is no confusion which is the region and which is the city anymore.
Thank you
crumbs-br.png
crumbs-in.png
Reply all
Reply to author
Forward
0 new messages