问题
The author said: Version 3 of the Google Geocoding Web service is also implemented to further enable an unobtrusive javascript approach. However, I can not find any examples on their sites. Has anybody used it before?
回答1:
Here's a quick example. I haven't used django-gmapi
before, so this might not be the best approach.
>>> # import the Geocoder class and instantiate it
>>> from gmapi.maps import Geocoder
>>> geocoder = Geocoder()
>>> # Let's geocode the Stack Exchange address!
>>> stack_exchange_hq = "One Exchange Plaza, 26th Floor, New York, NY"
>>> results, status_code = geocoder.geocode({'address': stack_exchange_hq })
>>> print results
{'address_components': [{'long_name': '1',
'short_name': '1',
'types': ['street_number']},
{'long_name': 'Exchange Plaza',
'short_name': 'Exchange Plaza',
'types': ['route']},
{'long_name': 'Downtown',
'short_name': 'Downtown',
'types': ['neighborhood', 'political']},
{'long_name': 'Manhattan',
'short_name': 'Manhattan',
'types': ['sublocality', 'political']},
{'long_name': 'New York',
'short_name': 'New York',
'types': ['locality', 'political']},
{'long_name': 'New York',
'short_name': 'New York',
'types': ['administrative_area_level_2',
'political']},
{'long_name': 'New York',
'short_name': 'NY',
'types': ['administrative_area_level_1',
'political']},
{'long_name': 'United States',
'short_name': 'US',
'types': ['country', 'political']},
{'long_name': '10006',
'short_name': '10006',
'types': ['postal_code']}],
'formatted_address': '1 Exchange Plaza, New York, NY 10006, USA',
'geometry': {'location': {'arg': [40.707183, -74.013402], 'cls': 'LatLng'},
'location_type': 'ROOFTOP',
'viewport': {'arg': [{'arg': [40.705834, -74.014751],
'cls': 'LatLng'},
{'arg': [40.708532, -74.012053],
'cls': 'LatLng'}],
'cls': 'LatLngBounds'}},
'partial_match': True,
'types': ['street_address']}
>>> # look at the first (and only) result
>>> result = results[0]
>>> lat, lng = result['geometry']['location']['arg']
>>> print lat, lng
40.707183 -74.013402
Stick that back into google maps, and you get One Exchange Plaza, as we wanted.
Note I'm not catching any errors when I parse the results above. Your app should probably cache the results in the database, so that your page loads are not slowed down by geocoding queries, and so you don't hit any api limits.
来源:https://stackoverflow.com/questions/8143755/does-anybody-has-experiences-with-geocoding-using-django-gmapi