我有一个问题我使用Leaflet并且每2秒有一个GPS坐标,与地图上的移动物体相关,我成功地每2秒创建一个基于gps坐标的移动标记,但后来我没能画出来跟在标记之后的折线。
你知道吗?感谢 var redraw = function(p3DMessage){
lat = parseFloat(p3DMessage.lat);
lng = parseFloat(p3DMessage.lon);
console.log(lat)
console.log(lng)
if (theMarker != undefined) {
mymap.removeLayer(theMarker);
};
var polylinePoints = [[lat, lng]];
//failed to draw polyline...
//L.polyline([[lat, lng]]).addTo(mymap);
theMarker = L.marker([lat,lng]).addTo(mymap);
};
答案 0 :(得分:4)
Polyline.addLatLng
可让您将点添加到现有折线。
因此,建立地图,添加折线并在绘图功能的每个刻度处添加一个点。请注意,您还可以重复使用标记。像
这样的东西var map = L.map(document.getElementById('map')).setView([48.8583736, 2.2922926], 15);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var marker = null;
var line = L.polyline([]).addTo(map);
function redraw (p3DMessage) {
var lat = parseFloat(p3DMessage.lat),
lng = parseFloat(p3DMessage.lon),
point = {lat: lat, lng: lng};
if (!marker) {
marker = L.marker(point).addTo(map)
}
line.addLatLng(point);
marker.setLatLng(point);
}
和演示
var map = L.map(document.getElementById('map')).setView([48.8583736, 2.2922926], 15);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var points = [
{lat: 48.857, lon:2.2922926},
{lat: 48.858, lon:2.291},
{lat: 48.859, lon:2.293},
{lat: 48.860, lon:2.294},
{lat: 48.862, lon:2.294},
];
var marker = null;
var line = L.polyline([]).addTo(map);
function redraw(point) {
if (!marker) {
marker = L.marker(point).addTo(map)
}
line.addLatLng(point);
marker.setLatLng(point);
}
function update() {
if (points.length) {
redraw(points.shift());
setTimeout(update, 1000);
}
}
setTimeout(update, 1000);
&#13;
html, body {
height: 100%;
margin: 0;
}
#map {
width: 100%;
height: 100%;
}
&#13;
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css" integrity="sha512-M2wvCLH6DSRazYeZRIm1JnYyh22purTM+FDB5CsyxtQJYeKq83arPe5wgbNmcFXGqiSH2XR8dT/fJISVA1r/zQ==" crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js" integrity="sha512-lInM/apFSqyy1o6s89K4iQUKg6ppXEgsVxT35HbzUupEVRh2Eu9Wdl4tHj7dZO0s1uvplcYGmt3498TtHq+log==" crossorigin=""></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/0.4.12/leaflet.draw.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/0.4.12/leaflet.draw.js"></script>
<div id='map'></div>
&#13;