node.jsgoogle-mapsgoogle-maps-api-3google-apigoogle-places-api

google new text search api returning data out of bounds


I’m using Google’s new Places API with the Text Search feature, but I’m running into an issue. Often, when I search for a keyword in a specific location, I get results that are far outside the desired area. For example, if I search for "scrap recycling" in Niedeck 5, Germany, I’m getting results from the US and the UK, which makes no sense. I’ve set a location bias with a radius of 50,000 meters (the maximum limit), but it’s not respecting that boundary. The older Places API worked much better, as it returned results within the specified radius. Is there any way I can fix this in the new API? attaching a curl where i mentioned where the discrepancy is (Note : usually happens when i use nextPageToken)

curl --location 'https://places.googleapis.com/v1/places:searchText' \
--header 'Content-Type: application/json' \
--header 'X-Goog-Api-Key: KEY\
--header 'X-Goog-FieldMask: *' \
--data '{
"textQuery": "scrap recycling",
"locationBias": {
"circle": {
"center": {
"latitude": 51.49998193374399,
"longitude": 10.064777998776602
},
"radius": 50000.0
}
},
"pageToken":”XXXX"
}

I posted the same issue on https://issuetracker.google.com/ , they flagged it as intended and said to use locationRestriction instead of locationBias https://developers.google.com/maps/documentation/places/web-service/text-search#location-restriction . However the documentation did not mentioned how to use locationRestriction , tried several ways to implement but could not find the right way.

Needed at least some reliability and accuracy like i used to get using old API


Solution

  • You have a point, the documentation is missing a working example.

    For Text Search (New) you need to use the locationRestriction under SearchTextRequest, which only supports rectangle:

    curl -X POST -d '{
      "textQuery" : "scrap recycling",
      "pageSize" : "20",
      "locationBias": {
        "rectangle": {
          "low": {
            "latitude": 51.497,
            "longitude": 10.063   
          },
          "high": {
            "latitude": 51.500,
            "longitude": 10.065  
          }
        }
      }                                                                                                                                                    
    }' \
      -H 'Content-Type: application/json' \
      -H 'X-Goog-Api-Key: ...' \
      -H 'X-Goog-FieldMask: places.id,places.formattedAddress' \
      'https://places.googleapis.com/v1/places:searchText'
    

    Note: there are other locationRestrictions that support circle, but Text Search (New) does not support that and will reject it with

    "Invalid JSON payload received. Unknown name \"circle\" at 'location_restriction': Cannot find field."