大家好,我是一个OpenStreetMaps的新手。我已经在地图上放置了一些带有自定义图标和弹出框的标记。现在,我真的需要知道如何在Openstreet地图上移动一个标记。我正在使用Leaflet API实现它。在letlet官方网站的文档中没有关于标记在两点之间动画的内容。请帮帮我,因为我很无助。给我一些链接、博客或其他帮助材料。
谢谢。
大家好,我是一个OpenStreetMaps的新手。我已经在地图上放置了一些带有自定义图标和弹出框的标记。现在,我真的需要知道如何在Openstreet地图上移动一个标记。我正在使用Leaflet API实现它。在letlet官方网站的文档中没有关于标记在两点之间动画的内容。请帮帮我,因为我很无助。给我一些链接、博客或其他帮助材料。
谢谢。
使用Leaflet.MovingMarker:
//MovingMarker Options
var animationMarker = L.Marker.movingMarker(
[[48.8567, 2.3508],[50.45, 30.523333]],
20000, {autostart: true});
// Custom Icon Object
var greenIcon = L.icon({
iconUrl: 'icon.png',
});
// Set icon to movingMarker
animationMarker.options.icon = greenIcon;
// Add marker to Map
map.addLayer(animationMarker );
marker.setLatLng(<LatLng> latlng)
方法。这些标记具有 HTML 元素作为图标。你可以使用 transition
方法和 CSS 样式进行样式设置。<style>
.<icon-class> {
transition: all 1s;
}
</style>
https://github.com/ewoken/Leaflet.MovingMarker
添加脚本,然后使用:var myMovingMarker = L.Marker.movingMarker([[48.8567, 2.3508],[50.45, 30.523333]], [20000]).addTo(map);
myMovingMarker.start();
http://leafletjs.com/reference.html#posanimation
如果您想要更加复杂的方法,可以看看这个插件:
<!DOCTYPE html>
<html>
<head>
<style>
#map {
height: 500px;
width: 80%;
}
</style>
<script></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.5.1/leaflet.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.5.1/leaflet.css">
<script src="https://unpkg.com/leaflet-drift-marker@1.0.3/lib/DriftMarker/Drift_Marker.js"></script>
</head>
<body>
<div id="map"></div>
</body>
<script>
// We’ll add a tile layer to add to our map, in this case it’s a OSM tile layer.
// Creating a tile layer usually involves setting the URL template for the tile images
var osmUrl = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
osmAttrib = '© <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors',
osm = L.tileLayer(osmUrl, {
maxZoom: 18,
attribution: osmAttrib
});
// initialize the map on the "map" div with a given center and zoom
var map = L.map('map').setView([19.04469, 72.9258], 1).addLayer(osm);
var marker = new Drift_Marker([19.04469, 72.9258], {
draggable: true,
title: "Resource location",
alt: "Resource Location",
riseOnHover: true
}).addTo(map)
.bindPopup("test").openPopup();
// Script for adding marker on map click
function onMapClick(e) {
marker.slideTo( e.latlng, {
duration: 2000
});
}
map.on('click', onMapClick);
marker.slideTo( [20, 20], {
duration: 2000
});
</script>
</html>
(点击地图,标记将滑动到其新位置)
marker.setLatLng()
方法设置标记的位置。您可能需要自己实现动画效果... - tyrMoveMarker(起始点,结束点,速度)
这样的方法!! - Azeem