From 1e2e8d9bea0560d3ea75b40ca5cd7ab0bb5f4afd Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 31 Oct 2020 13:15:42 -0700 Subject: Import GPX file as geofence --- web/app/view/map/GeofenceMapController.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'web/app/view/map/GeofenceMapController.js') diff --git a/web/app/view/map/GeofenceMapController.js b/web/app/view/map/GeofenceMapController.js index f075ac93..5274bc39 100644 --- a/web/app/view/map/GeofenceMapController.js +++ b/web/app/view/map/GeofenceMapController.js @@ -33,6 +33,30 @@ Ext.define('Traccar.view.map.GeofenceMapController', { } }, + onFileChange: function (fileField) { + var reader, parser, xml, segment, point, projection, points = [], view = this.getView(); + if (fileField.fileInputEl.dom.files.length > 0) { + reader = new FileReader(); + reader.onload = function (event) { + parser = new DOMParser(); + xml = parser.parseFromString(reader.result, 'text/xml'); + segment = xml.getElementsByTagName('trkseg')[0]; + projection = view.mapView.getProjection(); + Array.from(segment.getElementsByTagName('trkpt')).forEach(function (point) { + lat = Number(point.getAttribute('lat')); + lon = Number(point.getAttribute('lon')); + points.push(ol.proj.transform([lon, lat], 'EPSG:4326', projection)); + }); + view.getFeatures().clear(); + view.getFeatures().push(new ol.Feature(new ol.geom.LineString(points))); + }; + reader.onerror = function (event) { + Traccar.app.showError(event.target.error); + }; + reader.readAsText(fileField.fileInputEl.dom.files[0]); + } + }, + onSaveClick: function (button) { var geometry, projection; if (this.getView().getFeatures().getLength() > 0) { -- cgit v1.2.3 From 5395dc657dc9f9d6d53b09b2bf69d6492d39cf1b Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 31 Oct 2020 13:19:13 -0700 Subject: Fix lint issues --- web/app/view/map/GeofenceMapController.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'web/app/view/map/GeofenceMapController.js') diff --git a/web/app/view/map/GeofenceMapController.js b/web/app/view/map/GeofenceMapController.js index 5274bc39..237858ad 100644 --- a/web/app/view/map/GeofenceMapController.js +++ b/web/app/view/map/GeofenceMapController.js @@ -34,15 +34,16 @@ Ext.define('Traccar.view.map.GeofenceMapController', { }, onFileChange: function (fileField) { - var reader, parser, xml, segment, point, projection, points = [], view = this.getView(); + var reader, parser, xml, segment, projection, points = [], view = this.getView(); if (fileField.fileInputEl.dom.files.length > 0) { reader = new FileReader(); - reader.onload = function (event) { + reader.onload = function () { parser = new DOMParser(); xml = parser.parseFromString(reader.result, 'text/xml'); segment = xml.getElementsByTagName('trkseg')[0]; projection = view.mapView.getProjection(); Array.from(segment.getElementsByTagName('trkpt')).forEach(function (point) { + var lat, lon; lat = Number(point.getAttribute('lat')); lon = Number(point.getAttribute('lon')); points.push(ol.proj.transform([lon, lat], 'EPSG:4326', projection)); -- cgit v1.2.3 From f5960bacc081068532fa7a3c899a0b3349b0c2f8 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 31 Oct 2020 13:20:19 -0700 Subject: Move variables --- web/app/view/map/GeofenceMapController.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'web/app/view/map/GeofenceMapController.js') diff --git a/web/app/view/map/GeofenceMapController.js b/web/app/view/map/GeofenceMapController.js index 237858ad..31ab586c 100644 --- a/web/app/view/map/GeofenceMapController.js +++ b/web/app/view/map/GeofenceMapController.js @@ -34,10 +34,11 @@ Ext.define('Traccar.view.map.GeofenceMapController', { }, onFileChange: function (fileField) { - var reader, parser, xml, segment, projection, points = [], view = this.getView(); + var reader, view = this.getView(); if (fileField.fileInputEl.dom.files.length > 0) { reader = new FileReader(); reader.onload = function () { + var parser, xml, segment, projection, points = []; parser = new DOMParser(); xml = parser.parseFromString(reader.result, 'text/xml'); segment = xml.getElementsByTagName('trkseg')[0]; -- cgit v1.2.3