How to prevent google geocoder from returning results from other countries
Asked Answered
L

5

5

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.

Lytta answered 14/11, 2013 at 22:21 Comment(4)
Note that biasing only prefers results for a specific domain; if more relevant results exist outside of this domain, they may be included.Rubino
yeah i noted that, that made me ask the question :DLytta
Well, that IS the answer.. you can't.Rubino
The answer is -> You can :DLytta
P
3

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.

Provision answered 29/1, 2015 at 16:21 Comment(3)
Not for me... "Budapest, France" locate it to the real Budapest, Hungary and componentRestrictions on FR makes even bad request (exemple: grevrsvrqnrenreoi) be GeocoderStatus.OK, located in some random place in France. I've tried both on, it's not better...Zeuxis
I had the same issue. If I include the componentRestrictions for say, the US, no matter what kind of gibberish I try to geocode, GeocoderStatus is always returned as OK. If I remove the componentRestrictions and search for the same gibberish, it returns ZERO_RESULTS.Thrall
'componentRestrictions' doesn't work anymore, should be just 'components'.Practitioner
V
13

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);
        }
});
Vallo answered 14/11, 2013 at 22:41 Comment(6)
this causes " Uncaught InvalidValueError: unknown property components "Lytta
I think i can be done I just havent figured it out yet. [link]developers.google.com/maps/documentation/javascript/…Vallo
I think i got try "componentRestrictions":{"country":"DE"}Vallo
YEAAH it works !! I just had to pass it as an object like : 'componentRestrictions':{'country':'DE'} :D if you update your answer i will accept itLytta
How about when user type outside of limit country?Togoland
'componentRestrictions' doesn't work anymore, should be just 'components' in the object.Practitioner
P
3

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.

Provision answered 29/1, 2015 at 16:21 Comment(3)
Not for me... "Budapest, France" locate it to the real Budapest, Hungary and componentRestrictions on FR makes even bad request (exemple: grevrsvrqnrenreoi) be GeocoderStatus.OK, located in some random place in France. I've tried both on, it's not better...Zeuxis
I had the same issue. If I include the componentRestrictions for say, the US, no matter what kind of gibberish I try to geocode, GeocoderStatus is always returned as OK. If I remove the componentRestrictions and search for the same gibberish, it returns ZERO_RESULTS.Thrall
'componentRestrictions' doesn't work anymore, should be just 'components'.Practitioner
R
2

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!

Rockel answered 14/11, 2013 at 22:36 Comment(2)
thank you very much that looks like a good approach! i will feedback when i tryedLytta
Good approach yes, but only for countries that got monolithic boundaries. It can't really work for countries like France which got islands on many oceans, in the Pacific and more..Zeuxis
E
1

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"} },
Elisaelisabet answered 30/6, 2015 at 9:38 Comment(4)
No, there is no change for me with thisZeuxis
What country did you use? For me this was the only way it worked.Elisaelisabet
France, it was the same effect, I think iamjpg's answer is right when he wrote "you can't"...Zeuxis
'componentRestrictions' doesn't work anymore, should be just 'components'.Practitioner
S
1

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.

  1. Without componentRestrictions — Result: Schenectady, New York 12345, USA

  2. With Deutschland added to the address — Result: 12345 High Germany Rd SE, Little Orleans, MD 21766, USA

  3. With componentRestrictions — Result: Deutschland

Update

Another solution is to check the partial_match flag:

if (result[0].partial_match) {
    return reject();
}
Spermatozoon answered 28/8, 2017 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.