Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

openstreetmap - How to remove or empty the leaflet map based on the condition

I want to remove the map based on the selected condition.

.component.ts

dataChanged (selectsensor) {
console.log(selectsensor)
for( var j=0;j<selectsensor.length;j++){


  for (var i =0; i< this.sensors.length;i++){

    if(this.sensors[i].name == selectsensor[selectsensor.length - 1]){


      if(this.sensorsarray.filter(sense=>sense.name == selectsensor[selectsensor.length - 1]).length > 0){
      console.log(this.sensorsarray.filter(sense=>sense.name == selectsensor[selectsensor.length - 1]).length);
      for(var t=0;t<this.sensorsarray.length;t++){
        if(this.sensorsarray[t].name == selectsensor[selectsensor.length - 1]){
          this.sensorsarray.splice(t);
          
        }
      }
      console.log(this.sensorsarray);
      
        alert("Value exists!")
        
    }else{
      this.sensorsarray.push(this.sensors[i]);
      var v= "map"+ (this.sensorsarray.length - 1 )+"";
      map[i] = L.map(v).setView([this.sensors[i].latitude,this.sensors[i].longitude], 11);
        
      L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
          attribution: ' <span style="font-size:17px">'+this.sensors[i].name+' </span>'
        }).addTo(map[i]);
        var latlng = L.latLng(this.sensors[i].latitude, this.sensors[i].longitude)
  
    
        marker = L.marker(latlng, { icon: sensoricon }).addTo(map[i]);
        circlemark[i] = L.circle(latlng, { radius: 5000, weight: 0 }).addTo(map[i]);


    }
    }

I want to remove the map when the condition is this.sensorsarray.filter(sense=>sense.name == selectsensor[selectsensor.length - 1]).length > 0)

question from:https://stackoverflow.com/questions/65938077/how-to-remove-or-empty-the-leaflet-map-based-on-the-condition

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The map can removed with:

map.remove()

So then it should look like for you:

this.sensorsarray.filter(sense=>sense.name == selectsensor[selectsensor.length - 1]).length > 0){
   map[i].remove()
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...