leaflet 绘制轨迹

// 轨迹
	var gjtc = {layers:[]};
	var londonBrusselFrankfurtAmsterdamLondon = [[51.507222, -0.1275], [50.85, 4.35],[50.116667, 8.683333], [52.366667, 4.9], [51.507222, -0.1275]];
	// 轨迹形式1
	// 初始绘制轨迹线,以一个标注从起点开始往终点移动
	function snake() {
		if(gjtc.group){
			gjtc.group.clearLayers(); 
			gjtc.layers = []
		}
		// 标记平缓运动
		gjtc.marker2 = L.Marker.movingMarker(londonBrusselFrankfurtAmsterdamLondon,
			[3000, 9000, 9000, 4000], {autostart: true});
		gjtc.line = L.polyline(londonBrusselFrankfurtAmsterdamLondon, {color: 'red'});
		// marker3.loops = 0;
		// marker3.bindPopup('', {closeOnClick: false});
		gjtc.layers.push(gjtc.marker2)
		gjtc.layers.push(gjtc.line)
		gjtc.group = L.layerGroup(gjtc.layers).addTo(map);
	}
	// 轨迹形式2
	// 初始绘制起点终点标注,轨迹线从起点往终点绘制
	function snake2(){
		if(gjtc.group){
			gjtc.group.clearLayers(); 
			gjtc.layers = []
		}
		for (var i = 0, latlngs = [], len = londonBrusselFrankfurtAmsterdamLondon.length-1; i < len; i++) {
			latlngs.push(new L.LatLng(londonBrusselFrankfurtAmsterdamLondon[i][0], londonBrusselFrankfurtAmsterdamLondon[i][1]));
		}
		map.fitBounds(L.latLngBounds(latlngs));  // 设置地图显示边界
		var start = L.marker(latlngs[0]);
		var end = L.marker(latlngs[len - 1]);
		gjtc.line2 = L.polyline(latlngs, {snakingSpeed: 200});
		gjtc.line2.addTo(map).snakeIn();
		gjtc.layers.push(start)
		gjtc.layers.push(end)
		gjtc.layers.push(gjtc.line2)
		gjtc.group = L.layerGroup(gjtc.layers).addTo(map)
	}
	// 轨迹形式3
	// 初始绘制起点标注,轨迹线从起点往终点绘制,到终点时绘制终点标注
	function snake3(){
		if(gjtc.group){
			gjtc.group.clearLayers(); 
			gjtc.layers = []
		}
		for (var i = 0, latlngs = [], len = londonBrusselFrankfurtAmsterdamLondon.length-1; i < len; i++) {
			latlngs.push(new L.LatLng(londonBrusselFrankfurtAmsterdamLondon[i][0], londonBrusselFrankfurtAmsterdamLondon[i][1]));
		}
		var route = L.featureGroup([
			L.marker(latlngs[0]),
			L.polyline(latlngs, {snakingSpeed: 200}),
			L.marker(latlngs[len - 1])
		]).addTo(map)
		map.fitBounds(L.latLngBounds(latlngs));
		route.snakeIn();  // 触发动画
		gjtc.layers.push(route)
		gjtc.group = L.layerGroup(gjtc.layers).addTo(map)
	}

在项目涉及带gps或者有gis需求部分时,不可避免的可能需要绘制轨迹这个功能,上面的代码是之前一个项目涉及轨迹回放功能时,查看文档等完成的效果

demo:(movingMarker)https://github.com/ewoken/Leaflet.MovingMarker
(Polyline.SnakeAnim)https://github.com/IvanSanchez/Leaflet.Polyline.SnakeAnim

你可能感兴趣的:(leaflet)