Set automatic Zoom to fit all markers on Map using JavaScript in Google Maps API V3

on May 21, 2021 11:02 PM

Is it posible to automaticaly make zoom wich will be better to show all markers.

Thank you very much.

Bind (Populate) Google Map Marker from Address using C# and VB.Net in ASP.Net




Download FREE API for Word, Excel and PDF in ASP.Net: Download
on May 22, 2021 12:05 AM
on May 22, 2021 12:06 AM

Hi alex0230,

Google Maps API provides a LatLngBounds object to which you can add multiple LatLng objects. Then you can pass this to fitBounds() function to set the automatic zoom.


<script type="text/javascript" src=""></script>
<script type="text/javascript">
    var markers = [
        <asp: Repeater ID="rptMarkers" runat="server">
                "title": '<%# Eval("Name") %>',
                "lat": '<%# Eval("Latitude") %>',
                "lng": '<%# Eval("Longitude") %>',
                "description": '<%# Eval("Description") %>'
        </asp: Repeater >
<script type="text/javascript"> 
    window.onload = function () {
        var infoWindow = new google.maps.InfoWindow();
        var map = new google.maps.Map(document.getElementById("dvMap"), {
            center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
            mapTypeId: google.maps.MapTypeId.ROADMAP

        //Create LatLngBounds object.
        var latlngbounds = new google.maps.LatLngBounds();
        for (var i = 0; i < markers.length; i++) {
            var data = markers[i]
            var myLatlng = new google.maps.LatLng(, data.lng);
            var marker = new google.maps.Marker({
                position: myLatlng,
                map: map,
                title: data.title
            (function (marker, data) {
                google.maps.event.addListener(marker, "click", function (e) {
                    infoWindow.setContent("<div style = 'width:200px;min-height:40px'>" + data.description + "</div>");
          , marker);
            })(marker, data);

<div id="dvMap" style="width: 500px; height: 500px">

For more details refer below article.

Google Maps API V3: Center and Zoom to fit all markers on Google Maps