我想允許用戶使用我的應用程序向Google地圖添加地點。本教程演示如何實現地方搜索https://developers.google.com/academy/apis/maps/places/basic-place-search我瞭解代碼,但地方搜索和地方添加是不同的。就地添加,我們必須使用POST URL和POST主體https://developers.google.com/places/documentation/?hl=fr#adding_a_place。我不知道如何在我的代碼中插入POST正文。我想利用這個代碼,但適應它放在地址:實現基本Google地方使用Python添加
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