1

I am trying to restrict the bounds in use-places-autocomplete as below

  requestOptions: {
      types: ["geocode"],
      fields: [
        "name",
        "address_components",
        "geometry.location",
        "place_id",
        "formatted_address",
      ],
      
      bounds: window.google.maps.LatLngBounds(bounds?.n, bounds?.w),
      componentRestrictions: {
        country: "ke",
      },

      initOnMount: true,
      cache: false,
    },

only this does not work. The bounds are gotten from geocode api and set as below

setBounds({
      n: results[0].geometry.bounds.getNorthEast(), 
      w: results[0].geometry.bounds.getSouthWest()
    })

is there something I am doing wrong

Taio
  • 3,152
  • 11
  • 35
  • 59
  • Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) or a working codesandbox that demonstrates the issue so that the community could take a look at it and troubleshoot it better. – Yrll Mar 22 '23 at 03:10

1 Answers1

-1

To restrict Place Autocomplete results to the given bounds, try using strictBounds to the requestOptions. At least that's how it works in the Google Maps JavaScript API.

miguev
  • 4,481
  • 21
  • 41
  • There is no "strictBounds" in use-places-autocomplete package – Taio May 20 '23 at 17:27
  • 2
    Sorry, didn't realize this was about react. I'd suggest filing a bug or feature request for the use-places-autocomplete package then; `strictBounds` is available in the JavaScript Places library already. – miguev May 21 '23 at 19:16
  • I took a look at the [library's repo](https://github.com/wellyshen/use-places-autocomplete), and it looks like the library is using the [Places Autocomplete Service](https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service) instead of the [Places Widget](https://developers.google.com/maps/documentation/javascript/reference/places-widget) which uses the `strictBounds`. Is it similar to [locationRestriction](https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#AutocompletionRequest.locationRestriction)? @miguev – Yrll May 30 '23 at 23:59