From 7d912dc20b4dfdd50f68092486cac6391f06ed57 Mon Sep 17 00:00:00 2001 From: neo Date: Fri, 29 Nov 2024 14:07:28 +0100 Subject: [PATCH 1/2] UPDATE: Remove mainLizmap --- assets/src/modules/Geolocation.js | 22 ++++++++++++++++------ assets/src/modules/Lizmap.js | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/assets/src/modules/Geolocation.js b/assets/src/modules/Geolocation.js index cf514e0e20..72d4d2e82f 100644 --- a/assets/src/modules/Geolocation.js +++ b/assets/src/modules/Geolocation.js @@ -19,7 +19,13 @@ import Feature from 'ol/Feature.js'; */ export default class Geolocation { - constructor() { + /** + * Create a geolocation instance + * + * @param {Map} map - OpenLayers map + * @param {object} lizmap3 - The old lizmap object + */ + constructor(map, lizmap3) { const color = 'rgb(3, 149, 214)'; const fillColor = 'rgba(3, 149, 214, 0.1)'; const strokeWidth = 1; @@ -41,20 +47,22 @@ export default class Geolocation { name: 'LizmapGeolocationGeolocationDrawLayer' }); - mainLizmap.map.addToolLayer(this._geolocationLayer); + map.addToolLayer(this._geolocationLayer); + this._lizmap3 = lizmap3; this._firstGeolocation = true; this._isBind = false; this._bindIntervalID = 0; this._bindIntervalInSecond = 10; this._isLinkedToEdition = false; + const qgisProjectProjection = lizmap3.map.getProjection(); this._geolocation = new olGeolocation({ // `enableHighAccuracy` must be set to true to have the heading value trackingOptions: { enableHighAccuracy: true }, - projection: mainLizmap.projection + projection: qgisProjectProjection }); this._geolocation.on('change:position', () => { @@ -66,7 +74,7 @@ export default class Geolocation { this._geolocation.on('change:tracking', () => { // FIXME : later we'll need an object listening to 'geolocation.isTracking' event and setting visibility accordingly - const geolocationLayer = mainLizmap._lizmap3.map.getLayersByName('geolocation')[0]; + const geolocationLayer = this._lizmap3.map.getLayersByName('geolocation')[0]; if (geolocationLayer) { geolocationLayer.setVisibility(this.isTracking); } @@ -143,7 +151,8 @@ export default class Geolocation { return this.position; } else { const position = this._geolocation.getPosition(); - return transform(position, mainLizmap.projection, crs); + const qgisProjectProjection = this._lizmap3.map.getProjection(); + return transform(position, qgisProjectProjection, crs); } } @@ -155,7 +164,8 @@ export default class Geolocation { get position() { const position = this._geolocation.getPosition(); if (position) { - const position4326 = transform(position, mainLizmap.projection, 'EPSG:4326'); + const qgisProjectProjection = this._lizmap3.map.getProjection(); + const position4326 = transform(position, qgisProjectProjection, 'EPSG:4326'); return [parseFloat(position4326[0].toFixed(6)), parseFloat(position4326[1].toFixed(6))]; } return undefined; diff --git a/assets/src/modules/Lizmap.js b/assets/src/modules/Lizmap.js index 8ec8bb1cc8..6b7080902a 100644 --- a/assets/src/modules/Lizmap.js +++ b/assets/src/modules/Lizmap.js @@ -152,7 +152,7 @@ export default class Lizmap { this.map = new map(); this.edition = new Edition(this._lizmap3); this.featuresTable = new FeaturesTable(this.initialConfig, this.lizmap3); - this.geolocation = new Geolocation(); + this.geolocation = new Geolocation(this.map, this.lizmap3); this.geolocationSurvey = new GeolocationSurvey(); this.selectionTool = new SelectionTool(); this.digitizing = new Digitizing(); From c80aa92e14d61dd3c886b7b68c6c3ed8d2e0f49e Mon Sep 17 00:00:00 2001 From: neo Date: Mon, 2 Dec 2024 12:37:24 +0100 Subject: [PATCH 2/2] UPDATE: Remove mainLizmap dependencies --- assets/src/modules/Geolocation.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/assets/src/modules/Geolocation.js b/assets/src/modules/Geolocation.js index 72d4d2e82f..f86c6e85f8 100644 --- a/assets/src/modules/Geolocation.js +++ b/assets/src/modules/Geolocation.js @@ -4,7 +4,7 @@ * @copyright 2023 3Liz * @license MPL-2.0 */ -import { mainLizmap, mainEventDispatcher } from '../modules/Globals.js'; +import { mainEventDispatcher } from '../modules/Globals.js'; import olGeolocation from 'ol/Geolocation.js'; import { transform } from 'ol/proj.js'; import { Vector as VectorSource } from 'ol/source.js'; @@ -50,6 +50,7 @@ export default class Geolocation { map.addToolLayer(this._geolocationLayer); this._lizmap3 = lizmap3; + this._map = map; this._firstGeolocation = true; this._isBind = false; this._bindIntervalID = 0; @@ -97,7 +98,8 @@ export default class Geolocation { this._geolocation.on('change:accuracyGeometry', () => { // Zoom on accuracy geometry extent when geolocation is activated for the first time if (this._firstGeolocation) { - mainLizmap.extent = this._geolocation.getAccuracyGeometry().getExtent(); + const bounds = this._geolocation.getAccuracyGeometry().getExtent(); + map.getView().fit(bounds, {nearest: true}); this.center(); this._firstGeolocation = false; @@ -107,12 +109,13 @@ export default class Geolocation { // Handle geolocation error this._geolocation.on('error', error => { - mainLizmap.displayMessage(error.message, 'danger', true); + this._lizmap3.addMessage(error.message, 'danger', true); }); } center() { - mainLizmap.center = this._geolocation.getPosition(); + const center = this._geolocation.getPosition(); + this._map.getView().setCenter(center); } toggleBind() {