Google Geocoder在url和javascript请求之间产生了不同的结果

时间:2016-03-04 22:37:14

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

我正在使用Google地理编码的组件过滤来获取有关位置的数据。首先,我将这个链接称为错误的“伦敦”作为“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
    });
  }
});

2 个答案:

答案 0 :(得分:2)

实际上,它很简单(也许)。基本上,订单很重要。看看差异:

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

VS

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

对于JS地理编码,请参阅

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

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

我认为这与排除自身的&#34;组件有关&#34;基于部分结果。如果您单独搜索locality:Lindon,它就会显示在美国的某个地方。因此,一旦您搜索了英国的那个和那个查询,它将注册这两个不能一起工作并返回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.

这里的课程似乎是:始终从最常见的组件转到最具体的组件。

答案 1 :(得分:1)

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

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

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

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