import Geolocation from 'ol/Geolocation'
import { convertCoordinates } from './mapviewer-services'
const libNamespace = 'geoLocation'
/**
* Module GPS pour Core , utiliser .use(GeoLocation({}))
* @module geoLocation
*/
class MapviewerGeolocation {
// TODO reprendre les fonctionnalités de Core (orientation, mock??)
constructor (viewer, options) {
this.ctx = viewer
this.position = null
this.available = false
if (navigator.geolocation === undefined) { return }
this.geolocation = new Geolocation({
trackingOptions: {
enableHighAccuracy: true,
},
projection: this.ctx.Map.getView().getProjection(),
})
this.geolocation.on('error', function (error) {
console.log('mapviewerGeolocation', error.message)
})
this.geolocation.on('change:position', ev => this.setPosition(ev))
this.geolocation.setTracking(true)
this.available = true
}
setPosition (ev) {
this.position = ev.target.getPosition()
this.ctx.dispatchEvent('geolocation:change', this.position)
}
/** position converti dans le susteme de coordonnées de la carte */
getPosition () {
return convertCoordinates(this.position, this.ctx.Map.getView().getProjection(), 'EPSG:4326')
// return this.position
}
getPositionFps () {
return this.position
}
isAvailable () {
return this.available
}
}
// Permet d'etendre le module
export default function extendCoreLib (options) {
return function patch (viewer) {
const functions = { }
functions[libNamespace] = new MapviewerGeolocation(viewer, options)
return Object.assign(viewer, functions)
}
}