From d326343f00a4fef3da6f3f97437bafabccff675b Mon Sep 17 00:00:00 2001 From: Bruno Caimar Date: Fri, 16 Jun 2023 19:18:51 -0300 Subject: [PATCH] feat: add locationbias parameter to places autocomplete --- googlemaps/places.py | 14 ++++++++++++++ tests/test_places.py | 7 ++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/googlemaps/places.py b/googlemaps/places.py index 269a17fa..8310163b 100644 --- a/googlemaps/places.py +++ b/googlemaps/places.py @@ -556,6 +556,7 @@ def places_autocomplete( types=None, components=None, strict_bounds=False, + location_bias=None ): """ Returns Place predictions given a textual search string and optional @@ -604,6 +605,12 @@ def places_autocomplete( the region defined by location and radius. :type strict_bounds: bool + :param location_bias: Prefer results in a specified area, by specifying + either a radius plus lat/lng, or two lat/lng pairs + representing the points of a rectangle. See: + https://developers.google.com/maps/documentation/places/web-service/autocomplete#locationbias + :type location_bias: string + :rtype: list of predictions """ @@ -620,6 +627,7 @@ def places_autocomplete( types=types, components=components, strict_bounds=strict_bounds, + location_bias=location_bias ) @@ -674,6 +682,7 @@ def _autocomplete( types=None, components=None, strict_bounds=False, + location_bias=None ): """ Internal handler for ``autocomplete`` and ``autocomplete_query``. @@ -702,6 +711,11 @@ def _autocomplete( params["components"] = convert.components(components) if strict_bounds: params["strictbounds"] = "true" + if location_bias: + valid = ["ipbias", "circle", "rectangle"] + if location_bias.split(":")[0] not in valid: + raise ValueError("location_bias should be prefixed with one of: %s" % valid) + params["locationbias"] = location_bias url = "/maps/api/place/%sautocomplete/json" % url_part return client._request(url, params).get("predictions", []) diff --git a/tests/test_places.py b/tests/test_places.py index 32f03d16..ab2a33fe 100644 --- a/tests/test_places.py +++ b/tests/test_places.py @@ -223,6 +223,7 @@ def test_autocomplete(self): types="geocode", components={"country": "au"}, strict_bounds=True, + location_bias="circle:500@41.0,-81.0" ) self.assertEqual(1, len(responses.calls)) @@ -230,11 +231,15 @@ def test_autocomplete(self): "%s?components=country%%3Aau&input=Google&language=en-AU&" "origin=-33.86746%%2C151.20709&" "location=-33.86746%%2C151.20709&offset=3&radius=100&" - "strictbounds=true&types=geocode&key=%s&sessiontoken=%s" + "strictbounds=true&types=geocode&key=%s&sessiontoken=%s&" + "locationbias=circle:500@41.0,-81.0" % (url, self.key, session_token), responses.calls[0].request.url, ) + with self.assertRaises(ValueError): + self.client.places_autocomplete("Google", location_bias="invalid") + @responses.activate def test_autocomplete_query(self): url = "https://maps.googleapis.com/maps/api/place/queryautocomplete/json"