【发布时间】:2020-04-12 03:00:16
【问题描述】:
我正在使用 Leaflet map 开发 Angular。我的项目正在获取飞行路径的数据 json 坐标。我下面的代码工作正常,但问题是我在数据更新时在地图上有重复的折线路径。我做了一些步骤来删除以前的折线路径并显示新路径,但我仍然有同样的问题。
export class MapTrackBeforPage implements OnInit {
map: Map;
poly:L.Polyline
fg = L.featureGroup()
protected points: { lat: number, lng: number }[] = [];
constructor(
private http: HTTP,
public zone : NgZone) {
}
ionViewDidEnter() {
this.getmarker()
this.leafletMap()
}
leafletMap() {
// In setView add latLng and zoom
this.map = new Map('Map_id', { attributionControl: false }).setView([33, 44], 6);
this.fg =L.featureGroup().addTo(this.map)
return tileLayer('https://{s}.tile.thunderforest.com/transport-dark/{z}/{x}/{y}.png', {
attribution: 'Baghdad Int`l Airport © 2019',
}).addTo(this.map);
}
async getmarker() {
this.zone.runTask(()=>{
setInterval(()=>{
this.http.get('xxxxxxxxxxxxxxxxxx'', {}, {})
.then(data => {
this.fg.clearLayers()
let getdata = JSON.parse(data.data)
console.log(getdata)
this.AddPath(JSON.parse(data.data))
})
},5000)
})
}
AddPath(path) {
// Flight path
for (let datas of path) {
this.points.push({ lat: datas.lat, lng: datas.lng })
console.log(this.points)
this.poly = new L.Polyline([this.points], { color: 'red', weight: 3 }).addTo(this.fg);
// zoom the map to the polyline
this.map.fitBounds(this.fg.getBounds())
}
new L.Marker(this.points[0], {
icon: new L.DivIcon({
html: ` <img src="assets/icon/B733.png" style="width:25px"/>`,
iconSize: [20, 20],
})
}).addTo(this.map)
}
}
【问题讨论】: