javascript - 地点自动完成库中的 IP 位置偏差

标签 javascript google-maps google-maps-api-3

我使用places-autocomplete js库来获取地址建议。 请参阅https://developers.google.com/maps/documentation/javascript/places-autocomplete 。 我使用自动完成类。

文档说

Note: If you do not supply any bounds or a map viewport, the API will attempt to detect the user's location from their IP address, and will bias the results to that location. If you would prefer to have no location bias, set the bounds to encompass the whole world: (-90,-180),(90,180).

当我使用仅带有类型参数的 API 并设置为“地址”时,我会收到来自世界各地的建议。

为什么我没有从 IP 中获得对位置的偏见?不支持与类型参数一起使用吗?

最佳答案

您需要将 componentRestrictions 参数添加到 Google 地方信息的配置中。

例如:

req.address = USER_INPUT
req.componentRestrictions = {
     country: ISO_COUNTRY_CODE
};
geocoder.geocode(req, function(results, status) {});

您可以通过 IP 地址对其进行偏置,但是您需要自己从 IP 获取国家/地区 ISO 代码。您可以使用 IP Find 之类的服务来执行此操作

例如。 https://ipfind.co/me

将返回一个country_code属性。 country_code 是您需要传递给上面的 Google Places 配置的 ISO 值。希望这会有所帮助。

关于javascript - 地点自动完成库中的 IP 位置偏差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44757154/

相关文章:

javascript - 外部 SVG 作为 Google map api 标记 - 是或否

angularjs - 如何在@agm/core 中为谷歌地图使用 api key 和客户端 ID?

google-maps - 让所有街道在 Google map 视口(viewport)中可见

javascript - 如何从复杂对象数组中获取唯一属性数组

javascript - jQuery datepicker 获取显示的月份和年份不是当前的

javascript - 如何在谷歌地图上显示项目类似于谷歌显示其结果的方式

javascript - 如何获取覆盖对象的事件处理程序?

javascript - 绘制标记时不显示 Google map api v3

javascript - 如何创建一个 jquery 函数来设置每个 div 的最大高度?

javascript - JQuery-UI ToolTip 位置