From b93d36f154d6e5b16769ddec07bb8936512f8bb1 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 15 Mar 2017 09:38:12 +0500 Subject: Zoom to all devices on launch --- web/app/controller/Root.js | 17 +++++++++++++++++ web/app/view/MapController.js | 28 +++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) (limited to 'web') diff --git a/web/app/controller/Root.js b/web/app/controller/Root.js index 404fb7cc..94f0705f 100644 --- a/web/app/controller/Root.js +++ b/web/app/controller/Root.js @@ -167,6 +167,9 @@ Ext.define('Traccar.controller.Root', { self.updateEvents(data.events); } }; + if (first) { + this.first = true; + } }, updateDevices: function (array) { @@ -196,6 +199,10 @@ Ext.define('Traccar.controller.Root', { store.add(Ext.create('Traccar.model.Position', array[i])); } } + if (this.first) { + this.zoomToAllDevices(); + this.first = false; + } }, updateEvents: function (array) { @@ -225,5 +232,15 @@ Ext.define('Traccar.controller.Root', { Ext.toast(text, device.get('name'), 'br'); } } + }, + + zoomToAllDevices: function () { + var lat, lon, zoom; + lat = Traccar.app.getPreference('latitude', 0); + lon = Traccar.app.getPreference('longitude', 0); + zoom = Traccar.app.getPreference('zoom', 0); + if (lat === 0 && lon === 0 && zoom === 0) { + this.fireEvent('zoomtoalldevices'); + } } }); diff --git a/web/app/view/MapController.js b/web/app/view/MapController.js index 95296f14..1a2a9c13 100644 --- a/web/app/view/MapController.js +++ b/web/app/view/MapController.js @@ -28,7 +28,8 @@ Ext.define('Traccar.view.MapController', { controller: { '*': { mapstaterequest: 'getMapState', - togglestaterequest: 'getToggleState' + togglestaterequest: 'getToggleState', + zoomtoalldevices: 'zoomToAllDevices' } }, store: { @@ -98,5 +99,30 @@ Ext.define('Traccar.view.MapController', { return true; }, this); } + }, + + zoomToAllDevices: function () { + var data, i, point, minx, miny, maxx, maxy; + data = Ext.getStore('LatestPositions').getData(); + for (i = 0; i < data.items.length; i++) { + point = ol.proj.fromLonLat([ + data.items[i].get('longitude'), + data.items[i].get('latitude') + ]); + if (i === 0) { + minx = maxx = point[0]; + miny = maxy = point[1]; + } else { + minx = Math.min(point[0], minx); + miny = Math.min(point[1], miny); + maxx = Math.max(point[0], maxx); + maxy = Math.max(point[1], maxy); + } + } + if (minx !== maxx || miny !== maxy) { + this.getView().getMapView().fit([minx, miny, maxx, maxy]); + } else if (point) { + this.getView().getMapView().fit(new ol.geom.Point(point)); + } } }); -- cgit v1.2.3 From f9cbb295fcb677b7fc44f17afa22e50c3ef48a16 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 15 Mar 2017 10:55:49 +0500 Subject: Separate function for zooming to array of positions --- web/app/view/MapController.js | 23 +----------------- web/app/view/MapMarkerController.js | 48 +++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 43 deletions(-) (limited to 'web') diff --git a/web/app/view/MapController.js b/web/app/view/MapController.js index 1a2a9c13..57727f20 100644 --- a/web/app/view/MapController.js +++ b/web/app/view/MapController.js @@ -102,27 +102,6 @@ Ext.define('Traccar.view.MapController', { }, zoomToAllDevices: function () { - var data, i, point, minx, miny, maxx, maxy; - data = Ext.getStore('LatestPositions').getData(); - for (i = 0; i < data.items.length; i++) { - point = ol.proj.fromLonLat([ - data.items[i].get('longitude'), - data.items[i].get('latitude') - ]); - if (i === 0) { - minx = maxx = point[0]; - miny = maxy = point[1]; - } else { - minx = Math.min(point[0], minx); - miny = Math.min(point[1], miny); - maxx = Math.max(point[0], maxx); - maxy = Math.max(point[1], maxy); - } - } - if (minx !== maxx || miny !== maxy) { - this.getView().getMapView().fit([minx, miny, maxx, maxy]); - } else if (point) { - this.getView().getMapView().fit(new ol.geom.Point(point)); - } + this.zoomToAllPositions(Ext.getStore('LatestPositions').getData().items); } }); diff --git a/web/app/view/MapMarkerController.js b/web/app/view/MapMarkerController.js index 27591c18..bb418704 100644 --- a/web/app/view/MapMarkerController.js +++ b/web/app/view/MapMarkerController.js @@ -311,32 +311,14 @@ Ext.define('Traccar.view.MapMarkerController', { }, addReportMarkers: function (store, data) { - var i, position, point, minx, miny, maxx, maxy; + var i; this.clearReport(); for (i = 0; i < data.length; i++) { - position = data[i]; - point = ol.proj.fromLonLat([ - position.get('longitude'), - position.get('latitude') - ]); - if (i === 0) { - minx = maxx = point[0]; - miny = maxy = point[1]; - } else { - minx = Math.min(point[0], minx); - miny = Math.min(point[1], miny); - maxx = Math.max(point[0], maxx); - maxy = Math.max(point[1], maxy); - } if (store.showMarkers) { - this.addReportMarker(position); + this.addReportMarker(data[i]); } } - if (minx !== maxx || miny !== maxy) { - this.getView().getMapView().fit([minx, miny, maxx, maxy]); - } else if (point) { - this.getView().getMapView().fit(new ol.geom.Point(point)); - } + this.zoomToAllPositions(data); }, clearReport: function () { @@ -482,5 +464,29 @@ Ext.define('Traccar.view.MapMarkerController', { deselectFeature: function () { this.selectMarker(null, false); this.fireEvent('deselectfeature'); + }, + + zoomToAllPositions: function (data) { + var i, point, minx, miny, maxx, maxy; + for (i = 0; i < data.length; i++) { + point = ol.proj.fromLonLat([ + data[i].get('longitude'), + data[i].get('latitude') + ]); + if (i === 0) { + minx = maxx = point[0]; + miny = maxy = point[1]; + } else { + minx = Math.min(point[0], minx); + miny = Math.min(point[1], miny); + maxx = Math.max(point[0], maxx); + maxy = Math.max(point[1], maxy); + } + } + if (minx !== maxx || miny !== maxy) { + this.getView().getMapView().fit([minx, miny, maxx, maxy]); + } else if (point) { + this.getView().getMapView().fit(new ol.geom.Point(point)); + } } }); -- cgit v1.2.3 From ccc2f0bd4387d7117a1c9b629be2d22edccb18d9 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 15 Mar 2017 14:06:51 +0500 Subject: Use local variable --- web/app/controller/Root.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'web') diff --git a/web/app/controller/Root.js b/web/app/controller/Root.js index 94f0705f..8a9b0d9b 100644 --- a/web/app/controller/Root.js +++ b/web/app/controller/Root.js @@ -124,7 +124,7 @@ Ext.define('Traccar.controller.Root', { }, asyncUpdate: function (first) { - var self = this, protocol, pathname, socket; + var self = this, protocol, pathname, socket, firstPositions = first; protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; pathname = window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/') + 1); socket = new WebSocket(protocol + '//' + window.location.host + pathname + 'api/socket'); @@ -161,15 +161,13 @@ Ext.define('Traccar.controller.Root', { self.updateDevices(data.devices); } if (data.positions) { - self.updatePositions(data.positions); + self.updatePositions(data.positions, firstPositions); + firstPositions = false; } if (data.events) { self.updateEvents(data.events); } }; - if (first) { - this.first = true; - } }, updateDevices: function (array) { @@ -188,7 +186,7 @@ Ext.define('Traccar.controller.Root', { } }, - updatePositions: function (array) { + updatePositions: function (array, first) { var i, store, entity; store = Ext.getStore('LatestPositions'); for (i = 0; i < array.length; i++) { @@ -199,9 +197,8 @@ Ext.define('Traccar.controller.Root', { store.add(Ext.create('Traccar.model.Position', array[i])); } } - if (this.first) { + if (first) { this.zoomToAllDevices(); - this.first = false; } }, -- cgit v1.2.3 From 7135682a0f262dc4b76f41bec2db4c022f88b17b Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 15 Mar 2017 14:38:04 +0500 Subject: Remove intermediate variable --- web/app/controller/Root.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'web') diff --git a/web/app/controller/Root.js b/web/app/controller/Root.js index 8a9b0d9b..b6e5f385 100644 --- a/web/app/controller/Root.js +++ b/web/app/controller/Root.js @@ -124,7 +124,7 @@ Ext.define('Traccar.controller.Root', { }, asyncUpdate: function (first) { - var self = this, protocol, pathname, socket, firstPositions = first; + var self = this, protocol, pathname, socket; protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; pathname = window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/') + 1); socket = new WebSocket(protocol + '//' + window.location.host + pathname + 'api/socket'); @@ -161,8 +161,8 @@ Ext.define('Traccar.controller.Root', { self.updateDevices(data.devices); } if (data.positions) { - self.updatePositions(data.positions, firstPositions); - firstPositions = false; + self.updatePositions(data.positions, first); + first = false; } if (data.events) { self.updateEvents(data.events); -- cgit v1.2.3