john Smith john Smith - 2 months ago 15
Javascript Question

How to prevent google geocoder from returning results from other countries

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"

google returns "Cuvry, France"

and that's definitely not in Germany.

How can I prevent google geocoder from returning results that are not in Germany?
I mean return, not check in callback if country-code is not matching.

Answer Source

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.