Google Geocode region works only in certain cases

冷暖自知 提交于 2019-12-02 13:37:40

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/27277068/google-geocode-region-works-only-in-certain-cases

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!