-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlacesAPI.py
More file actions
40 lines (31 loc) · 1.19 KB
/
PlacesAPI.py
File metadata and controls
40 lines (31 loc) · 1.19 KB
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
31
32
33
34
35
36
37
38
39
import requests
from urllib.parse import urlencode
api_key = "AIzaSyAtkUswEgSgGbODpMZtOoWm4N5-z3_t3N8"
lat, lng = 0.0, 0.0
base_endpoint_places = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json"
def get_location(sentence):
params = {
"key": api_key,
"input": f"{sentence}",
"inputtype": "textquery",
"fields": "place_id,formatted_address,name,geometry,permanently_closed"
}
location_bias = f"point:{lat},{lng}"
circular = True
return request_api(location_bias, params, circular)
def request_api(locationbias, params, use_cirular):
locationbias = use_circular(locationbias, use_cirular)
params['locationbias'] = locationbias
params_encoded = urlencode(params)
places_endpoint = f"{base_endpoint_places}?{params_encoded}"
response = requests.get(places_endpoint)
resp_json_payload = response.json()
if len(resp_json_payload['candidates']) == 0:
return []
else:
return resp_json_payload['candidates'][0]['geometry']['location']
def use_circular(locationbias, use_cirular):
if use_cirular:
radius = 5000
locationbias = f"circle:{radius}@{lat},{lng}"
return locationbias