Google地理编码区域仅在某些情况下有效

时间:2014-12-03 16:43:21

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

我认为这是一个错误,我将找出将其提交给谷歌的地方,但我想我先问这里,以防我做错了什么或误解了什么。我不能让谷歌的地理编码器始终如一地使用区域偏见。例如,以下内容确实有效:

  • https://maps.googleapis.com/maps/api/geocode/json?address=boston返回马萨诸塞州波士顿
  • https://maps.googleapis.com/maps/api/geocode/json?address=boston&region=uk返回英格兰林肯郡的波士顿

但以下情况不起作用

  • https://maps.googleapis.com/maps/api/geocode/json?address=manchester返回英国曼彻斯特
  • https://maps.googleapis.com/maps/api/geocode/json?address=manchester&region=us仍然返回英国曼彻斯特

波士顿,林肯郡和曼彻斯特,CT都没有出现在无区域请求的结果中,所以我认为它们的工作方式应该相同。但是,重要的是要注意它可能取决于手动区域条目。以下内容无法按预期工作,并且都返回波士顿,马萨诸塞州:

  • https://maps.googleapis.com/maps/api/geocode/json?address=boston&region=ca应该是安大略省的波士顿
  • https://maps.googleapis.com/maps/api/geocode/json?address=boston&region=us-ky应该是波士顿,肯塔基州

编辑:JS API

@ dr-molle接受的回答是正确的。但是,如果您使用的是Google的JS API,则无法指定components。您可以在bounds中指定边界区域。要查找边界,您需要LatLng个对象作为矩形框的NE和SW点。对于CT,我做了以下几点:

CT_NE_POINT = new google.maps.LatLng(42.05, -71.783333);
CT_SW_POINT = new google.maps.LatLng(40.966667, -73.733333);
CT_BOUNDS = new google.maps.LatLngBounds(CT_SW_POINT, CT_NE_POINT);

...

geocoder = new google.maps.Geocoder();
geocoder.geocode({address: 'manchester', bounds: CT_BOUNDS}, someCallbackFunction);

编辑2:componentRestrictions

JavaScript API接受componentRestrictions对象。您会看到the docs在对象文字中没有显示componentRestrictions,但它确实在下面描述了它(它没有提到它是一个对象)。上面第一个编辑中的代码可以简化为:

geocoder = new google.maps.Geocoder();
geocoder.geocode({
  address: 'manchester',
  componentRestrictions: { administrativeArea: 'CT' }
}, someCallbackFunction);

1 个答案:

答案 0 :(得分:5)

来自文档:

  

请注意,偏差仅优先于特定域的结果;如果更多   相关结果存在于该领域之外,可能包括在内。

要将结果限制为特定国家/地区,请使用Component Filtering

e.g。 https://maps.googleapis.com/maps/api/geocode/json?address=manchester&components=country:US