有人有使用django-gmapi进行地理编码的经验吗? [英] Does anybody has experiences with geocoding using django-gmapi?

查看:118
本文介绍了有人有使用django-gmapi进行地理编码的经验吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作者说:Google地理编码Web服务的版本3也被实现,以进一步启用一个不引人注目的JavaScript方法。但是,我的网站上找不到任何示例。以前有人使用过吗?

解决方案

这是一个简单的例子。我以前没有使用 django-gmapi ,所以这可能不是最好的方法。

 >>> #导入Geocoder类并实例化
>>>来自gmapi.maps import Geocoder
>>> geocoder = Geocoder()
>>>> #让我们对Stack Exchange地址进行地理编码!
>>> stack_exchange_hq =纽约纽约市26楼的一个交流广场
>>>结果,status_code = geocoder.geocode({'address':stack_exchange_hq})
>>>>打印结果
{'address_components':[{'long_name':'1',
'short_name':'1',
'types':['street_number']},
{'long_name':'Exchange Plaza',
'short_name':'Exchange Plaza',
'types':['route']},
{'long_name'市区',
'short_name':'市区',
'类型':['邻居','政治']},
{'long_name':'曼哈顿',
'short_name':'Manhattan',
'types':['sublocality','political']},
{'long_name':'New York',
'short_name' '纽约',
'types':['locality ,'政治']},
{'long_name':'纽约',
'short_name':'纽约',
'types':['administrative_area_level_2',
'political']},
{'long_name':'New York',
'short_name':'NY',
'types':['administrative_area_level_1',
'政治']},
{'long_name':'美国',
'short_name':'US',
'types':['country','political']} ,
{'long_name':'10006',
'short_name':'10006',
'types':['postal_code']}],
'formatted_address' '1交易广场,纽约, 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,
'类型':['street_address']}

>>> #查看第一个(而且只)结果
>>> result = results [0]
>>> lat,lng = result ['geometry'] ['location'] ['arg']
>>> print lat,lng
40.707183 -74.013402

坚持回谷歌地图,你得到一个交流广场,就像我们想要的。


注意当我解析上面的结果时,我没有抓到任何错误。您的应用程序应该可以将结果缓存在数据库中,以便您的页面加载不会通过地理编码查询而减慢,因此您不会遇到任何API限制。


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?

解决方案

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.

这篇关于有人有使用django-gmapi进行地理编码的经验吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆