【问题标题】:Google Geocoder different results between url and javascript requestsurl和javascript请求之间的谷歌地理编码器不同的结果
【发布时间】:2016-03-04 22:37:13
【问题描述】:

我正在使用带有 Google 地理编码的组件过滤来获取有关位置的数据。首先,我用拼写错误的“London”将这个链接称为“Lindon”,但没有返回任何内容。好,我不想让它找到任何东西!

http://maps.googleapis.com/maps/api/geocode/xml?components=locality:Lindon%7Ccountry:gb

但是当我通过 javascript 调用它时,它会返回“伦敦”数据。我不希望它,我希望它不会像在 URL 请求中那样找到它。我没有正确使用 Javascript 调用吗?这是link我用来编程调用谷歌地理编码器。

var geocoder = new google.maps.Geocoder();
geocoder.geocode({
  componentRestrictions: {
    country: 'GB',
    locality: 'Lindon'
  }
}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    if (results.length >= 1) {
      for (var ii = 0; ii < results[0].address_components.length; ii++) {
        var street_number = route = street = city = state = zipcode = country = formatted_address = '';
        var types = results[0].address_components[ii].types.join(",");
        if (types == "street_number") {
          addr.street_number = results[0].address_components[ii].long_name;
        }
        if (types == "route" || types == "point_of_interest,establishment") {
          addr.route = results[0].address_components[ii].long_name;
        }
        if (types == "sublocality,political" || types == "locality,political" || types == "neighborhood,political" || types == "administrative_area_level_3,political") {
          addr.city = (city == '' || types == "locality,political") ? results[0].address_components[ii].long_name : city;
        }
        if (types == "administrative_area_level_1,political") {
          addr.state = results[0].address_components[ii].short_name;
        }
        if (types == "postal_code" || types == "postal_code_prefix,postal_code") {
          addr.zipcode = results[0].address_components[ii].long_name;
        }
        if (types == "country,political") {
          addr.country = results[0].address_components[ii].long_name;
        }
      }
      addr.success = true;
      //for (name in addr) {
      //    console.log('### google maps api ### ' + name + ': ' + addr[name]);
      //}
      response(addr);
    } else {
      response({
        success: false
      });
    }
  } else {
    response({
      success: false
    });
  }
});

【问题讨论】:

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


【解决方案1】:

实际上,它相当简单(也许)。基本上,顺序很重要。查看区别:

https://maps.googleapis.com/maps/api/geocode/xml?components=country:GB|locality:Lindon

https://maps.googleapis.com/maps/api/geocode/xml?components=locality:Lindon|country:GB

对于 JS 地理编码,请参阅

https://jsbin.com/furodil/edit?html,js,console,output

在我这里的 JSBin 示例中,顺序是“错误的”,并返回空结果;如果你翻转它们,它会返回伦敦部分匹配。您让 Web 服务和 JS Geocoder 以不同的顺序发送组件。

我认为这与基于部分结果的“排除自身的组件”有关。如果您单独搜索locality:Lindon,它会显示为在美国的某个地方。因此,一旦您搜索该内容并 THEN 查询英国,它将记录这两者不能一起工作,并将返回 ZERO_RESULTS 结果。见https://developers.google.com/maps/documentation/geocoding/intro#ComponentFiltering

Component filtering will return a ZERO_RESULTS response only if you provide filters that exclude each other.

这里的教训似乎是:总是从最一般的组件到最具体的组件。

【讨论】:

    【解决方案2】:

    在结果数组中,有一个 partial_match 字段表明地理编码器无法为您的请求返回完全匹配的内容。你可能想用这个。更多细节在这里:

    https://developers.google.com/maps/documentation/geocoding/intro#Results
    

    我还创建了一个 JSFiddle 来测试这个:

    https://jsfiddle.net/k6yh2jLu/7/
    

    【讨论】:

    • 我知道部分匹配,但我的问题与为什么使用两种不同的方法从谷歌地图获取地理编码结果,使用相同的输入得到不同的结果???
    • 它们返回不同的结果,因为直接链接(您发布的那个)没有进行部分匹配。
    • 那不是真的。这里的文档:developers.google.com/maps/documentation/geocoding/… 表明 Web 服务支持 partial_match 就像 Javascript API 一样。需要进一步研究...
    • 你是对的!!它确实进行部分匹配。我的错。
    猜你喜欢
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多