Skip to content

Commit 5df4f40

Browse files
authored
docs: specify support for place ID parameter in distance_matrix.py (#400)
Updated param comments to specify support for place ID via 'place_id:' Per API documentation: "using place IDs is preferred over using addresses or latitude/longitude coordinates" Created test_place_id_param() to test case when origin/destination consist solely of place IDs. Updated test_mixed_params() to include a place ID in the origin.
1 parent fc0d026 commit 5df4f40

2 files changed

Lines changed: 46 additions & 9 deletions

File tree

googlemaps/distance_matrix.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,19 @@ def distance_matrix(client, origins, destinations,
2626
transit_routing_preference=None, traffic_model=None, region=None):
2727
""" Gets travel distance and time for a matrix of origins and destinations.
2828
29-
:param origins: One or more locations and/or latitude/longitude values,
30-
from which to calculate distance and time. If you pass an address as
31-
a string, the service will geocode the string and convert it to a
29+
:param origins: One or more addresses, Place IDs, and/or latitude/longitude
30+
values, from which to calculate distance and time. Each Place ID string
31+
must be prepended with 'place_id:'. If you pass an address as a string,
32+
the service will geocode the string and convert it to a
3233
latitude/longitude coordinate to calculate directions.
3334
:type origins: a single location, or a list of locations, where a
3435
location is a string, dict, list, or tuple
3536
36-
:param destinations: One or more addresses and/or lat/lng values, to
37-
which to calculate distance and time. If you pass an address as a
38-
string, the service will geocode the string and convert it to a
39-
latitude/longitude coordinate to calculate directions.
37+
:param destinations: One or more addresses, Place IDs, and/or lat/lng values
38+
, to which to calculate distance and time. Each Place ID string must be
39+
prepended with 'place_id:'. If you pass an address as a string, the
40+
service will geocode the string and convert it to a latitude/longitude
41+
coordinate to calculate directions.
4042
:type destinations: a single location, or a list of locations, where a
4143
location is a string, dict, list, or tuple
4244

tests/test_distance_matrix.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ def test_mixed_params(self):
8484
content_type="application/json",
8585
)
8686

87-
origins = ["Bobcaygeon ON", [41.43206, -81.38992]]
87+
origins = [
88+
"Bobcaygeon ON", [41.43206, -81.38992],
89+
"place_id:ChIJ7cv00DwsDogRAMDACa2m4K8"
90+
]
8891
destinations = [
8992
(43.012486, -83.6964149),
9093
{"lat": 42.8863855, "lng": -78.8781627},
@@ -95,7 +98,8 @@ def test_mixed_params(self):
9598
self.assertEqual(1, len(responses.calls))
9699
self.assertURLEqual(
97100
"https://maps.googleapis.com/maps/api/distancematrix/json?"
98-
"key=%s&origins=Bobcaygeon+ON%%7C41.43206%%2C-81.38992&"
101+
"key=%s&origins=Bobcaygeon+ON%%7C41.43206%%2C-81.38992%%7C"
102+
"place_id%%3AChIJ7cv00DwsDogRAMDACa2m4K8&"
99103
"destinations=43.012486%%2C-83.6964149%%7C42.8863855%%2C"
100104
"-78.8781627" % self.key,
101105
responses.calls[0].request.url,
@@ -181,3 +185,34 @@ def test_lang_param(self):
181185
"destinations=San+Francisco%%7CVictoria+BC" % self.key,
182186
responses.calls[0].request.url,
183187
)
188+
@responses.activate
189+
def test_place_id_param(self):
190+
responses.add(
191+
responses.GET,
192+
"https://maps.googleapis.com/maps/api/distancematrix/json",
193+
body='{"status":"OK","rows":[]}',
194+
status=200,
195+
content_type="application/json",
196+
)
197+
198+
origins = [
199+
'place_id:ChIJ7cv00DwsDogRAMDACa2m4K8',
200+
'place_id:ChIJzxcfI6qAa4cR1jaKJ_j0jhE',
201+
]
202+
destinations = [
203+
'place_id:ChIJPZDrEzLsZIgRoNrpodC5P30',
204+
'place_id:ChIJjQmTaV0E9YgRC2MLmS_e_mY',
205+
]
206+
207+
matrix = self.client.distance_matrix(origins, destinations)
208+
209+
self.assertEqual(1, len(responses.calls))
210+
self.assertURLEqual(
211+
"https://maps.googleapis.com/maps/api/distancematrix/json?"
212+
"key=%s&"
213+
"origins=place_id%%3AChIJ7cv00DwsDogRAMDACa2m4K8%%7C"
214+
"place_id%%3AChIJzxcfI6qAa4cR1jaKJ_j0jhE&"
215+
"destinations=place_id%%3AChIJPZDrEzLsZIgRoNrpodC5P30%%7C"
216+
"place_id%%3AChIJjQmTaV0E9YgRC2MLmS_e_mY" % self.key,
217+
responses.calls[0].request.url,
218+
)

0 commit comments

Comments
 (0)