Menu
Close

Onclick Marker Update

Sample Implementation

    
      
      <html>
        <head>
          <title>Onclick Marker Update</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;
          }
                              
          .mrk {
            position: absolute;
            top: 50px;
            width: 60px;
            background-color: #fff;
            text-align: center;
            left: -13px;
            font-weight: 600;
            border-radius: 8px;
          }      
          </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>
              <button id="btn" style="position: absolute; top: 10px; left: 10px; padding: 5px;">current location update</button>
                    
    <script>
        var map, allMarker = [];

        function initMap1() {
            map = new mappls.Map('map', {
                center: [28.61, 77.23],
                zoomControl: true,
                location: true
            });
            map.setMinZoom(5);
            var data = {
                type: "FeatureCollection",
                features: [{
                        type: "Feature",
                        properties: {
                            htmlPopup: "Set - 1 .1",
                        },
                        geometry: {
                            type: "Point",
                            coordinates: [28.544, 77.5454],
                        },
                    },
                    {
                        type: "Feature",
                        properties: {
                            htmlPopup: "Set - 1 .2",
                        },
                        geometry: {
                            type: "Point",
                            coordinates: [28.27189158, 77.2158203125],
                        },
                    },
                    {
                        type: "Feature",
                        properties: {
                            htmlPopup: "Set - 1 .3",
                        },
                        geometry: {
                            type: "Point",
                            coordinates: [28.549511, 77.267825],
                        },
                    },
                ],
            };

            map.on("load", function() {
                multipleMarker(data, 'https://apis.mapmyindia.com/map_v3/1.png');
            });
        }

        document.getElementById('btn').onclick = function() {
            var updatedGeoData = {
                type: "FeatureCollection",
                features: [{
                        type: "Feature",
                        properties: {
                            htmlPopup: "Set - 2 .1",
                        },
                        geometry: {
                            type: "Point",
                            coordinates: [28.0544, 77.05454],
                        },
                    },
                    {
                        type: "Feature",
                        properties: {
                            htmlPopup: "Set - 2 .2",
                        },
                        geometry: {
                            type: "Point",
                            coordinates: [28.9158, 77.03125],
                        },
                    },
                    {
                        type: "Feature",
                        properties: {
                            htmlPopup: "Set - 2 .3",
                        },
                        geometry: {
                            type: "Point",
                            coordinates: [28.511, 77.825],
                        },
                    },
                ],
            };

            multipleMarker(updatedGeoData, 'https://apis.mapmyindia.com/map_v3/2.png');
        };

        function multipleMarker(data) {
            allMarker.forEach(markerObjerct => {
                mappls.remove({
                    map: map,
                    layer: markerObjerct
                });
            })
            data.features.forEach(item => {
                const mark = new mappls.Marker({
                    map: map,
                    position: {
                        "lat": item.geometry.coordinates[0],
                        "lng": item.geometry.coordinates[1]
                    },
                    icon_url: 'https://apis.mapmyindia.com/map_v3/1.png',
                    html: `<div class="mrk">${item.properties.htmlPopup}</div>`,
                });
                mark.addListener('click', function() {
                    console.log(mark.getPosition());
                });
                allMarker.push(mark);

            });
            setTimeout(() => {
                mappls.fitBounds({
                    map: map,
                    layers: allMarker
                });
            }, 500);

        }
    </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