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=bostonreturns Boston, MAhttps://maps.googleapis.com/maps/api/geocode/json?address=boston®ion=ukreturns Boston, Lincolnshire in England
However the following does not work
https://maps.googleapis.com/maps/api/geocode/json?address=manchesterreturns Manchester, UKhttps://maps.googleapis.com/maps/api/geocode/json?address=manchester®ion=usstill 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®ion=cashould be Boston, Ontariohttps://maps.googleapis.com/maps/api/geocode/json?address=boston®ion=us-kyshould 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
来源:https://stackoverflow.com/questions/27277068/google-geocode-region-works-only-in-certain-cases