Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add locationbias parameter to places autocomplete #491

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions googlemaps/places.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

"""
Expand All @@ -620,6 +627,7 @@ def places_autocomplete(
types=types,
components=components,
strict_bounds=strict_bounds,
location_bias=location_bias
)


Expand Down Expand Up @@ -674,6 +682,7 @@ def _autocomplete(
types=None,
components=None,
strict_bounds=False,
location_bias=None
):
"""
Internal handler for ``autocomplete`` and ``autocomplete_query``.
Expand Down Expand Up @@ -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", [])
7 changes: 6 additions & 1 deletion tests/test_places.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,23 @@ def test_autocomplete(self):
types="geocode",
components={"country": "au"},
strict_bounds=True,
location_bias="circle:[email protected],-81.0"
)

self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"%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:[email protected],-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"
Expand Down