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

Aar*_*ron 1 google-maps geocoding google-maps-api-3

我认为这是一个错误,我将查找将其提交给Google的位置,但我认为我会先在这里提出问题,以防万一我做错了什么或误解了某些内容。我无法让Google的地址解析器始终使用区域偏差。例如,以下方法确实有效:

  • 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 仍返回英国曼彻斯特

对于无区域请求,结果都未出现在波士顿,林肯郡和康涅狄格州的曼彻斯特,因此我认为它们应该发挥相同的作用。但是,请务必注意,它可能取决于手动区域输入。以下操作无法正常工作,并且都返回马萨诸塞州波士顿:

  • 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);
Run Code Online (Sandbox Code Playgroud)

编辑2: componentRestrictions

JavaScript API确实接受一个componentRestrictions对象。您会看到文档未显示componentRestrictions在对象文字中,但确实在下面对其进行了描述(此处未提及它是对象)。上面第一次编辑中的代码可以简化为:

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

Dr.*_*lle 5

从文档中:

请注意,偏向只偏向特定领域的结果;如果在此域之外还存在其他相关结果,则可以将其包括在内。

要将结果限制为特定国家/地区,请使用组件过滤

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