Gmaps results only local even when i query another city

36 views Asked by At

I made a python program that takes a business type and location then outputs results in that location. The output is always local in the twin cities.

def get_places(query, location):
    parameters = {"query": query, "location": location, "key": GOOGLE_API_KEY}

    all_results = []
    response = requests.get(ENDPOINT, params=parameters)
    results = response.json().get("results", [])
    all_results.extend(results)

results print confirming it should be searching tacoma WA but instead the results I keep getting minneapolis where I live: https://maps.googleapis.com/maps/api/place/textsearch/json?query=food&location=tacoma%2C+WA&key=xxxxxxxxxKEYxxxxxxxxxx&pagetoken=AUacShjNDU-_ibehFFVXMZseXVaF3h2S6P4DAXaIwWpJHgPuDvcCF95vgMiYjBmPiQOtiEcJvmLuFNWuoQBWEw1RYH8FKlPi7h4-jUsq2oYdpeEsoT00YojkdA7GofRggFd9KMCoRM_r-lyXnD7P4ZD6ylkeM029ycQ-9v6jdt6qobrmIq3Y6gikuRxOxHINMaFpnu-EMK5JvGkX46L_tEqz_dbur-rio4ivQPLvygqKcAjMfdMFrQWPsGzKMC7FtTVTCpwoGKKlEOIAr4cxNHtzGBgjTkUH4idJNKlCWpS1NJi15s-J40A1wJ0Wg5oFGV3OrCh5rUHR_ym28B98w8dnrMxibECfdljveS89MWJRR7BxIrQeLSYheNgu-_-_T0rmFNA

clearing cache and cookies didnt help either. The only thing that makes sense to me is a setting in my Cloud Console but I can't find anything that could be causing this.

2

There are 2 answers

0
Nick ODell On

The docs for the location parameter say that it must be specified as a latitude/longitude pair:

location

The point around which to retrieve place information. This must be specified as latitude,longitude.

https://developers.google.com/maps/documentation/places/web-service/search-text#location

You'll need to geocode your location before using it in a query.

0
Yrll On

Either use a location parameter specifying latlng or explicitly put the location in the query parameter.

You can't use an address name in the location parameter. What you should use is a latlng coordinate as mentioned in the documentation:

"...This must be specified as latitude,longitude."

I tried to geocode the location you specified (Tacoma WA) which is around 47.25248908430412, -122.44652831940736. So your query should look something like this:

https://maps.googleapis.com/maps/api/place/textsearch/json?query=food&location=47.25248908430412,%20-122.44652831940736&key=API_KEY

The other way to do this is to explicitly mention the location in the query parameter itself. A note under the location parameter description in the documentation also says this:

"The location parameter may be overriden if the query contains an explicit location such as Market in Barcelona. Using quotes around the query may also influence the weight given to the location and radius."

This means that instead of including a latlng location parameter, you can just put the "Tacoma WA" inside the query parameter together with the string "food". Your query should look like this:

https://maps.googleapis.com/maps/api/place/textsearch/json?query=food%20in%20Tacoma%20WA&key=API_KEY

This should provide you the expected results instead of the local foods in your area.

The most probable reason for this is because the location value you used is invalid, it searched for "foods" using your current location instead.

I hope this helps!