为什么属性“半径”不起作用getPlacePredictions谷歌地图

问题描述 投票:0回答:3

我使用getPlacePredictions获取结果自动完成谷歌地图。我在莫斯科使用中心地图。我读了文档https://developers.google.com/maps/documentation/javascript/reference#AutocompletionRequest

我看到radius类型编号并以米(50000)指定。但是我得到距离650000的结果。例子我键入“Sara”并获得结果“Saransk,Россия”。我的位置 - 是莫斯科。距离莫斯科和萨兰斯克有65万米的距离。

我的代码示例:https://jsfiddle.net/bg479gna/33/

const autoComplete = new google.maps.places.AutocompleteService();
const input = document.querySelector('#search');
const result = document.querySelector('#result');
const getPlaceAutocomplete = (value) => {
  autoComplete.getPlacePredictions({
    input: value,
    location: new google.maps.LatLng(55.755826, 37.617300), //Moscow
    radius: 50000, //this value 50km. But i get cities on distance 650km. WHY?!?!
    componentRestrictions: {
      country: 'ru',
    },
  }, (data) => {
    data.forEach((item) => {
        result.innerHTML += `<li>${item.description}</li>`
    });
  });
};

input.addEventListener('keyup', () => {
    const value = input.value;

    getPlaceAutocomplete(value)
});

我怎么解决这个问题?

我尝试使用getQueryPredictionsgetPredictions而不是getPlacePredictions,但我得到了相同的结果。

google-maps google-maps-api-3
3个回答
0
投票

你可以qazxsw poi参数。但qazxsw poi不支持。更多strictBounds

AutocompleteService


2
投票

这只是一种偏见,而不是严格的限制。从info.你可以参考(强调我的):

location类型:LatLng预测偏置的位置。预测将偏向给定的位置和半径。或者,可以使用边界。 radius类型:number用于预测偏置的区域的半径。半径以米为单位指定,并且必须始终附有位置属性。或者,可以使用边界。


-1
投票

如果您想为每个国家设置here.限制,您需要the documentation请参阅strictgoogle.maps.places.ComponentRestrictions

AutoCompleteService ClassAutoCompleteService Request

希望这可以帮助!

© www.soinside.com 2019 - 2024. All rights reserved.