Methode去除在leaflet0.7.7中不起作用

时间:2016-10-03 13:03:25

标签: leaflet draw

我在leaflet0.7.7中寻找以下代码的解决方案:

var currentlyDisplayedRectangles = []; //Used in CorridorHandler

drawRoute = function(route) {
  route = new L.Polyline(L.PolylineUtil.decode(route)); // OSRM polyline decoding
  boxes = L.RouteBoxer.box(route, this.wideroad);
  var bounds = new L.LatLngBounds([]);
    if (currentlyDisplayedRectangles) {
      for (var i = 0; i < currentlyDisplayedRectangles.length; i++) {
        //currentlyDisplayedRectangles[i].remove(); //Doesn't work in leaflet0.7.7
        console.log(currentlyDisplayedRectangles[i]);
      }
    } else {
      currentlyDisplayedRectangles = [];
    }
    for (var i = 0; i < boxes.length; i++) {
      var displayedRectangle = L.rectangle(boxes[i], {color: "#ff7800", weight: 1}).addTo(this.map);
      currentlyDisplayedRectangles.push(displayedRectangle);
      bounds.extend(boxes[i]);
    }
    this.map.fitBounds(bounds);
    return route;
  }; //End drawRoute()

这在leaflet1.0.0中完美有效,但在leaflet0.7.7

中没有
currentlyDisplayedRectangles[i].remove();

这将导致函数不可用的错误。

出于某种原因,我实际上无法更新到leaflet1.0.0,因为很多其他东西不再起作用了,而且我不得不接受这个。

稍后,我愿意更新。但同时我需要一个前传版本的传单解决方案。

有人知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

在Leaflet 0.7.x中,Ilayer abstract class根本没有remove方法。

将其与Leaflet 1.0中的remove method of the Layer class进行比较。

作为替代方案,请使用removeLayer method from the Map class