Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" 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.

like image 285
john Smith Avatar asked Nov 14 '13 22:11

john Smith


People also ask

How to get the most out of the Google Geocoding API?

Get the most out of the tool by learning how to use it with real-life data During a period of time, the coordinates of this sport center in the north of Madrid was erroneously returned in about 3% of all our geocoding requests to Google’s Geocoding API. Source: Google Maps

How are geocoding responses returned?

Geocoding responses Geocoding responses are returned in the format indicated by the output flag within the URL request's path. In this example, the Geocoding API requests a json response for a query on "1600 Amphitheatre Parkway, Mountain View, CA". This request demonstrates using the JSON output flag:

Is Google’s geocoding service good?

After over a year of intense usage, we have found that the Google Geocoding service provides results of excellent quality. Nevertheless, working with real-life data, there were some things that caused us quite a headache until we figured them out, which is why we would like to share with you three things to avoid when using Google’s Geocoding API:

What is the difference between geocoding and reverse geocoding?

Geocoding is the process of converting addresses (like "1600 Amphitheatre Parkway, Mountain View, CA") into geographic coordinates (like latitude 37.423021 and longitude -122.083739), which you can use to place markers on a map, or position the map. Reverse geocoding is the process of converting geographic coordinates into a human-readable address.


2 Answers

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);
        }
});
like image 65
foobar Avatar answered Sep 18 '22 17:09

foobar


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.

like image 45
Mr Sorbose Avatar answered Sep 18 '22 17:09

Mr Sorbose