【发布时间】:2019-09-03 08:19:48
【问题描述】:
目前我正在使用来自 Google Maps API 的 AutocompleteService 类。从documentation 我使用bounds 来应用纽约市的界限。但是,我正在从 NJ 和 PA 获得结果。
AutocompleteService 类没有strictBounds 选项,这使得很难将结果限制在范围内。以下是我为纽约市提供的以下界限:
/**
* NYC Bound Parameters for Google Maps API
* @enum {object}
*/
OfficeMap.NYC_BOUNDS = {
'south': 40.4773991,
'west': -74.25908989999999,
'north': 40.9175771,
'east': -73.7002721
};
正如您从上面的图片中看到的那样,显示了来自 PA 的建议。
/** @private {this._google.maps.places.Autocomplete}Autocomplete instance */
this._service = new this._google.maps.places.AutocompleteService();
// 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,
types: ['geocode'],
componentRestrictions: {country: 'us'},
bounds: OfficeMap.NYC_BOUNDS
}, predictions => {
if(predictions) {
let results = predictions.map(e => [e['description']]);
this._inputAutocomplete._autocomplete.options = results;
const autocompleteSelf = this._inputAutocomplete._autocomplete;
this._inputAutocomplete._autocomplete.select = () => {
if (autocompleteSelf.highlightedIndex !== -1) {
autocompleteSelf.input.value = autocompleteSelf
.scoredOptions[autocompleteSelf.highlightedIndex]
.displayValue;
autocompleteSelf.removeDropdown();
let nameOfLocation = autocompleteSelf.input.value;
let googlePlaceObj = this.getGooglePlaceByName(nameOfLocation,
predictions);
this.displayPlacesOnMap(googlePlaceObj);
}
};
}
});
}
});
getPlacePredictions 方法应该只给出 NYC 建议,因为传递的是 NYC bounds 参数。来自documentation
没有办法限制它。
如何将 AutocompleteService getPlacePredictions 限制为仅返回范围内的对象?
【问题讨论】:
-
虽然烦人,但
bounds的 the documentation you linked to 确实说“预测将偏向于但不限于给定的范围。”
标签: java google-maps google-maps-api-3