diff --git a/assets/src/modules/Geolocation.js b/assets/src/modules/Geolocation.js index 82b3756246..9538cfb5a4 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'; @@ -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,23 @@ export default class Geolocation { name: 'LizmapGeolocationGeolocationDrawLayer' }); - mainLizmap.map.addToolLayer(this._geolocationLayer); + map.addToolLayer(this._geolocationLayer); + this._lizmap3 = lizmap3; + this._map = map; 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 +75,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); } @@ -89,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; @@ -99,12 +109,13 @@ export default class Geolocation { // Handle geolocation error this._geolocation.on('error', error => { - mainLizmap.displayMessage(error.message, 'error', true); + this._lizmap3.addMessage(error.message, 'error', true); }); } center() { - mainLizmap.center = this._geolocation.getPosition(); + const center = this._geolocation.getPosition(); + this._map.getView().setCenter(center); } toggleBind() { @@ -143,7 +154,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 +167,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();