I want to allow users to add places to Google Maps using my app. This tutorial shows how to implement a Place Search https://developers.google.com/academy/apis/maps/places/basic-place-search I understand the code but Place Search and Place Add are different. In Place Add we have to use a POST URL and POST body https://developers.google.com/places/documentation/?hl=fr#adding_a_place. I don’t know how to insert POST body in my code. I want to use this code but to adapt it to 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'])'
EDIT
Thanks for your help @codegeek I finally find the solution based on this library 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
If you read the urllib2 documentation at http://docs.python.org/library/urllib2, it clearly states the following:
“urllib2.urlopen(url[, data][, timeout])
So, you need to call the urlopen function with data parameter which will then send the request through POST. Also. looking through the Google Places Add API page, you need to prepare the data which includes location, accruracy etc. urlencode() it and you should be good.
If you want an example, see this gist at:
https://gist.github.com/1841962#file_http_post_httplib.py