谷歌地理编码地区只适用于某些情况 [英] Google Geocode region works only in certain cases

查看:190
本文介绍了谷歌地理编码地区只适用于某些情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是一个错误,我会找出将它提交给谷歌的地方,但我想我会先在这里问,以防万一我做错了什么或误解了什么。我无法让Google的地理编码器始终如一地使用地区偏见。例如,以下方法可行:


  • https://maps.googleapis.com/maps/api/geocode / json?address = boston 返回Boston,MA

  • 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&地区=我们仍然返回英国曼彻斯特



波士顿,林肯郡和曼彻斯特都没有出现在对无区域请求的结果,所以我相信他们应该工作一样。但是,需要注意的是,它可能依赖于手动区域条目。以下行为并不像预期的那样有效,并且都返回马萨诸塞州的波士顿:


  • 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 应该是Boston,KY



编辑:JS API



@ dr-molle接受的答案是正确的。但是,您使用的是Google的JS API,您不能指定组件。你可以在 bounds 中指定一个边界区域。要找到你的边界,你需要一个矩形框的NE和SW点的 LatLng 对象。对于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 对象。您会看到文档不显示 componentRestrictions ,但它在下面描述它(它没有提及它是一个对象)。上面第一次编辑的代码可以简化为:

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


解决方案

b


请注意,偏好设定只偏好特定网域的结果;如果在此域以外存在更多
的相关结果,则可能包含这些结果。


将结果限制在特定国家/地区使用组件过滤



例如 https://地图.googleapis.com / maps / api / geocode / json?address = manchester& components = country:US

I think this is a bug and I'm going to find out where to submit it to Google but I figured I'd ask here first in case I'm just doing something wrong or misinterpreting something. I cannot get Google's geocoder to consistently use the region bias. For example the following does work:

  • https://maps.googleapis.com/maps/api/geocode/json?address=boston returns Boston, MA
  • https://maps.googleapis.com/maps/api/geocode/json?address=boston&region=uk returns Boston, Lincolnshire in England

However the following does not work

  • https://maps.googleapis.com/maps/api/geocode/json?address=manchester returns Manchester, UK
  • https://maps.googleapis.com/maps/api/geocode/json?address=manchester&region=us still returns Manchester, UK

Both Boston, Lincolnshire and Manchester, CT do not appear in the results for the regionless requests so I believe they should work the same. However, it's important to note that it may be dependent on manual region entries. The follow do not work as expected and both return Boston, MA:

  • https://maps.googleapis.com/maps/api/geocode/json?address=boston&region=ca should be Boston, Ontario
  • https://maps.googleapis.com/maps/api/geocode/json?address=boston&region=us-ky should be Boston, KY

EDIT: JS API

The accepted answer from @dr-molle is correct. However, you're using Google's JS API, you cannot specify components. You can specify a bounding region in bounds though. To find your bounds you need LatLng objects for the NE And SW points of a rectangular box. For CT I did the following:

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);

EDIT 2: componentRestrictions

The JavaScript API does accept a componentRestrictions object. You'll see that the docs do not show componentRestrictions in the object literal but it does describe it below (where it doesn't mention that it is an object). The code in the first edit above can be simplified to:

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

解决方案

From the documentation:

Note that biasing only prefers results for a specific domain; if more relevant results exist outside of this domain, they may be included.

To restrict the results to a specific country use Component Filtering

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

这篇关于谷歌地理编码地区只适用于某些情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆