jeudi 18 mars 2021

i can't remove polyline in leaflet map

I'am currently using leaflet map and drawing a 2 polylines what i want to do is that to be able to display only one of them at the time, my function for drawing the polylines looks like this

```
function Affichage(message,id) {
   var polyline=0;
   var polyline1=0;
      //console.log(message);
  //var marker = L.marker([message.GPS_lat, message.GPS_lon],{markerColor:'#583470'}).addTo(map);
//console.log(latlngs);
//polyline = L.polyline(message,{color: '#3498db', noClip:false, smoothFactor:0.5}).addTo(map);
switch (id) {
 case '0':

              polyline = L.polyline(message,{color: '#3498db', noClip:false, smoothFactor:0.5});
                 
            


   break;
  case '1':
              polyline1 = L.polyline(message,{color: '#00FFFF', noClip:false, smoothFactor:0.5});
             
              
  break;
 default:

  }
```

until now that function displays the polylines but when i changed it to

  switch (id) {
 case '0':

              polyline = L.polyline(message,{color: '#3498db', noClip:false, smoothFactor:0.5});
              map.removeLayer(polyline1); // to remove the second polyline



   break;
  case '1':
              polyline1 = L.polyline(message,{color: '#00FFFF', noClip:false, smoothFactor:0.5});
              map.removeLayer(polyline); // to remove the first polyline 

  break;
 default:

  }

it gives me an error " Uncaught TypeError: can't assign to property "_leaflet_id" on 0: not an object u https://unpkg.com/leaflet@1.6.0/dist/leaflet.js:5 removeLayer https://unpkg.com/leaflet@1.6.0/dist/leaflet.js:5"




Aucun commentaire:

Enregistrer un commentaire