In this article I will explain how to center and zoom Google Map on markers in such a way that all markers can be fitted using Google Maps API V3.
The map is centered and the zoom is adjusted based on the markers and their positions and the best possible view of Google Map is shown to the user.
Center and Zoom to fit all markers on Google Maps
The following code snippet consists of an array of markers of different geographic address locations. Each marker in the array contains title, latitude, longitude and description of the location.
Inside the window.onload event handler, the LoadMap function is executed.
A loop is executed over the array of markers and one by one each marker is populated on the Google Map.
Inside the loop each marker’s position is added to the LatLngBounds object of Google Maps. Once all the markers are added to the Google Map, the LatLngBounds object is used to determine the center and adjust zoom in such a way that all the markers are visible in best possible zoom of the Google Map.
Note: Using this technique you don’t need to rely on the Zoom property of Google Maps as based on the height and width of the Google Map the best possible zoom which displays all markers will be shown.
Finally the Google Map is displayed using the HTML DIV element.
<script type="text/javascript" src=""></script>
<script type="text/javascript">
    var markers = [
        "title": 'Aksa Beach',
        "lat": '19.1759668',
        "lng": '72.79504659999998',
        "description": 'Aksa Beach is a popular beach and a vacation spot in Aksa village at Malad, Mumbai.'
        "title": 'Juhu Beach',
        "lat": '19.0883595',
        "lng": '72.82652380000002',
        "description": 'Juhu Beach is one of favourite tourist attractions situated in Mumbai.'
        "title": 'Girgaum Beach',
        "lat": '18.9542149',
        "lng": '72.81203529999993',
        "description": 'Girgaum Beach commonly known as just Chaupati is one of the most famous public beaches in Mumbai.'
        "title": 'Jijamata Udyan',
        "lat": '18.979006',
        "lng": '72.83388300000001',
        "description": 'Jijamata Udyan is situated near Byculla station is famous as Mumbai (Bombay) Zoo.'
        "title": 'Sanjay Gandhi National Park',
        "lat": '19.2147067',
        "lng": '72.91062020000004',
        "description": 'Sanjay Gandhi National Park is a large protected area in the northern part of Mumbai city.'
    window.onload = function () {
    function LoadMap() {
        var mapOptions = {
            center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
            // zoom: 8, //Not required.
            mapTypeId: google.maps.MapTypeId.ROADMAP
        var infoWindow = new google.maps.InfoWindow();
        var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
        //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);
            //Extend each marker's position in LatLngBounds object.
        //Get the boundaries of the Map.
        var bounds = new google.maps.LatLngBounds();
        //Center map and adjust Zoom based on the position of all markers.
<div id="dvMap" style="width: 300px; height: 400px">
Google Maps API V3: Center and Zoom to fit all markers on Google Maps
Download Code