3

我在将 componentRestrictions 参数添加到 Google Maps Geocoder 对象时遇到问题。我正在使用 JavaScript 客户端 api。具体来说,该administrativeArea物业不适合我。文档说 type 是一个字符串,我尝试了很多组合但仍然没有运气。

这是我的地理编码功能:

function geocodeAddress(geocoder, resultsMap) {
    var address = document.getElementById('address').value;
    geocoder.geocode({'address': address, 'componentRestrictions': {administrativeArea: 'Maricopa County'}}, function(results, status) {
      if (status === 'OK') {
        resultsMap.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
          map: resultsMap,
          position: results[0].geometry.location
        });
        console.log(results)
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }

我可以让其他 componentRestriction 属性工作,比如postalCodeand country,但不是administrativeArea。文档说该属性匹配所有的administrative_area levels,所以我尝试使用不同的美国县和不同的美国州,但无法让它工作。我想我的语法错误,任何帮助将不胜感激!

4

1 回答 1

4

你的代码是正确的。不幸的是,前段时间组件过滤行为发生了重要变化。看起来 Maps JavaScript API 文档尚未更新有关组件限制的内容。

我建议查看更新的相应 Web 服务文档并说明以下内容

可以过滤的组件包括:

  • postal_code匹配 postal_code 和 postal_code_prefix。

  • country匹配国家名称或两个字母的 ISO 3166-1 国家代码。注意:API 遵循 ISO 标准定义国家,使用国家对应的 ISO 代码时过滤效果最好。

以下组件可用于影响结果,但不会强制执行:

  • route匹配路由的长名称或短名称。

  • 地点与地点和次地点类型相匹配。

  • 管理区域匹配所有管理区域级别。

来源:https ://developers.google.com/maps/documentation/geocoding/intro#ComponentFiltering

因此,正如您所见,行政区域不再是严格的过滤器。目前唯一可用的严格过滤器是邮政编码和国家。

希望我的回答能澄清你的疑惑。

于 2018-09-04T23:06:07.850 回答