diff --git a/js/plugin/Routing.js b/js/plugin/Routing.js index f0ee5ab..dedf2b8 100644 --- a/js/plugin/Routing.js +++ b/js/plugin/Routing.js @@ -36,6 +36,7 @@ BR.Routing = L.Routing.extend({ onAdd: function(map) { this._segmentsCasing = new L.FeatureGroup().addTo(map); + this._loadingTrailerGroup = new L.FeatureGroup().addTo(map); var container = L.Routing.prototype.onAdd.call(this, map); @@ -276,9 +277,24 @@ BR.Routing = L.Routing.extend({ }; this.fire('routing:setWaypointsStart'); + + // Workaround to optimize performance. + // Add markers/layers to map "at once" and avoid a repaint for every single one. + // Therefore remove and re-add FeatureGroup from/to map, also need to unset map reference, + // as LayerGroup.addLayer would add to map anyway. + this._waypoints.remove(); + this._waypoints._map = null; + this._loadingTrailerGroup.remove(); + this._loadingTrailerGroup._map = null; + for (i = 0; latLngs && i < latLngs.length; i++) { this.addWaypoint(latLngs[i], this._waypoints._last, null, callback); } + + this._loadingTrailerGroup._map = this._map; + this._loadingTrailerGroup.addTo(this._map); + this._waypoints._map = this._map; + this._waypoints.addTo(this._map); }, // patch to fix error when line is null or error line @@ -313,7 +329,7 @@ BR.Routing = L.Routing.extend({ dashArray: [10, 10], className: 'loading-trailer' }); - loadingTrailer.addTo(this._map); + this._loadingTrailerGroup.addLayer(loadingTrailer); } L.Routing.prototype._routeSegment.call( @@ -322,7 +338,7 @@ BR.Routing = L.Routing.extend({ m2, L.bind(function(err, data) { if (loadingTrailer) { - this._map.removeLayer(loadingTrailer); + this._loadingTrailerGroup.removeLayer(loadingTrailer); } cb(err, data); }, this) diff --git a/package.json b/package.json index 7deece8..0867742 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,7 @@ "leaflet-hotline": "^0.4.0", "leaflet-plugins": "~3.0.0", "leaflet-providers": "^1.5.0", - "leaflet-routing": "nrenner/leaflet-routing#62bf722", + "leaflet-routing": "nrenner/leaflet-routing#e94e153", "leaflet-sidebar-v2": "nrenner/leaflet-sidebar-v2#dev", "leaflet-triangle-marker": "^1.0.2", "leaflet.locatecontrol": "^0.60.0", diff --git a/yarn.lock b/yarn.lock index 414148a..8ef86f5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4917,9 +4917,9 @@ leaflet-providers@^1.5.0: resolved "https://registry.yarnpkg.com/leaflet-providers/-/leaflet-providers-1.5.0.tgz#06d598231ef36f8d0566c219cedf18cef2ff80f5" integrity sha512-btncloSyOHrgYNexoz2dRpCl+U9iDQME91RsOWQWNAD9jQUPAkq9mxuTvL/O9VOwrqcEtzhvuHBHIOacJAZDxQ== -leaflet-routing@nrenner/leaflet-routing#62bf722: - version "0.1.2" - resolved "https://codeload.github.com/nrenner/leaflet-routing/tar.gz/62bf72277a99d478f7d3f866ca85be0977089c13" +leaflet-routing@nrenner/leaflet-routing#e94e153: + version "0.1.3" + resolved "https://codeload.github.com/nrenner/leaflet-routing/tar.gz/e94e153b7574510313cb0bfefcd8776edebf627e" leaflet-sidebar-v2@nrenner/leaflet-sidebar-v2#dev: version "3.0.2"