Рекомендации Google Maps API PlacesService, показывающие результаты за пределами - PullRequest
0 голосов
/ 27 марта 2019

В настоящее время я использую библиотеку PlacesService из Google Maps API. Я хочу сделать предложения для Нью-Йорка. Я добавил strict_bounds для этого вопроса, но я все еще получаю результаты из Нью-Йорка, как показано на рисунке. enter image description here

По большей части это работает, но я получаю много предложений за пределами.

/** @private {?this._google.maps.Map} The google map object. */
this._map = new this._google.maps.Map(this._mapEl, {
  zoom: 11,
  center: this._mapPosition
});

/** @private {this._google.maps.places.Autocomplete}Autocomplete instance */
this._service = new this._google.maps.places.AutocompleteService();

/** @private {this._google.maps.places.PlaceService}PlaceService instance */
this._placeService = new this._google.maps.places.PlacesService(this._map);

Вот пример в конструкторе.

  // Attach handler for the autocomplete search box. This updates the map
    // position and re-sorts locations around that position.
    this._searchEl.addEventListener('keyup', (event) => {
      if(event.target.value) {
        this._service.getPlacePredictions({
           input: event.target.value,
           offset: 3,
           strictBounds: true,
           types: ['geocode'],
           bounds: this._map.getBounds()
        }, (predictions) => {
          if(predictions) {
            let results = predictions.map(e => [e['description']]);

            event.target.missplete = new MissPlete({
              input: event.target,
              options: results,
              className: 'c-autocomplete'
            })

            event.target.missplete.select = () => {
              let msplt = event.target.missplete;

              if (msplt.highlightedIndex !== -1) {
                msplt.input.value = msplt
                  .scoredOptions[msplt.highlightedIndex].displayValue;
                msplt.removeDropdown();

                console.dir('we did it');

              }
            };

            event.target.predictions = predictions;

            console.log(event.target.predictions);
          }
        });
      }
    });

Существует метод displayPlacesOnMap, который получает массив объектов Google Place. Здесь мы получаем place_id для использования библиотеки PlaceService.

   displayPlacesOnMap(mapItems) {
     if(mapItems) {
       mapItems.forEach(place => {
         let request = {
            placeId: place.place_id,
            fields: ['name', 'formatted_address', 'place_id', 'geometry']
         }

         const officeMap = this;

         this._placeService.getDetails(request, function(place, status) {
            if (status === 'OK') {
              officeMap._mapPosition = place.geometry.location;
              officeMap._map.panTo(officeMap._mapPosition);
              officeMap.sortByDistance().clearLocations().updateUrl().updateList()
                  .updateUrl();
              $(officeMap._searchEl).blur();
            }
         })
       })
     }
   };
...