leaflet - clear overlay layer and put new polyline and markers on same layer -
i've been pulling hair out day trying figure 1 out. i'm writing thought simple leaflet app track aircraft movement historical gps plot data. idea able pick period of time display, display aircraft movement on map start , endpoints , polyline showing path aircraft took. want polyline own layer can turn on , off.
first time through, works great. different polylines each flights tracked wanted. however, when want pick different aircraft on same or different days, can't clear off old ploylines , display new ones. i've tried deleting objects out of layergroup, clear layergroup , else i've found try end result can clear out old polylines, can never new ones display.
here code creates map , layers:
var basemaps = { "default": maplayer, "faa": faalayer, "satellite": satlayer, "hybrid": hybridlayer }; var polylayer = l.layergroup([]); var overlaymaps = { "track": polylayer }; var map = l.map('leafletmap', { center: [33.559768, -117.728416], zoom: 8, layers: [maplayer, polylayer ] }); var layercontrols = { "map": maplayer, "track": polylayer }; l.map.include({ 'clearlayers': function() { this.eachlayer(function(layer) { if (!layer._tiles) { this.removelayer(layer); } }, this); } }); l.control.layers(basemaps, overlaymaps ).addto(map); new controller(map,polylayer).init();
for real time tracking, position updates every 2 minutes, move icon on map previous position new position, , plot polyline old lat/long new lat/long. keeping array of plotted positions can remove plane icon previous position , move new one. here code plots position updates:
/** * if first tracking record, add array. * if not, need remove marker previous position, add * polyline previous position current position, set * marker @ new position. */ if ( trackingrecordlen === 0 ) { // add new marker array , update length trackingrecord.push( startmark ); trackingrecordlen++; //markerlayer.push( mark ); //markerlayer.addlayer(mark); markerlayer.push( startmark ); l.layergroup(markerlayer).addto(map); } else { // first, remove marker @ previous position if not first 1 if (trackingrecordlen > 1 ) map.removelayer( trackingrecord[ trackingrecordlen-1 ] ); // add new marker array trackingrecord.push( cessnamark ); trackingrecordlen++; trackinglayergroup.push( cessnamark ); markerlayer.push( cessnamark ); //l.layergroup(layergroup).addto(map); // add polyline last position new position var prevlatlng = l.latlng( trackingrecord[ trackingrecordlen-2 ].getlatlng() ); var newlatlng = l.latlng ( trackingrecord[ trackingrecordlen-1 ].getlatlng() ); var polyline = l.polyline([prevlatlng, newlatlng], {color: linecolor }).addto(polylayer); }
when want track new group of aircraft data, following clear out old polylines:
polylayer.clearlayers();
works clear out old polylines, when code above executed, no new polylines plotted. doing wrong?
thanks help.
Comments
Post a Comment