Menu
Close

Onclick Cluster with Callback

Sample Implementation

    
      
      <html>
        <head>
          <title>Onclick Cluster with Callback</title>
          <meta name="viewport" content="initial-scale=1.0">
          <meta charset="utf-8">
          <style>
          html,
          body,
          #map {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100vh;
          }
          </style>
             <script src="https://apis.mappls.com/advancedmaps/api/<Token>/map_sdk?layer=vector&v=3.0&callback=initMap1" defer async></script>
            </head>
        <body>
                    <div id="map"></div>
                        <script>
        var map, marker;
        function initMap1() {
            map = new mappls.Map("map", {
                center: [28.544, 77.5454],
                zoom: 9,
            });

            map.on("load", () => {
                var geoData = {
                    type: "FeatureCollection",
                    features: [
                        {
                            type: "Feature",
                            properties: {
                                htmlPopup: "Test-1",
                            },
                            geometry: {
                                type: "Point",
                                coordinates: [77.5454, 28.544],
                            },
                        },
                        {
                            type: "Feature",
                            properties: {
                                htmlPopup: "Test-2",
                            },
                            geometry: {
                                type: "Point",
                                coordinates: [77.2158203125, 28.271898],
                            },
                        },
                        {
                            type: "Feature",
                            properties: {
                                htmlPopup: "Test-3",
                            },
                            geometry: {
                                type: "Point",
                                coordinates: [77.267825, 28.54959],
                            },
                        },
                        {
                            type: "Feature",
                            properties: {
                                htmlPopup: "Test-4",
                            },
                            geometry: {
                                type: "Point",
                                coordinates: [77.545452, 28.54462],
                            },
                        },
                        {
                            type: "Feature",
                            properties: {
                                htmlPopup: "Test-5",
                            },
                            geometry: {
                                type: "Point",
                                coordinates: [77.2158203145, 28.271856],
                            },
                        },
                        {
                            type: "Feature",
                            properties: {
                                htmlPopup: "Test-5",
                            },
                            geometry: {
                                type: "Point",
                                coordinates: [77.267806, 28.5496],
                            },
                        },
                        {
                            type: "Feature",
                            properties: {
                                htmlPopup: "noida",
                            },
                            geometry: {
                                type: "Point",
                                coordinates: [77.54, 28.54],
                            },
                        },
                        {
                            type: "Feature",
                            properties: {
                                htmlPopup: "delhi",
                            },
                            geometry: {
                                type: "Point",
                                coordinates: [77.267, 28.549],
                            },
                        },
                    ],
                };

                marker = mappls.Marker({
                    map: map,
                    position: geoData,
                    fitbounds: true,
                    cType: 1,
                    clusters: true,
                });
            });

            map.on("click", "cluster_mkr0", function (e) {
                var features = map.queryRenderedFeatures(e.point, {
                    layers: ["cluster_mkr0"],
                });
                var coordinates = e.features[0].geometry.coordinates.slice();
                var clusterId = features[0].properties.cluster_id,
                    point_count = features[0].properties.point_count,
                    clusterSource = map.getSource("cluster_mkr0");

                clusterSource.getClusterLeaves(
                    clusterId,
                    point_count,
                    0,
                    function (err, aFeatures) {
                        var popupString = "";
                        var childrenCount = Object.keys(aFeatures).length;
                        aFeatures.forEach((feature) => {
                            popupString += feature.properties.htmlPopup + "<br>";
                        });
                        new mapplsgl.Popup()
                            .setLngLat(coordinates)
                            .setHTML(
                                "<h4>Total Markers in Cluster :</h4>" +
                                childrenCount +
                                "<h3>Cluster Children data</h3>" +
                                popupString
                            )
                            .addTo(map);
                    }
                );
            });
        }
    </script>
            </body>
      </html>
      
      
      
    
  
Call at Mappls
Request Call Back

Personalisation SDK, by Mappls, is India's first O2O engagement tool that can 3X your customer engagement, retention and conversion.

There's so much that Mappls MapmyIndia can do for your enterprise or individual requirements. Explore our website to learn more or request a callback/email if you'd like us to connect with you.

or