最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to prevent google geocoder from returning results from other countries - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question edited Aug 29, 2017 at 8:48 john Smith asked Nov 14, 2013 at 22:21 john Smithjohn Smith 17.9k12 gold badges78 silver badges122 bronze badges 4
  • Note that biasing only prefers results for a specific domain; if more relevant results exist outside of this domain, they may be included. – putvande Commented Nov 14, 2013 at 22:33
  • yeah i noted that, that made me ask the question :D – john Smith Commented Nov 14, 2013 at 22:36
  • Well, that IS the answer.. you can't. – putvande Commented Nov 14, 2013 at 22:37
  • The answer is -> You can :D – john Smith Commented Nov 14, 2013 at 23:09
Add a comment  | 

5 Answers 5

Reset to default 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);
        }
});

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.

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!

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"} },

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();
}
发布评论

评论列表(0)

  1. 暂无评论