Google Maps - Search by Borders / Viewport

I want my search to only return images in the map / viewport. I am approaching UK and looking for London with maps but the answer also returns 3 hits in the US. How do I limit my search?


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



source to share

1 answer

I am guessing that by limiting the results, you are trying to render the appropriate markers correctly?

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

In the if loop, you can use a method named contains () on the map boundaries, like

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


Where RESULT-LOCATION is the location of the result returned from the geocoding request. This way it will only display markers within the boundaries of the maps.

Hope it helps.