Skip to content

Commit

Permalink
Merge pull request #5028 from neo-garaix/red-ml-dep/geolocation-module
Browse files Browse the repository at this point in the history
Reduce mainLizmap dependencies in Geolocation module
  • Loading branch information
rldhont authored Dec 2, 2024
2 parents 01442d7 + c80aa92 commit da7ca75
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
33 changes: 23 additions & 10 deletions assets/src/modules/Geolocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand All @@ -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', () => {
Expand All @@ -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);
}
Expand All @@ -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;

Expand All @@ -99,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() {
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion assets/src/modules/Lizmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit da7ca75

Please sign in to comment.