diff --git a/js/plugin/RouteLoaderConverter.js b/js/plugin/RouteLoaderConverter.js index 72b4dd2..5cb58ee 100644 --- a/js/plugin/RouteLoaderConverter.js +++ b/js/plugin/RouteLoaderConverter.js @@ -8,6 +8,7 @@ BR.routeLoader = function(map, layersControl, routing, pois) { _maxTrackPoints: 200, _closeCanceled: true, _currentGeoJSON: {}, + _counter: 0, _options: { format: undefined, showTrackLayer: true, @@ -37,20 +38,22 @@ BR.routeLoader = function(map, layersControl, routing, pois) { }); }, - getSimplifiedLatLngs: function() { + getSimplifiedCoords: function(tolerance) { var simplifiedLine = turf.simplify(this._trackPoints.geometry, { - tolerance: this._options.simplifyTolerance, + tolerance: tolerance, highQuality: true }); - - return L.GeoJSON.coordsToLatLngs(simplifiedLine.coordinates); + return simplifiedLine.coordinates; + // return L.GeoJSON.coordsToLatLngs(simplifiedLine.coordinates); }, refreshTestLayer: function() { this.onBusyChanged(true); this._testLayer.clearLayers(); - var simplifiedLatLngs = this.getSimplifiedLatLngs(); + var simplifiedLatLngs = L.GeoJSON.coordsToLatLngs( + this.getSimplifiedCoords(this._options.simplifyTolerance) + ); if (simplifiedLatLngs.length > this._maxTrackPoints) { this.onBusyChanged(false); return false; @@ -94,6 +97,7 @@ BR.routeLoader = function(map, layersControl, routing, pois) { setSliderRange: function() { $slider = $('#simplify_tolerance'); + $slider.prop('min', -500); var guessedTolerance = this.guessSimplifyTolerance(this._trackPoints); $slider.data('guess', guessedTolerance.toFixed(20)); $slider.val(0); @@ -111,8 +115,18 @@ BR.routeLoader = function(map, layersControl, routing, pois) { else this._options.simplifyTolerance = Math.abs(guess + Math.pow(f, 3) * (guess / Math.pow(frac, 3))); if (!this.refreshTestLayer()) { + var iterate = this.findLowestTolerance( + parseInt(e.target.value), + parseInt($(e.target).data('lastknowngood')), + guess, + frac + ); + $(e.target).prop('min', iterate); + $(e.target).data('lastknowngood', iterate); e.target.value = $(e.target).data('lastknowngood'); - this._options.simplifyTolerance = this._options.simplifyLastKnownGood; + this._options.simplifyTolerance = this._options.simplifyLastKnownGood = Math.abs( + guess + Math.pow(iterate, 3) * (guess / Math.pow(frac, 3)) + ); this.refreshTestLayer(); } else { this._options.simplifyLastKnownGood = this._options.simplifyTolerance; @@ -120,6 +134,18 @@ BR.routeLoader = function(map, layersControl, routing, pois) { } }, + findLowestTolerance: function(min, max, guess, frac) { + if (Math.abs(max - min) <= 2) return max; + var meridian = Math.round((max + min) / 2); + + var tolerance = Math.abs(guess + Math.pow(meridian, 3) * (guess / Math.pow(frac, 3))); + var simplifiedCoords = this.getSimplifiedCoords(tolerance); + + if (simplifiedCoords.length > this._maxTrackPoints) + return this.findLowestTolerance(meridian, max, guess, frac); + else return this.findLowestTolerance(min, meridian, guess, frac); + }, + onBusyChanged: function(isBusy) { if (typeof isBusy === undefined) { isBusy = false; @@ -177,7 +203,7 @@ BR.routeLoader = function(map, layersControl, routing, pois) { ); }, this); - L.DomUtil.get('simplify_tolerance').onchange = L.bind(this.onToleranceSlider, this); + L.DomUtil.get('simplify_tolerance').oninput = L.bind(this.onToleranceSlider, this); L.DomUtil.get('loadedittrackFile').onchange = L.bind(this.onFileChanged, this); this.onFileChanged({ target: L.DomUtil.get('loadedittrackFile') }); @@ -294,7 +320,9 @@ BR.routeLoader = function(map, layersControl, routing, pois) { this._options.simplifyTolerance = this.guessSimplifyTolerance(this._trackPoints); var routingPoints = []; - var simplifiedLatLngs = this.getSimplifiedLatLngs(); + var simplifiedLatLngs = L.GeoJSON.coordsToLatLngs( + this.getSimplifiedCoords(this._options.simplifyTolerance) + ); for (var i = 0; i < simplifiedLatLngs.length; i++) { routingPoints.push(simplifiedLatLngs[i]);