问题
I'm using the google geocoder with an option to only return results from Germany
Here's the relevant part of my function
...
var geocoder = new google.maps.Geocoder();
geocoder.geocode({"address":address,"region":"DE" }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0].geometry.location) {
completeGeo(results[0],address);
} else {
completeGeo(null,address);
}
...
but if i geocode "cuvry" too find that street in germany
google returns for example "Cuvry, France" which is outside of the region parameter
How can I prevent google geocoder from returning results that are not in a certain Country? I mean return, not check in callback if country-code is matching.
回答1:
When I changed it to
geocoder.geocode({"address":address, "componentRestrictions":{"country":"DE"} },
for germany my search results found vietnam.
I have changed it to this:
geocoder.geocode( {'address':address + ', Deutschland'}, function(results, status)
This works fine with countries name in own language.
回答2:
This might work using component filters. "components":"country:DE"
var geocoder = new google.maps.Geocoder();
geocoder.geocode({"address":address, "componentRestrictions":{"country":"DE"} },
function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0].geometry.location) {
completeGeo(results[0],address);
} else {
completeGeo(null,address);
}
});
回答3:
The short answer is you can't.
The issue has been filed with Google here:
https://code.google.com/p/gmaps-api-issues/issues/detail?id=4233
The only thing you can try is passing an "accepted bounds" to the geocoder. It's far from perfect but did help a little on my project. Here is a rough copy of the code I use for attempting to limit the Google geocoder to the Western United States. Obviously you'd want to edit the bounds to represent the region you're interested in.
var geocoder = new google.maps.Geocoder()
var callGoogleGeocoder = function(str) {
var geo_bounds;
geo_bounds = setGeoBounds();
return geocoder.geocode({
address: str,
bounds: geo_bounds
}, function(results, status) {
console.log(results);
});
}
var setGeoBounds = function() {
var geo_bounds, ne, sw;
geo_bounds = new google.maps.LatLngBounds();
sw = new google.maps.LatLng(41.24843789608676, -126.5633079709794);
ne = new google.maps.LatLng(49.01224853841337, -108.3479759397294);
geo_bounds.extend(sw);
geo_bounds.extend(ne);
return geo_bounds;
};
Please take a moment to vote on the issue at Google I linked to above. For me, it's the #1 feature the Google Geocoder is missing.
Good luck!
回答4:
Use the full country name (eg. The Netherlands
instead of NL
) since the latter did not work (returned results outside NL):
geocoder.geocode({"address":address, "componentRestrictions":{"country":"The Netherlands"} },
回答5:
I've just got the same problem and solved it this way (for Germany):
var searchObject = {
'address': address,
componentRestrictions: {
country: 'DE'
}
};
geocoder().geocode(searchObject, function (result, status) {
if (status !== google.maps.GeocoderStatus.OK) {
return reject();
}
if (result[0].formatted_address === 'Deutschland' && address !== 'Deutschland') {
return reject();
}
return reject();
});
So, when you search for a wrong address, Google always return Deutschland
as address when used componentRestrictions
. That's the only way I got it working, because when you remove componentRestrictions
Google will result a guessed location, which is not correct for my use case.
Example: Search for postcode 12345, which is not valid in Germany.
Without
componentRestrictions
— Result:Schenectady, New York 12345, USA
With
Deutschland
added to the address — Result:12345 High Germany Rd SE, Little Orleans, MD 21766, USA
With
componentRestrictions
— Result:Deutschland
Update
Another solution is to check the partial_match
flag:
if (result[0].partial_match) {
return reject();
}
来源:https://stackoverflow.com/questions/19989653/how-to-prevent-google-geocoder-from-returning-results-from-other-countries