如何防止谷歌地理编码器从其他国家/地区返回结果

How to prevent google geocoder from returning results from other countries

本文关键字:其他国家 地区 返回 结果 编码器 何防止 谷歌      更新时间:2023-09-26

我正在使用谷歌地理编码器,可以选择只返回来自德国的结果

这是我函数的相关部分

    ...
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({"address":address,"region":"DE" }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[0].geometry.location) {
                completeGeo(results[0],address);
            } else {
                completeGeo(null,address);
            }
     ...

但是,如果我对"cuvry"进行地理编码,也可以找到德国的那条街道

谷歌返回例如"法国库夫里",它位于区域参数之外

如何防止谷歌地理编码器返回不在某个国家/地区的结果?我的意思是返回,如果国家/地区代码匹配,则不签入回调。

这可能适用于组件过滤器。"组件":"国家:德国"

var geocoder = new google.maps.Geocoder();
geocoder.geocode({"address":address, "componentRestrictions":{"country":"DE"} },
function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        if (results[0].geometry.location) {
            completeGeo(results[0],address);
        } else {
            completeGeo(null,address);
        }
});

当我将其更改为 geocoder.geocode({"address":address, "componentRestrictions":{"country":"DE"} }, 对于德国,我的搜索结果找到了越南。

我已将其更改为:

geocoder.geocode( {'address':address + ', Deutschland'}, function(results, status)

这适用于使用自己语言的国家/地区名称。

简短的回答是你不能。

该问题已在此处提交给谷歌:

https://code.google.com/p/gmaps-api-issues/issues/detail?id=4233

您唯一可以尝试的是将"可接受的边界"传递给地理编码器。它远非完美,但确实对我的项目有所帮助。这是我用于尝试将Google地理编码器限制在美国西部的代码的粗略副本。显然,您希望编辑边界以表示您感兴趣的区域。

var geocoder = new google.maps.Geocoder()
var callGoogleGeocoder = function(str) {
  var geo_bounds;
  geo_bounds = setGeoBounds();
  return geocoder.geocode({
    address: str,
    bounds: geo_bounds
  }, function(results, status) {
    console.log(results);
  });
}
var setGeoBounds = function() {
  var geo_bounds, ne, sw;
  geo_bounds = new google.maps.LatLngBounds();
  sw = new google.maps.LatLng(41.24843789608676, -126.5633079709794);
  ne = new google.maps.LatLng(49.01224853841337, -108.3479759397294);
  geo_bounds.extend(sw);
  geo_bounds.extend(ne);
  return geo_bounds;
};

请花点时间在我上面链接的谷歌上对这个问题进行投票。对我来说,这是谷歌地理编码器缺少的#1功能。

祝你好运!

使用完整的国家/地区名称(例如。 The Netherlands而不是 NL ),因为后者不起作用(在 NL 之外返回结果):

geocoder.geocode({"address":address, "componentRestrictions":{"country":"The Netherlands"} },

我刚刚遇到了同样的问题并以这种方式解决了它(对于德国):

var searchObject = {
    'address': address,
    componentRestrictions: {
        country: 'DE'
    }
};

geocoder().geocode(searchObject, function (result, status) {
    if (status !== google.maps.GeocoderStatus.OK) {
        return reject();
    }
    if (result[0].formatted_address === 'Deutschland' && address !== 'Deutschland') {
        return reject();
    }
    return reject();
});

因此,当您搜索错误的地址时,Google在使用时始终Deutschland返回componentRestrictions地址。这是我让它工作的唯一方法,因为当您删除componentRestrictions Google 时,会产生一个猜测的位置,这对于我的用例来说是不正确的。

例:搜索邮政编码 12345,该代码在德国无效。

  1. 没有componentRestrictions — 结果:Schenectady, New York 12345, USA

  2. Deutschland添加到地址 — 结果:12345 High Germany Rd SE, Little Orleans, MD 21766, USA

  3. 使用componentRestrictions — 结果:Deutschland

更新

另一种解决方案是检查partial_match标志:

if (result[0].partial_match) {
    return reject();
}