2
votes

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!

1
What happens when you use the bounds without the region? Perhaps they cannot be used together.Andrew Leach
Maybe this was an temporarily bug. I was trying it, and it didn't work, but now I get results.Dr.Molle
@AndrewLeach : Yeah, I have tried that, dounds without region... unfortunately, to no avail..bguiz
Here's a prime example:Dovy
@dovy did you forget to include a link in that comment?bguiz

1 Answers

1
votes

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");
                    }
                }
            }
        });
}