Skip to content

Commit 0a55fac

Browse files
authored
Merge pull request #110 from gtt-project/fix/geolocation
Fixed geolocation error
2 parents 9128289 + 660cc84 commit 0a55fac

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

src/components/gtt-client.ts

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
import { focus as events_condifition_focus } from 'ol/events/condition'
1919
import { defaults as control_defaults} from 'ol/control'
2020
import { transform, fromLonLat, transformExtent } from 'ol/proj'
21-
import { createEmpty, extend, getCenter } from 'ol/extent'
21+
import { createEmpty, extend, getCenter, containsCoordinate } from 'ol/extent'
2222
import { FeatureCollection } from 'geojson'
2323
import { quick_hack } from './quick_hack'
2424
import Vector from 'ol/source/Vector'
@@ -59,14 +59,15 @@ export class GttClient {
5959
filters: FilterOption
6060
vector: VectorLayer
6161
bounds: VectorLayer
62-
geolocation: Geolocation
62+
geolocations: Array<Geolocation>
6363

6464
constructor(options: GttClientOption) {
6565
this.filters = {
6666
location: false,
6767
distance: false
6868
}
6969
this.maps = []
70+
this.geolocations = []
7071

7172
// needs target
7273
if (!options.target) {
@@ -241,7 +242,7 @@ export class GttClient {
241242
this.toolbar.setPosition('bottom-left' as any) // is type.d old?
242243
this.map.addControl(this.toolbar)
243244
this.setView()
244-
this.setGeolocation()
245+
this.setGeolocation(this.map)
245246
this.setGeocoding(this.map)
246247
this.parseHistory()
247248

@@ -711,13 +712,13 @@ export class GttClient {
711712
m.getView().setCenter(transform([parseFloat(this.defaults.lon), parseFloat(this.defaults.lat)],
712713
'EPSG:4326', 'EPSG:3857'));
713714
})
714-
if (this.geolocation) {
715-
this.geolocation.once('change:position', (_) => {
715+
this.geolocations.forEach(g => {
716+
g.once('change:position', (evt) => {
716717
this.maps.forEach(m => {
717-
m.getView().setCenter(this.geolocation.getPosition())
718+
m.getView().setCenter(g.getPosition())
718719
})
719720
})
720-
}
721+
})
721722
}
722723
}
723724

@@ -799,27 +800,30 @@ export class GttClient {
799800
/**
800801
* Add Geolocation functionality
801802
*/
802-
setGeolocation() {
803-
this.geolocation = new Geolocation({
803+
setGeolocation(currentMap: Map) {
804+
const geolocation = new Geolocation({
804805
tracking: false,
805-
projection: this.map.getView().getProjection()
806+
projection: currentMap.getView().getProjection()
806807
})
807-
this.geolocation.on('change', () => {
808+
this.geolocations.push(geolocation)
809+
810+
geolocation.on('change', (evt) => {
808811
console.log({
809-
accuracy: this.geolocation.getAccuracy(),
810-
altitude: this.geolocation.getAltitude(),
811-
altitudeAccuracy: this.geolocation.getAltitudeAccuracy(),
812-
heading: this.geolocation.getHeading(),
813-
speed: this.geolocation.getSpeed()
812+
accuracy: geolocation.getAccuracy(),
813+
altitude: geolocation.getAltitude(),
814+
altitudeAccuracy: geolocation.getAltitudeAccuracy(),
815+
heading: geolocation.getHeading(),
816+
speed: geolocation.getSpeed()
814817
})
815818
})
816-
this.geolocation.on('error', (_) => {
819+
geolocation.on('error', (error) => {
817820
// TBD
821+
console.error(error)
818822
})
819823

820824
const accuracyFeature = new Feature()
821-
this.geolocation.on('change:accuracyGeometry', (_) => {
822-
accuracyFeature.setGeometry(this.geolocation.getAccuracyGeometry())
825+
geolocation.on('change:accuracyGeometry', (evt) => {
826+
accuracyFeature.setGeometry(geolocation.getAccuracyGeometry())
823827
})
824828

825829
const positionFeature = new Feature()
@@ -836,9 +840,14 @@ export class GttClient {
836840
})
837841
}))
838842

839-
this.geolocation.on('change:position', (_) => {
840-
const position = this.geolocation.getPosition()
843+
geolocation.on('change:position', (evt) => {
844+
const position = geolocation.getPosition()
841845
positionFeature.setGeometry(position ? new Point(position) : null)
846+
847+
const extent = currentMap.getView().calculateExtent(currentMap.getSize())
848+
if (!containsCoordinate(extent, position)) {
849+
currentMap.getView().setCenter(position)
850+
}
842851
})
843852

844853
const geolocationLayer = new VectorLayer({
@@ -847,19 +856,16 @@ export class GttClient {
847856
})
848857
})
849858
geolocationLayer.set('displayInLayerSwitcher', false)
850-
this.map.addLayer(geolocationLayer)
859+
currentMap.addLayer(geolocationLayer)
851860

852861
// Control button
853862
const geolocationCtrl = new Toggle({
854863
html: '<i class="gtt-icon-compass" ></i>',
855864
title: "Geolocation",
856865
active: false,
857866
onToggle: (active: boolean) => {
858-
this.geolocation.setTracking(active)
867+
geolocation.setTracking(active)
859868
geolocationLayer.setVisible(active)
860-
if (active) {
861-
this.map.getView().setCenter(this.geolocation.getPosition())
862-
}
863869
}
864870
} as any)
865871
this.toolbar.addControl(geolocationCtrl)

0 commit comments

Comments
 (0)