3

I want my search to only return hits within the map/viewport. I zoom in over Britain and search for London with the map bounds, but the response also return 3 hits in the US. How do I limit the search?

Code:

function codeAddress(){
    geocoder = new google.maps.Geocoder();
    var address = document.getElementById("inputAddress").value;
    var bounds = map.getBounds();
    geocoder.geocode({address: address, bounds: bounds}, function(results, status){
        if(status == google.maps.GeocoderStatus.OK){
            // put markers on each hit
        }
    });
}

1 Answers1

0

I'm assuming that by limiting the results you are trying to plot relevant markers right?

So instead of limiting the search directly, you can try the indirect approach. if(status == google.maps.GeocoderStatus.OK){ // put markers on each hit }

In the if loop you can use the method called contains() on the bounds of the map, like so

if(status == google.maps.GeocoderStatus.OK) {
        if(map.getBounds().contains(RESULT-LOCATION)) {
            // put markers on each hit
        }
    }

Where the RESULT-LOCATION is the location of the result that is returned from the Geocode request. This way it will plot the markers only within the maps bounds.

Hope this helps.

Saad Ulde
  • 89
  • 1
  • 7