【问题标题】:How to remove L.rectangle(boxes[i])如何删除 L.rectangle(boxes[i])
【发布时间】:2023-03-11 13:06:01
【问题描述】:

几天前,我实现了一个 routingControl = L.Routing.control({...}),它非常适合我的需求。但是,我的一位客户还需要我也能够实现它的 RouteBoxer。现在按照我的代码,我想从我的地图中删除这些框以绘制新的。但是,在尝试找到解决方案 2 天后,我放弃了。 wideroad 是一个来自 10、20、30 公里等下拉列表的参数。

function routeBoxer(wideroad) {
  this.route = [];
  this.waypoints = []; //Array for drawBoxes
  this.wideroad = parseInt(wideroad); //Distance in km
  this.routeArray = routingControl.getWaypoints();
  for (var i=0; i<routeArray.length; i++) {
    waypoints.push(routeArray[i].latLng.lng + ',' + routeArray[i].latLng.lat);
  }
  this.route = loadRoute(waypoints, this.drawRoute);
}; //End routeBoxer()

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([]);
  for (var i = 0; i < boxes.length; i++) {
    **L.rectangle(boxes[i], {color: "#ff7800", weight: 1}).addTo(this.map);**
    bounds.extend(boxes[i]);
  }
  console.log('drawRoute:',boxes);
  this.map.fitBounds(bounds);
  return route;
}; //End drawRoute()

loadRoute = function (waypoints) {
  var url = '//router.project-osrm.org/route/v1/driving/';
  var _this = this;
  url += waypoints.join(';');
  var jqxhr = $.ajax({
    url: url,
    data: {
      overview: 'full',
      steps: false,
      //compression: false,
      alternatives: false
    },
    dataType: 'json'
   })
  .done(function(data) {
    _this.drawRoute(data.routes[0].geometry);
    //console.log("loadRoute.done:",data);
  })
  .fail(function(data) {
    //console.log("loadRoute.fail:",data);
  });
}; //End loadRoute()

好吧,我现在的问题是如何删除以前绘制的框以绘制新的框,因为使用下拉列表更改了wideroad。我从 Leaflet-routeboxer 应用程序中获得的大部分代码。 提前感谢您的帮助...

【问题讨论】:

    标签: leaflet routeboxer


    【解决方案1】:

    您必须保留对矩形的引用,以便以后可以操作(删除它们)。请注意,Leaflet 和 Leaflet-routeboxer 都不会为您执行此操作。

    例如:

    if (this._currentlyDisplayedRectangles) {
        for (var i = 0; i < this._currentlyDisplayedRectangles.length; i++) {
            this._currentlyDisplayedRectangles[i].remove();
        }
    } else {
        this._currentlyDisplayedRectangles = [];
    }
    
    for (var i = 0; i < boxes.length; i++) {
        var displayedRectangle = L.rectangle(boxes[i], {color: "#ff7800", weight: 1}).addTo(this.map);
        bounds.extend(boxes[i]);
        this._currentlyDisplayedRectangles.push(displayedRectangle);
    }
    

    如果您不存储对L.rectangle() 实例的引用,那么您以后显然将无法对其进行操作。这也适用于其他 Leaflet 层 - 不存储对 Leaflet 层的显式引用是 Leaflet 示例中的常见模式。

    【讨论】:

    • 我在this._currentlyDisplayedRectangles[i].remove()上遇到了一个错误; CorridorHandler.js:107 Uncaught TypeError: this._currentlyDisplayedRectangles[i].remove 不是函数
    • 您可能使用的是旧版本的 Leaflet,它没有在 L.Layers 上实现 remove() 方法。检查您正在使用的版本的文档,或升级到 Leaflet 1.0.0。
    • 嗯,我必须回到Leaflet0.7.7,因为有太多东西不起作用。对于我上面的错误,我必须搜索另一个解决方案。如果你能帮助我很好。
    猜你喜欢
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    相关资源
    最近更新 更多