Search code examples
google-mapsgoogle-maps-api-3geometrygoogle-maps-markerszooming

How to set zoom to fit circle in Google Maps


We have distance search filter. It has a map viewport that allow to set base marker and a input text box that allows enter distance in kilometers.

We then add a circle to show this distance on the map.

How can I zoom the map so it fits the circle?


Solution

  • For multiple circles use union instead of extend:

    var bounds = new google.maps.LatLngBounds();
    
    $.each(circles, function(index, circle){
        bounds.union(circle.getBounds());
    });
    
    map.fitBounds(bounds);