使用自动完成服务()将 Google 地图地点限制为特定国家/地区

Restricting Google Maps Places to Specific Countries using AutocompleteService()

本文关键字:地区 国家 地图 Google 服务      更新时间:2023-09-26

使用google.maps.places.AutocompleteService如何限制搜索国家(加拿大和美国)。 如果您使用的是AutoComplete则可以将国家/地区添加到选项中,但使用AutocompleteService似乎并非如此。 我在查看文档时也发现了componentRestrictions,但这也不起作用。

您如何限制使用 Google 地方信息搜索的国家/地区?

function initService(controlInput) {
  var service = new google.maps.places.AutocompleteService();
  var displaySuggestions = function(predictions, status) {
      if (status != google.maps.places.PlacesServiceStatus.OK) {
          console.log(status);
          return;
      }
      predictions.forEach(function(prediction) {
          console.log(prediction);
      });
  };
  controlInput.addEventListener('keyup', function(event, input) {
      service.getQueryPredictions({ 
        input: controlInput.value,
        //country: 'ca',  <--- doesn't work
        componentRestrictions: {
          country: 'ca' <--- also doesn't work
        }
      }, displaySuggestions);
  });
 }

尝试遵循组件筛选中的本指南:

在地理编码响应中,Google 地图地理编码 API 可以返回仅限于特定区域的地址结果。限制是使用组件过滤器指定的。筛选器由由竖线 (|) 分隔的组件:值对列表组成。仅返回与所有筛选器匹配的结果。过滤器值支持与其他地理编码请求相同的拼写更正和部分匹配方法。如果地理编码结果与组件过滤器部分匹配,则响应中将包含partial_match字段。

可以筛选的组件包括:

    路由
  • 匹配路由的长名称或短名称。
  • 局部性
  • 与局部性和子局部性类型匹配。
  • administrative_area匹配所有administrative_area级别。
  • postal_code匹配postal_code和postal_code_prefix。
  • 国家
  • /地区与国家/地区名称或两个字母的 ISO 3166-1 国家/地区代码匹配。

: 每个地址组件只能在地址参数中指定或作为组件过滤器指定,而不能同时指定两者。这样做可能会导致ZERO_RESULTS

带有components=country:ES的"圣克鲁斯"的地理编码将返回西班牙加那利群岛的圣克鲁斯德特内里费岛。请求:

https://maps.googleapis.com/maps/api/geocode/json?address=santa+cruz&components=country:ES&key=YOUR_API_KEY
同样在这里 将搜索限制

在特定国家/地区,了解如何实施限制搜索并遵循ISO 3166-1(两个字母的国家/地区代码)。我希望这有所帮助。

您正在使用不接受组件限制选项的 AutocompleteService.getQueryPredictions 方法。它对 AutocompleteService.getPlacePredictions 方法有效。请参阅文档:

自动完成服务:https://developers.google.com/maps/documentation/javascript/reference#AutocompleteService

自动完成请求:https://developers.google.com/maps/documentation/javascript/reference#AutocompletionRequest

查询自动完成请求:https://developers.google.com/maps/documentation/javascript/reference#QueryAutocompletionRequest

我使用了一个国家/地区选择选项来设置autocomplete.setComponentLimits的值。

自动完成将仅限于从该国家/地区进行搜索。请原谅格式,因为我刚刚将其转储在这里。

    function setAutocompleteCountry() {
    var country = document.getElementById('id_countryselect').value;
    if (country === 'ALL') {
        autocomplete.setComponentRestrictions({'country': []});
    } else {
        autocomplete.setComponentRestrictions({'country': country});
    }
}
autocomplete.addListener('place_changed', function () {
        var place = autocomplete.getPlace();
    ------> Rest of code

您可以在启动自动完成服务后将其与 getPlacesPredicitions 一起使用。您可以像这样将其包含在请求对象中:

 const service = new google.maps.places.AutocompleteService();
 let request = {
     input: val,                    
     componentRestrictions: {country: 'us'},
 };
 service.getPlacePredictions(request, (predictions, status) => {
    this.addresses = predictions
   //each predicition is one address
 });