我正在使用 ngx-leaflet,在向地图添加标记时,由于大量的函数调用(zone.js),应用程序变慢了。我尝试使用 changeDetection 和 ngZone 但无济于事。请帮忙 :)
constructor(zone: NgZone) {}
onMapReady(map: L.Map) {
this.map = map;
this.zone.run(() => {
this.getPoints();
});
L.control.scale({position: 'bottomleft'}).addTo(this.map);
L.control.zoom({position: 'bottomleft'}).addTo(this.map);
this.createLegend();
}
private updateLayers(pointList: Point[]) {
this.layers = [];
let group = L.featureGroup();
for (let point of pointList) {
if (point.gps) {
this.zone.run( ()=> {
let marker: Marker = L.marker([point.gps.latitude, point.gps.longitude], {icon: this.setIcon(point.status)});
group.addLayer(marker);
this.setPopupContent(marker, point);
this.layers.push(marker);
this.layers = this.layers.slice();
this.changeDetector.detectChanges();
});
}
}
if (pointList.length != 0) {
this.zone.run(()=> {
this.leafletDirective.getMap().fitBounds(group.getBounds(), {
padding: [10, 10],
animate: false,
duration: 0.01,
noMoveStart: true});
});
}
}