forked from googlemaps/google-maps-services-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaces.py
More file actions
195 lines (152 loc) · 6.48 KB
/
places.py
File metadata and controls
195 lines (152 loc) · 6.48 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#
# Copyright 2015 Google Inc. All rights reserved.
#
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#
"""Performs requests to the Google Places API."""
from googlemaps import convert
def places(client, query, location=None, radius=None, language=None,
min_price=None, max_price=None, open_now=False, types=None,
page_token=None):
"""
Places search.
:param query: The text string on which to search, for example: "restaurant".
:type query: string
:param location: The latitude/longitude value for which you wish to obtain the
closest, human-readable address.
:type location: string, dict, list, or tuple
:param radius: Distance in meters within which to bias results.
:type radius: int
:param language: The language in which to return results.
:type langauge: string
:param min_price: Restricts results to only those places with no less than
this price level. Valid values are in the range from 0 (most affordable)
to 4 (most expensive).
:type min_price: int
:param max_price: Restricts results to only those places with no greater
than this price level. Valid values are in the range from 0 (most
affordable) to 4 (most expensive).
:type max_price: int
:param open_now: Return only those places that are open for business at
the time the query is sent.
:type open_now: bool
:param types: Restricts the results to places matching at least one of the
specified types. The full list of supported types is available here:
https://developers.google.com/places/supported_types
:type types: string or list of strings
:param page_token: Token from a previous search that when provided will
returns the next page of results for the same search.
:type page_token: string
:rtype: result dict with the following keys:
results: list of places
html_attributions: set of attributions which must be displayed
next_page_token: token for retrieving the next page of results
"""
params = {"query": query}
if location:
params["location"] = convert.latlng(location)
if radius:
params["radius"] = radius
if language:
params["language"] = language
if min_price:
params["minprice"] = min_price
if max_price:
params["maxprice"] = max_price
if open_now:
params["opennow"] = "true"
if page_token:
params["pagetoken"] = page_token
return client._get("/maps/api/place/textsearch/json", params)
def place(client, place_id, language=None):
"""
Comprehensive details for an individual place.
:param place_id: A textual identifier that uniquely identifies a place,
returned from a Places search.
:type place_id: string
:param language: The language in which to return results.
:type langauge: string
:rtype: result dict with the following keys:
result: dict containing place details
html_attributions: set of attributions which must be displayed
"""
params = {"placeid": place_id}
if language:
params["language"] = language
return client._get("/maps/api/place/details/json", params)
def places_photo(client, photo_reference, max_width=None, max_height=None):
"""
Downloads a photo from the Places API.
:param photo_reference: A string identifier that uniquely identifies a
photo, as provided by either a Places search or Places detail request.
:type photo_reference: string
:param max_width: Specifies the maximum desired width, in pixels.
:type max_width: int
:param max_height: Specifies the maximum desired height, in pixels.
:type max_height: int
:rtype: iterator containing the raw image data, which typically can be
used to save an image file locally. For example:
```
f = open(local_filename, 'wb')
for chunk in client.photo(photo_reference, max_width=100):
if chunk:
f.write(chunk)
f.close()
```
"""
if not (max_width or max_height):
raise ValueError("a max_width or max_height arg is required")
params = {"photoreference": photo_reference}
if max_width:
params["maxwidth"] = max_width
if max_height:
params["maxheight"] = max_height
# "extract_body" and "stream" args here are used to return an iterable
# response containing the image file data, rather than converting from
# json.
response = client._get("/maps/api/place/photo", params,
extract_body=lambda response: response,
requests_kwargs={"stream": True})
return response.iter_content()
def places_autocomplete(client, input_text, offset=None, location=None,
radius=None, language=None):
"""
Returns Place predictions given a textual search query, such as
"pizza near New York", and optional geographic bounds.
:param input_text: The text query on which to search.
:type input_text: string
:param offset: The position, in the input term, of the last character
that the service uses to match predictions. For example, if the input
is 'Google' and the offset is 3, the service will match on 'Goo'.
:type offset: int
:param location: The latitude/longitude value for which you wish to obtain the
closest, human-readable address.
:type location: string, dict, list, or tuple
:param radius: Distance in meters within which to bias results.
:type radius: number
:param language: The language in which to return results.
:type langauge: string
:rtype: list of predictions
"""
params = {"input": input_text}
if offset:
params["offset"] = offset
if location:
params["location"] = convert.latlng(location)
if radius:
params["radius"] = radius
if language:
params["language"] = language
response = client._get("/maps/api/place/queryautocomplete/json", params)
return response["predictions"]