Search code examples
javascriptgoogle-mapsgeolocationgpsgeocode

Google's Geocoder returns wrong country, ignoring the region hint


I'm using Google's Geocoder to find lat lng coordinates for a given address.

    var geocoder = new google.maps.Geocoder();
    geocoder.geocode(
    {
        'address':  address,
        'region':   'uk'
    }, function(results, status) {
        if(status == google.maps.GeocoderStatus.OK) {
            lat: results[0].geometry.location.lat(),
            lng: results[0].geometry.location.lng()
    });

address variable is taken from an input field.

I want to search locations only in UK. I thought that specifying 'region': 'uk' should be enough but it's not. When I type in "Boston" it's finding Boston in US and I wanted the one in UK.

How to restrict Geocoder to return locations only from one country or maybe from a certain lat lng range?

Thanks


Solution

  • UPDATE: This answer may not be the best approach anymore. See the comments below the answer for more details.


    In addition to what Pekka already suggested, you may want to concatenate ', UK' to your address, as in the following example:

    <!DOCTYPE html>
    <html> 
    <head> 
       <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
       <title>Google Maps Geocoding only in UK Demo</title> 
       <script src="http://maps.google.com/maps/api/js?sensor=false" 
               type="text/javascript"></script> 
    </head> 
    <body> 
       <div id="map" style="width: 400px; height: 300px"></div> 
    
       <script type="text/javascript"> 
    
       var mapOptions = { 
          mapTypeId: google.maps.MapTypeId.TERRAIN,
          center: new google.maps.LatLng(54.00, -3.00),
          zoom: 5
       };
    
       var map = new google.maps.Map(document.getElementById("map"), mapOptions);
       var geocoder = new google.maps.Geocoder();
    
       var address = 'Boston';
    
       geocoder.geocode({
          'address': address + ', UK'
       }, 
       function(results, status) {
          if(status == google.maps.GeocoderStatus.OK) {
             new google.maps.Marker({
                position:results[0].geometry.location,
                map: map
             });
          }
       });
    
       </script> 
    </body> 
    </html>
    

    Screenshot:

    Geocoding only in UK

    I find that this is very reliable. On the other hand, the following example shows that neither the region parameter, nor the bounds parameter, are having any effect in this case:

    <!DOCTYPE html>
    <html> 
    <head> 
       <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
       <title>Google Maps Geocoding only in UK Demo with Bounds</title> 
       <script src="http://maps.google.com/maps/api/js?sensor=false" 
               type="text/javascript"></script> 
    </head> 
    <body> 
       <div id="map" style="width: 500px; height: 300px"></div> 
    
       <script type="text/javascript"> 
    
       var mapOptions = { 
          mapTypeId: google.maps.MapTypeId.TERRAIN,
          center: new google.maps.LatLng(50.00, -33.00),
          zoom: 3
       };
    
       var map = new google.maps.Map(document.getElementById("map"), mapOptions);   
       var geocoder = new google.maps.Geocoder();
    
       // Define north-east and south-west points of UK
       var ne = new google.maps.LatLng(60.00, 3.00);
       var sw = new google.maps.LatLng(49.00, -13.00);
    
       // Define bounding box for drawing
       var boundingBoxPoints = [
          ne, new google.maps.LatLng(ne.lat(), sw.lng()),
          sw, new google.maps.LatLng(sw.lat(), ne.lng()), ne
       ];
    
       // Draw bounding box on map    
       new google.maps.Polyline({
          path: boundingBoxPoints,
          strokeColor: '#FF0000',
          strokeOpacity: 1.0,
          strokeWeight: 2,
          map: map
       });
    
       // Geocode and place marker on map
       geocoder.geocode({
          'address': 'Boston',
          'region':  'uk',
          'bounds':  new google.maps.LatLngBounds(sw, ne)
       }, 
       function(results, status) {
          if(status == google.maps.GeocoderStatus.OK) {
             new google.maps.Marker({
                position:results[0].geometry.location,
                map: map
             });
          }
       });
    
       </script> 
    </body> 
    </html>