我想允许用户使用我的应用向 Google 地图添加地点。本教程展示了如何实现地点搜索https://developers.google.com/academy/apis/maps/places/basic-place-search我理解代码,但地点搜索和地点添加是不同的。在 Place Add 中,我们必须使用 POST URL 和 POST 正文https://developers.google.com/places/documentation/?hl=fr#adding_a_place。我不知道如何在我的代码中插入 POST 正文。我想使用此代码,但要使其适应 Place Add:
import urllib2
import json
AUTH_KEY = 'Your API Key'
LOCATION = '37.787930,-122.4074990'
RADIUS = 5000
url = ('https://maps.googleapis.com/maps/api/place/search/json?location=%s'
'&radius=%s&sensor=false&key=%s') % (LOCATION, RADIUS, AUTH_KEY)
response = urllib2.urlopen(url)
json_raw = response.read()
json_data = json.loads(json_raw)
if json_data[‘status’] == ‘OK’:
for place in json_data['results']:
print ‘%s: %s\n’ % (place['name'], place['reference'])'
编辑
感谢您的帮助@codegeek 我终于找到了基于此库的解决方案https://github.com/slimkrazy/python-google-places
url = 'https://maps.googleapis.com/maps/api/place/add/json?sensor=false&key=%s' % AUTH_KEY
data = {
"location": {
"lat": 37.787930,
"lng": -122.4074990
},
"accuracy": 50,
"name": "Google Shoes!",
"types": ["shoe_store"]
}
request = urllib2.Request(url, data=json.dumps(data))
response = urllib2.urlopen(request)
add_response = json.load(response)
if add_response['status'] != 'OK':
# there is some error