【问题标题】:How to make markers in Leaflet blinking如何使传单中的标记闪烁
【发布时间】:2017-01-26 22:23:55
【问题描述】:

有没有一种简单的方法可以让 Leaflet 地图中的标记闪烁?我的意思是动画闪烁 - 类似于在 1 秒内从不透明度 1.0 到不透明度 0.5 的过渡循环,然后反转,循环结束。

【问题讨论】:

  • 我在我的项目中使用 animate.css。可以使用闪烁效果。

标签: animation leaflet marker


【解决方案1】:

当您添加Marker 时,您可以指定Icon - 其中的选项包括className。您可以使用 className 选项通过 CSS 为标记的图标设置动画。

var map = L.map('map').setView([51.505, -0.09], 13);

L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors',
  maxZoom: 18
}).addTo(map);

L.marker([51.5, -0.09], {
  icon: L.icon({
    iconUrl: 'https://unpkg.com/leaflet@1.0.3/dist/images/marker-icon.png',
    className: 'blinking'
  })
}).addTo(map);
#map {
  bottom: 0;
  left: 0;
  position: absolute;
  right: 0;
  top: 0;
}

@keyframes fade { 
  from { opacity: 0.5; } 
}

.blinking {
  animation: fade 1s infinite alternate;
}
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>
<div id="map"></div>

要将标记从闪烁切换为不闪烁,您可以使用 Leaflet 的 DomUtilblinking 类添加到标记的 img 元素:

// With the class added, the marker will blink:
L.DomUtil.addClass(marker._icon, "blinking");

// Without the class, it won't:
L.DomUtil.removeClass(marker._icon, "blinking"); 

【讨论】:

  • 谢谢,但这不是我所需要的。我需要一个标记在某个时候开始闪烁。如何访问标记的图标并更改它的类名?我一直在互联网上搜索这个,但我无法找到答案。
  • 您可以随时通过调用setIcon来更改标记的图标,所以只要您需要在非闪烁图标和闪烁图标之间进行切换,只需调用它即可。
  • 我知道这个方法,但是我的偏好不是再次创建新的图标对象,而是访问图标并更改它的类名。如何做到这一点?
  • 我做到了,我得到了:“TypeError: t is undefined” 我的代码: var startMarker = L.marker( [startLat, startLng], { icon: startIcon } ); map.addLayer(startMarker); L.DomUtil.addClass(startMarker._icon, "markerBlinking");
  • 好的,当我移动 "L.DomUtil.addClass(startMarker._icon, "markerBlinking");"在“map.setView()”调用之后。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-03
相关资源
最近更新 更多