Embed Google Maps with Multiple Markers and InfoWindows / Info Popups Open on Click or Mouse Hover

Google Maps provide in-depth information each and every street on earth, it proves a good way to show your location or multiple locations on Google Map and not only this, but we can also embed this Map with Multiple Markers and Info Windows to show Information on Click or Hover.

In this post, we will discuss How to Embed Google Javascript Maps Anywhere with Multiple Locations with InfoWindows or Info Windows.

Step 1) Place following HTML Code

<h3>My Google Maps Demo</h3>
<!--The div element for the map -->
<div id="map"></div>

To set Width and Height of Map Container add below CSS style

#map {
height: 400px;
/* The height is 400 pixels */
width: 100%;
/* The width is the width of the web page */
}

Step 2) Include Google Maps JS API in HTML

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>

Note: Now to use Google Maps API we must have API Key which needs to be added in API URL above. See How to GET API Key?

 

Step 3) Add following JS script Code in HTML

                var map;
                var InforObj = [];
                var centerCords = {
                    lat: -25.344,
                    lng: 131.036
                };
                var markersOnMap = [{
                        placeName: "Australia (Uluru)",
                        LatLng: [{
                            lat: -25.344,
                            lng: 131.036
                        }]
                    },
                    {
                        placeName: "Australia (Melbourne)",
                        LatLng: [{
                            lat: -37.852086,
                            lng: 504.985963
                        }]
                    },
                    {
                        placeName: "Australia (Canberra)",
                        LatLng: [{
                            lat: -35.299085,
                            lng: 509.109615
                        }]
                    },
                    {
                        placeName: "Australia (Gold Coast)",
                        LatLng: [{
                            lat: -28.013044,
                            lng: 513.425586
                        }]
                    },
                    {
                        placeName: "Australia (Perth)",
                        LatLng: [{
                            lat: -31.951994,
                            lng: 475.858081
                        }]
                    }
                ];
        
                window.onload = function () {
                    initMap();
                };
        
                function addMarker() {
                    for (var i = 0; i < markersOnMap.length; i++) {
                        var contentString = '<div id="content"><h1>' + markersOnMap[i].placeName +
                            '</h1><p>Lorem ipsum dolor sit amet, vix mutat posse suscipit id, vel ea tantas omittam detraxit.</p></div>';
        
                        const marker = new google.maps.Marker({
                            position: markersOnMap[i].LatLng[0],
                            map: map
                        });
        
                        const infowindow = new google.maps.InfoWindow({
                            content: contentString,
                            maxWidth: 200
                        });
        
                        marker.addListener('click', function () {
                            closeOtherInfo();
                            infowindow.open(marker.get('map'), marker);
                            InforObj[0] = infowindow;
                        });
                        // marker.addListener('mouseover', function () {
                        //     closeOtherInfo();
                        //     infowindow.open(marker.get('map'), marker);
                        //     InforObj[0] = infowindow;
                        // });
                        // marker.addListener('mouseout', function () {
                        //     closeOtherInfo();
                        //     infowindow.close();
                        //     InforObj[0] = infowindow;
                        // });
                    }
                }
        
                function closeOtherInfo() {
                    if (InforObj.length > 0) {
                        /* detach the info-window from the marker ... undocumented in the API docs */
                        InforObj[0].set("marker", null);
                        /* and close it */
                        InforObj[0].close();
                        /* blank the array */
                        InforObj.length = 0;
                    }
                }
        
                function initMap() {
                    map = new google.maps.Map(document.getElementById('map'), {
                        zoom: 4,
                        center: centerCords
                    });
                    addMarker();
                }
centerCords: will lat Lattitude & Longitude object to show the center of the map on load.
InforObj: will have InfoWindow object that will be used to close other info’s on click or hover.
markersOnMap: Data object having Lat Long and InfoWindow content in HTML.
initMap() : Method to load map on onLoad method
addMarkerInfo() : Add Markers and Infowindow in for loop on markersOnMap
closeOtherInfo() : Keeps track of clicked infoWindow then close others to keep only one open at a time.
Currently, we have Click even to open InfoWindow popup but there is also commented code to show on Hover.
Complete Code with JS and CSS will look like below:
<!DOCTYPE html>
<html>

<head>
    <style>
        /*  <span class="metadata-marker" style="display: none;" data-region_tag="css"></span>       Set the size of the div element that contains the map */
        #map {
            height: 400px;
            /* The height is 400 pixels */
            width: 100%;
            /* The width is the width of the web page */
        }
    </style>
    <script>
        var map;
        var InforObj = [];
        var centerCords = {
            lat: -25.344,
            lng: 131.036
        };
        var markersOnMap = [{
                placeName: "Australia (Uluru)",
                LatLng: [{
                    lat: -25.344,
                    lng: 131.036
                }]
            },
            {
                placeName: "Australia (Melbourne)",
                LatLng: [{
                    lat: -37.852086,
                    lng: 504.985963
                }]
            },
            {
                placeName: "Australia (Canberra)",
                LatLng: [{
                    lat: -35.299085,
                    lng: 509.109615
                }]
            },
            {
                placeName: "Australia (Gold Coast)",
                LatLng: [{
                    lat: -28.013044,
                    lng: 513.425586
                }]
            },
            {
                placeName: "Australia (Perth)",
                LatLng: [{
                    lat: -31.951994,
                    lng: 475.858081
                }]
            }
        ];

        window.onload = function () {
            initMap();
        };

        function addMarkerInfo() {
            for (var i = 0; i < markersOnMap.length; i++) {
                var contentString = '<div id="content"><h1>' + markersOnMap[i].placeName +
                    '</h1><p>Lorem ipsum dolor sit amet, vix mutat posse suscipit id, vel ea tantas omittam detraxit.</p></div>';

                const marker = new google.maps.Marker({
                    position: markersOnMap[i].LatLng[0],
                    map: map
                });

                const infowindow = new google.maps.InfoWindow({
                    content: contentString,
                    maxWidth: 200
                });

                marker.addListener('click', function () {
                    closeOtherInfo();
                    infowindow.open(marker.get('map'), marker);
                    InforObj[0] = infowindow;
                });
                // marker.addListener('mouseover', function () {
                //     closeOtherInfo();
                //     infowindow.open(marker.get('map'), marker);
                //     InforObj[0] = infowindow;
                // });
                // marker.addListener('mouseout', function () {
                //     closeOtherInfo();
                //     infowindow.close();
                //     InforObj[0] = infowindow;
                // });
            }
        }

        function closeOtherInfo() {
            if (InforObj.length > 0) {
                /* detach the info-window from the marker ... undocumented in the API docs */
                InforObj[0].set("marker", null);
                /* and close it */
                InforObj[0].close();
                /* blank the array */
                InforObj.length = 0;
            }
        }

        function initMap() {
            map = new google.maps.Map(document.getElementById('map'), {
                zoom: 4,
                center: centerCords
            });
            addMarkerInfo();
        }
    </script>
</head>

<body>
    <h3>My Google Maps Demo</h3>
    <!--The div element for the map -->
    <div id="map"></div>

    <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>

</body>

</html>

See working demo link

6 thoughts on “Embed Google Maps with Multiple Markers and InfoWindows / Info Popups Open on Click or Mouse Hover”

  1. Very useful googlemaps tutorial for adding markers, I was just wondering if there is a way of adding markerclusters to this script?

    // Add a marker clusterer to manage the markers.
    var markerCluster = new MarkerClusterer(map, markers, { imagePath: ‘https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m’ });

  2. Very clean and useful googlemaps tutorial containing all the nice basics – thank you for sharing!! Cheers to down under 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *