1

我希望我的自动完成功能首先显示指定的国家,而不是其他国家。

现在我有这个:

const autocompleteService = new window.google.maps.places.AutocompleteService()
const location = window.google.maps.LatLng(48.856614, 2.3522219)
const query = {
  location: location,
  radius: 1000,
  types: ["(cities)"]
}
autocompleteService.getPlacePredictions(query, cb)

但是当我打字时lond,我London首先得到的是法国城市而不是法国城市(例如Londinières

编辑:

我也尝试了这个bounds选项,我得到了相同的结果

const bounds = new window.google.maps.LatLngBounds(
  new window.google.maps.LatLng(49.79295, 1.20374),
  new window.google.maps.LatLng(49.921316, 1.498147)
)
const autocompleteService = new window.google.maps.places.AutocompleteService()
const location = window.google.maps.LatLng(48.856614, 2.3522219)
const query = {
  bounds: bounds,
  types: ["(cities)"]
}

4

2 回答 2

1

AutocompletionRequest有一个名为的属性,componentRestrictions可用于提供国家列表以将搜索限制为:

const autocompleteService = new window.google.maps.places.AutocompleteService()
const location = window.google.maps.LatLng(48.856614, 2.3522219)
const query = {
  location: location,
  radius: 1000,
  types: ["(cities)"],
  componentRestrictions: { country: 'fr' }
}
autocompleteService.getPlacePredictions(query, cb)

这可能对您的目的过于严格,但您可以将此查询的结果与您的原始查询结合起来,以获得您首选国家/地区以外的建议。

于 2019-08-21T14:47:57.623 回答
0

您可以尝试此代码(前提是您可以先找到您的坐标。

var southWest = new google.maps.LatLng(42.97250, -3.82324);
var northEast = new google.maps.LatLng(51.64529, 5.49316);
var bounds = new google.maps.LatLngBounds(southWest,northEast);
var autocomplete = new google.maps.places.Autocomplete(input, {bounds: bounds});
google.maps.event.addListener(autocomplete, 'place_changed', function () {
  //...
});
于 2019-08-21T14:42:42.227 回答