How to prevent google geocoder from returning results from other countries

john Smith picture john Smith · Nov 14, 2013 · Viewed 8k times · Source

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.

Answer

foobar picture foobar · Nov 14, 2013

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