地图框 GL 更改标记图标,然后单击并在单击另一个标记时再次单击并返回



如何在单击时更改mapbox gl js标记图标,然后在单击新图标时将其更改回来?我可以弄清楚如何通过为其分配另一个类来更改单击时的标记,但是当单击新标记时,您将如何再次将其更改回来?我想知道是否有可能。谢谢。

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8' />
    <title></title>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
    <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.52.0/mapbox-gl.js'></script>
    <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.52.0/mapbox-gl.css' rel='stylesheet' />
    <style>
        body { margin:0; padding:0; }
        #map { position:absolute; top:0; bottom:0; width:100%; }
        .marker {
            background-image: url('mapbox-icon.png');
            background-size: cover;
            width: 50px;
            height: 50px;
            border-radius: 50%;
            cursor: pointer;
        }
        .mapboxgl-popup {
            max-width: 200px;
        }
        .mapboxgl-popup-content {
            text-align: center;
            font-family: 'Open Sans', sans-serif;
        }
    </style>
</head>
<body>
<div id='map'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoiZXhhbXBsZXMiLCJhIjoiY2lqbmpqazdlMDBsdnRva284cWd3bm11byJ9.V6Hg2oYJwMAxeoR9GEzkAA';
var geojson = {
    "type": "FeatureCollection",
    "features": [{
         "type": "Feature",
         "geometry": {
             "type": "Point",
             "coordinates": [-77.032, 38.913]
         },
         "properties": {
             "title": "Mapbox",
             "description": "Washington, D.C."
         }
     },
     {
         "type": "Feature",
         "geometry": {
         "type": "Point",
         "coordinates": [-122.414, 37.776]
        },
         "properties": {
             "title": "Mapbox",
             "description": "San Francisco, California"
         }
     }]
};
var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/light-v10',
    center: [-96, 37.8],
    zoom: 3
});
// add markers to map
geojson.features.forEach(function(marker) {
    // create a HTML element for each feature
    var el = document.createElement('div');
    el.className = 'marker';
    // make a marker for each feature and add it to the map
    new mapboxgl.Marker(el)
        .setLngLat(marker.geometry.coordinates)
        .setPopup(new mapboxgl.Popup({offset: 25}) // add popups
            .setHTML('<h3>' + marker.properties.title + '</h3><p>' + marker.properties.description + '</p>'))
        .addTo(map);
    el.addEventListener('click', function(e){                   
              // change the marker color, then change it back again. 
              // I can set for example el.className = 'marker2'
              // but how can I change it back to the original
   });
});
</script>
</body>
</html>
因此

,您将marker2类设置为单击的标记。在执行此操作之前,您可以在任何具有该类的元素上删除该类。像这样:

el.addEventListener('click', function(e){
  // get all the elements with class "marker2"
  var x = document.getElementsByClassName("marker2");
  var i;
  for (i = 0; i < x.length; i++) {
    x[i].className = "marker"; // set "marker" as the class for each of those elements
  }
  // at this point all markers are back to the original state
  // now you set the class of the current clicked marker
  this.className = 'marker2'; //don't use the variable "el", it's out of the scope and can change, "this" is the current clicked element

});

相关内容

最新更新