nakarte

Source code of https://map.sikmir.ru (fork)
git clone git://git.sikmir.ru/nakarte
Log | Files | Refs | LICENSE

commit 5896fdc429248d886d7a6cabb39f77bf5198bba8
parent 736b25f3a61008feb724b011c15633417c55770d
Author: Sergey Orlov <wladimirych@gmail.com>
Date:   Thu, 21 May 2020 15:11:52 +0200

elevations: extract function for communication with server to separate package

Diffstat:
Asrc/lib/elevations/index.js | 16++++++++++++++++
Msrc/lib/leaflet.control.elevation-profile/index.js | 37++-----------------------------------
2 files changed, 18 insertions(+), 35 deletions(-)

diff --git a/src/lib/elevations/index.js b/src/lib/elevations/index.js @@ -0,0 +1,16 @@ +import config from '~/config'; +import {fetch} from '~/lib/xhr-promise'; + +class ElevationProvider { + constructor(serverUrl = config.elevationsServer) { + this.url = serverUrl; + } + + async get(latlngs) { + const request = latlngs.map((ll) => `${ll.lat.toFixed(6)} ${ll.lng.toFixed(6)}`).join('\n'); + const xhr = await fetch(this.url, {method: 'POST', data: request, withCredentials: true}); + return xhr.responseText.split('\n').map((line) => (line === 'NULL' ? null : parseFloat(line))); + } +} + +export {ElevationProvider}; diff --git a/src/lib/leaflet.control.elevation-profile/index.js b/src/lib/leaflet.control.elevation-profile/index.js @@ -1,7 +1,6 @@ import L from 'leaflet'; import './elevation-profile.css'; -import {fetch} from '~/lib/xhr-promise'; -import config from '~/config'; +import {ElevationProvider} from '~/lib/elevations'; import '~/lib/leaflet.control.commons'; import {notify} from '~/lib/notifications'; import logging from '~/lib/logging'; @@ -84,7 +83,6 @@ function pathRegularSamples(latlngs, step) { const ElevationProfile = L.Class.extend({ options: { - elevationsServer: config.elevationsServer, samplingInterval: 50, sightLine: false }, @@ -103,7 +101,7 @@ const ElevationProfile = L.Class.extend({ var that = this; this.horizZoom = 1; this.dragStart = null; - this._getElevation(samples) + new ElevationProvider().get(samples) .then(function(values) { that.values = values; that._addTo(map); @@ -821,37 +819,6 @@ const ElevationProfile = L.Class.extend({ } }, - _getElevation: function(latlngs) { - function parseResponse(s) { - var values = [], - v; - s = s.split('\n'); - for (var i = 0; i < s.length; i++) { - if (s[i]) { - if (s[i] === 'NULL') { - v = null; - } else { - v = parseFloat(s[i]); - } - values.push(v); - } - } - return values; - } - - var req = []; - for (var i = 0; i < latlngs.length; i++) { - req.push(latlngs[i].lat.toFixed(6) + ' ' + latlngs[i].lng.toFixed(6)); - } - req = req.join('\n'); - const xhrPromise = fetch(this.options.elevationsServer, {method: 'POST', data: req, withCredentials: true}); - this.abortLoading = xhrPromise.abort.bind(xhrPromise); - return xhrPromise.then( - function(xhr) { - return parseResponse(xhr.responseText); - } - ); - }, onCloseButtonClick: function() { this.removeFrom(this._map); }