javascript - Mapbox GL 更改标记图标并在单击另一个时再次单击并返回

标签 javascript html css mapbox-gl-js

如何在单击时更改 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

});

关于javascript - Mapbox GL 更改标记图标并在单击另一个时再次单击并返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54408928/

相关文章:

html - "text-transform: uppercase"在加载期间导致 "jumping"

javascript - Angular 指令未正确接收对象

html - 在浏览器中工作的媒体查询调整大小而不是在移动设备中

javascript - Angular 4 : abort all pending $http requests on route change

javascript - Angular 服务减法过滤器

javascript - <fieldset> 以 React 形式破坏 onChange

javascript - 可拖动更改轴上的 jQuery

css - 如何通过 Firebug (Firefox) 跟踪计算样式?

html - "div"变成 "a"是错误的。那么,我该怎么做呢? (HTML4、CSS2、跨浏览器)

javascript - Echarts - 默认 noDataLoadingOption 选项即使有数据仍然显示气泡