有人有使用 django-gmapi 进行地理编码的经验吗?

发布于 2024-12-15 14:46:13 字数 96 浏览 1 评论 0原文

作者说: Google Geocoding Web 服务的第 3 版还实现了进一步启用不显眼的 JavaScript 方法。但是,我在他们的网站上找不到任何示例。以前有人用过吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡月浅 2024-12-22 14:46:13

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

>>> # 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

将其重新粘贴到 Google 地图中,您就会看到One Exchange Plaza,就像我们一样通缉。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文