2

I am trying to restrict the returned addresses from Google Maps geocoder API like so:

var geocoder = new google.maps.Geocoder();
var auVicSwLatLon = new google.maps.LatLng(-39.234713, 140.962526);
var auVicNeLatLon = new google.maps.LatLng(-33.981125, 149.975296);
var auVicLatLonBounds = new google.maps.LatLngBounds(auVicSwLatLon, auVicNeLatLon);
geocoder.geocode(
  {
    address: searchStr,
    region: 'AU',
    // bounds: auVicLatLonBounds,
  },
  function(results, status) {
    // ... do stuff here
  }
);

Restriction using region works. However restriction using bounds does no - when I uncomment the bounds attribute above, I get no results. Leave it commented, I get results from all over Australia.

Is there something I have done wrong here?

Thanks!


Additional info:

Relevant documentation here:

https://developers.google.com/maps/documentation/javascript/geocoding

and here:

https://developers.google.com/maps/documentation/javascript/reference#Geocoder

Note that the values I have used in LatLngBounds are for that of the state of Victoria (VIC). That is really what I am trying to achieve here. Thus if you know of an alternative way to accomplish this, please also answer this question!

bguiz
  • 27,371
  • 47
  • 154
  • 243
  • What happens when you use the `bounds` *without* the `region`? Perhaps they cannot be used together. – Andrew Leach Aug 08 '12 at 08:44
  • Maybe this was an temporarily bug. I was trying it, and it didn't work, but now I get results. – Dr.Molle Aug 08 '12 at 08:49
  • @AndrewLeach : Yeah, I have tried that, dounds without region... unfortunately, to no avail.. – bguiz Aug 08 '12 at 10:45
  • Here's a prime example: – Dovy Mar 11 '14 at 17:53
  • @dovy did you forget to include a link in that comment? – bguiz Mar 11 '14 at 23:44
  • Drat, so it was all removed apparently. But yes, example: http://maps.googleapis.com/maps/api/geocode/json?address=calvert&sensor=false&bounds=40.002355%2C-102.052002%7C40.350482%2C-101.323377&components=administrative_area:NE|country:US – Dovy Mar 13 '14 at 18:15

1 Answers1

1

Why don't you manually look for the results that satisfy the bounding requirements?

I iterate through the results and check the first one of them that is within a restricted area using lat and lng values. You could also get all the results that fall inside.

function searchFromAddress() {
    var address = document.getElementById("txtBxAddress").value;
    // Check if input is not empty
    if (address.length < 1) {
        return;
    }
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'address': address },
        function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var point;

                // Find first location inside restricted area
                for (var i = 0 ; i < results.length ; i++) {
                    point = results[i].geometry.location;
                    // I compare my lng values this way because their are negative
                    if (point.lat() > latMin && point.lat() < latMax && point.lng() < lngMin && point.lng() > lngMax) {
                        map.setCenter(point);
                        var marker = new google.maps.Marker({
                            position: point,
                            map: map,
                            title: "You are here",
                            icon: home_pin
                        });
                        break;
                    }
                    // No results inside our area
                    if (i == (results.length - 1)) {
                        alert("Try again");
                    }
                }
            }
        });
}
Simon MᶜKenzie
  • 8,344
  • 13
  • 50
  • 77
Arthur G
  • 11
  • 2