首页 > 解决方案 > How to return autocomplete values only in a specific radius in Google Autocomplete (ngx-autocom-place)?

问题描述

I'm trying to use Google Places in my Angular app to return to the user in an autocomplete input only address result which are in a radius from a location, so from the documenentation i should use parameters as location radius and strictbounds.

To use autocomplete i'm using the following ngx library but the issue is that after setting all the parameters in the options when i'm trying to write a certain address firstly i get only the one that are in the range BUT if i anyway try to write one out of range or even another city that is out of range it will still return that values and i would prevent it.

Here are my autocomplete options

  addressOptions = {
    types: ['address'],
    componentRestrictions: {
      country: 'it'
    },
    location: new google.maps.LatLng( 44.7179422, 10.5465457),
    radius: '1000',
    strictbounds: true
  };

        <input
          type="text"
          class="form-control"
          required
          placeholder="es. Via D'israeli"
          formControlName="indirizzo"
          ngxAutocomPlace
          [options]="addressOptions"
          (selectedPlace)="addressChange($event)"
        />

标签: angulargoogle-places

解决方案


mytyuk, it seems strictBounds will only be take into account when passing bounds property (https://developers.google.com/maps/documentation/javascript/places-autocomplete)

strictBounds is a boolean specifying whether the API must return only those places that are strictly within the region defined by the given bounds. The API does not return results outside this region even if they match the user input.


推荐阅读