Python-Geocoding [WARNING!!]

Warning

Please be cautious when you run the code below, especially when you turn on Places API and use text_search in googlemaps!!!

Although requesting Basic Data is free, it could be quite expensive when you geocode address information with text_search. (Price of text_search)


To retreive latitude and longitude of specific address of Hong Kong, I subscribe Google Maps Geocoding API and install two packages googleplaces and googlemaps. The first one is thrid-party tool and the second is official one.

1
2
3
4
pip install python-google-places
pip install --upgrade https://github.com/slimkrazy/python-google-places/zipball/master ## upgrade package and solve 2,500-request limit

pip install -U googlemaps

Referring to blog of DHogan [1], geocoding code is listed below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from googleplaces import GooglePlaces
import googlemaps
import json

class GoogleMaps(object):
"""提供google maps服务"""

def __init__(self):

self._GOOGLE_MAPS_KEY = "My API Key"
self._Google_Places = GooglePlaces(self._GOOGLE_MAPS_KEY)
self._Google_Geocod = googlemaps.Client(key=self._GOOGLE_MAPS_KEY)

def _text_search(self, query, language=None, location=None):
"""
根据搜索字符串,请求google API传回推荐的列表
:param query: 搜索字符串
:param language: 语言
:param location: 地区筛选
:return:
"""
# lat_lng = {"lat": "22.5745761", "lng": "113.9393772"}
# 经纬度附近搜索
# text_query_result = self.self._Google_Places.text_search(query='Gong Yuan', lat_lng=lat_lng)
# location 为人可认识的名称
# text_query_result = self.self._Google_Places.text_search(query='Tang Lang Shan', location='shenzhen')
# 指定语言搜索
text_query_result = self._Google_Places.text_search(query=query, language=language, location=location)
return text_query_result.places


Python-Geocoding [WARNING!!]
http://arwenzhou.github.io/2020/11/20/Python-Geocoding/
Author
Arwen
Posted on
November 20, 2020
Licensed under