關注公眾號"seeling_GIS",領取學習視頻資料
前序
- 之前有網友留言出,希望出一個關于點位的歷史軌跡。點位的歷史軌跡,我想的是通過 L.polyline 修改對應的點位數據 latlngs 然后通過 setLatLngs 的方式更新即可。如下代碼所示。不知道是否是該網友所需要的答案。
// 代碼示例來自 https://leafletjs.com/reference-1.6.0.html#polyline
var latlngs = [
[45.51, -122.68],
[37.77, -122.43],
[34.04, -118.2]
];
var polyline = L.polyline(latlngs, {color: 'red'}).addTo(map);
// zoom the map to the polyline
map.fitBounds(polyline.getBounds());
- 在實際工作中有對運行軌跡的呈現需求,所有就參看了一些例子,實現的方式有很多種,但是發(fā)現mapv實現的方式效果會更好,接入也很方便,所有就有了這篇文章
效果
這里是直接使用百度案例中的數據,因為經緯度數據是百度偏轉后的數據,這里沒有做糾偏,所有看圖結果是和實際路網是有一定偏移的

track.gif
引入 MapV
// 通過npm添加 mapv
npm install mapv
import * as mapv from 'mapv'
代碼實現
- 使用mapv實現該效果相對來說比較簡單,首先是創(chuàng)建數據集,然后設置樣式配制option,然后直接調用mapv分裝好的 leafletMapLayer 即可
- 圖上效果是兩個圖層疊加后的結果,一個是軌跡線圖層,一個是運動軌跡圖層
// 軌跡線 option
let option= {
strokeStyle: "rgba(53,57,255,0.5)",
shadowColor: "rgba(53,57,255,0.2)",
shadowBlur: 3,
lineWidth: 3.0,
draw: "simple",
};
// 運動軌跡 option
let aniOption = {
fillStyle: "rgba(255, 250, 250, 0.2)",
globalCompositeOperation: "lighter",
size: 1.5,
animation: {
stepsRange: {
start: 0,
end: 100,
},
trails: 3,
duration: 5,
},
draw: "simple",
};
// 初始化 dataset 數據
this.axios.get("/data/mapv/od-xierqi.txt").then((res) => {
var data = [];
var timeData = [];
let rs = res.data.split("\n");
console.log(rs.length);
var maxLength = 0;
for (var i = 0; i < rs.length; i++) {
var item = rs[i].split(",");
var coordinates = [];
if (item.length > maxLength) {
maxLength = item.length;
}
for (let j = 0; j < item.length; j += 2) {
var x = (Number(item[j]) / 20037508.34) * 180;
var y = (Number(item[j + 1]) / 20037508.34) * 180;
y =
(180 / Math.PI) *
(2 * Math.atan(Math.exp((y * Math.PI) / 180)) - Math.PI / 2);
if (x == 0 || y == NaN) {
continue;
}
coordinates.push([x, y]);
timeData.push({
geometry: {
type: "Point",
coordinates: [x, y],
},
count: 1,
time: j,
});
}
data.push({
geometry: {
type: "LineString",
coordinates: coordinates,
},
});
}
let dataset = new mapv.DataSet(data);
let timeDataset = new mapv.DataSet(timeData);
// 添加圖層到map
mapv.leafletMapLayer(dataset, option).addTo(map);
mapv.leafletMapLayer(timeDataset, aniOption).addTo(map);
});
更多內容,歡迎關注公眾號

seeling_GIS