How to disconnect a polyLine between a series of markers and start a new one in leaflet?

我怕爱的太早我们不能终老 提交于 2020-08-10 18:55:20

问题


I am using leaflet. I can add markers on my map and connecting them with polyLine method. Here is the link to my current code here. As of now, the way code is working is, you can start from point A and then to point B and then to Point C and so on. So it's a continuous chain of points which are connect with Polyline. What I want is I want to give an option to the user like on press escape or having a click option on div. I want user to break this chain and let him start over adding markers on the map keeping the earlier ones laid on the map. So this way I want user to add wherever he want's to add and disconnect either by pressing escape or by clicking on div tag and start a second series and then third and so on. Please have a look on my code.

 //Plotting the points
 map.on('click', function(e) {

  if (!_firstLatLng) {
    _firstLatLng = e.latlng;
    _firstPoint = e.layerPoint;
    L.marker(_firstLatLng).addTo(_map).bindPopup('e.latlng + '<br/>' + e.layerPoint).openPopup();
  } else {
    _secondLatLng = e.latlng;
    _secondPoint = e.layerPoint;
    L.marker(_secondLatLng).addTo(_map).bindPopup(e.latlng + '<br/>' + e.layerPoint).openPopup();
  }
 // connecting two points
  if (_firstLatLng && _secondLatLng) {
    // draw the line between points
    L.polyline([_firstLatLng, _secondLatLng], {
      color: 'blue'
    }).addTo(_map);
    _firstLatLng  = _secondLatLng
  }
})

Could you please help me with the logic here?

How can I implement it?

Any help is appreciated. I thank you.

Note: Map may not be visible due to some issues but you can click on map to pick points

来源:https://stackoverflow.com/questions/63232452/how-to-disconnect-a-polyline-between-a-series-of-markers-and-start-a-new-one-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!