@@ -18,7 +18,7 @@ import {
18
18
import { focus as events_condifition_focus } from 'ol/events/condition'
19
19
import { defaults as control_defaults } from 'ol/control'
20
20
import { transform , fromLonLat , transformExtent } from 'ol/proj'
21
- import { createEmpty , extend , getCenter } from 'ol/extent'
21
+ import { createEmpty , extend , getCenter , containsCoordinate } from 'ol/extent'
22
22
import { FeatureCollection } from 'geojson'
23
23
import { quick_hack } from './quick_hack'
24
24
import Vector from 'ol/source/Vector'
@@ -59,14 +59,15 @@ export class GttClient {
59
59
filters : FilterOption
60
60
vector : VectorLayer
61
61
bounds : VectorLayer
62
- geolocation : Geolocation
62
+ geolocations : Array < Geolocation >
63
63
64
64
constructor ( options : GttClientOption ) {
65
65
this . filters = {
66
66
location : false ,
67
67
distance : false
68
68
}
69
69
this . maps = [ ]
70
+ this . geolocations = [ ]
70
71
71
72
// needs target
72
73
if ( ! options . target ) {
@@ -241,7 +242,7 @@ export class GttClient {
241
242
this . toolbar . setPosition ( 'bottom-left' as any ) // is type.d old?
242
243
this . map . addControl ( this . toolbar )
243
244
this . setView ( )
244
- this . setGeolocation ( )
245
+ this . setGeolocation ( this . map )
245
246
this . setGeocoding ( this . map )
246
247
this . parseHistory ( )
247
248
@@ -711,13 +712,13 @@ export class GttClient {
711
712
m . getView ( ) . setCenter ( transform ( [ parseFloat ( this . defaults . lon ) , parseFloat ( this . defaults . lat ) ] ,
712
713
'EPSG:4326' , 'EPSG:3857' ) ) ;
713
714
} )
714
- if ( this . geolocation ) {
715
- this . geolocation . once ( 'change:position' , ( _ ) => {
715
+ this . geolocations . forEach ( g => {
716
+ g . once ( 'change:position' , ( evt ) => {
716
717
this . maps . forEach ( m => {
717
- m . getView ( ) . setCenter ( this . geolocation . getPosition ( ) )
718
+ m . getView ( ) . setCenter ( g . getPosition ( ) )
718
719
} )
719
720
} )
720
- }
721
+ } )
721
722
}
722
723
}
723
724
@@ -799,27 +800,30 @@ export class GttClient {
799
800
/**
800
801
* Add Geolocation functionality
801
802
*/
802
- setGeolocation ( ) {
803
- this . geolocation = new Geolocation ( {
803
+ setGeolocation ( currentMap : Map ) {
804
+ const geolocation = new Geolocation ( {
804
805
tracking : false ,
805
- projection : this . map . getView ( ) . getProjection ( )
806
+ projection : currentMap . getView ( ) . getProjection ( )
806
807
} )
807
- this . geolocation . on ( 'change' , ( ) => {
808
+ this . geolocations . push ( geolocation )
809
+
810
+ geolocation . on ( 'change' , ( evt ) => {
808
811
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 ( )
814
817
} )
815
818
} )
816
- this . geolocation . on ( 'error' , ( _ ) => {
819
+ geolocation . on ( 'error' , ( error ) => {
817
820
// TBD
821
+ console . error ( error )
818
822
} )
819
823
820
824
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 ( ) )
823
827
} )
824
828
825
829
const positionFeature = new Feature ( )
@@ -836,9 +840,14 @@ export class GttClient {
836
840
} )
837
841
} ) )
838
842
839
- this . geolocation . on ( 'change:position' , ( _ ) => {
840
- const position = this . geolocation . getPosition ( )
843
+ geolocation . on ( 'change:position' , ( evt ) => {
844
+ const position = geolocation . getPosition ( )
841
845
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
+ }
842
851
} )
843
852
844
853
const geolocationLayer = new VectorLayer ( {
@@ -847,19 +856,16 @@ export class GttClient {
847
856
} )
848
857
} )
849
858
geolocationLayer . set ( 'displayInLayerSwitcher' , false )
850
- this . map . addLayer ( geolocationLayer )
859
+ currentMap . addLayer ( geolocationLayer )
851
860
852
861
// Control button
853
862
const geolocationCtrl = new Toggle ( {
854
863
html : '<i class="gtt-icon-compass" ></i>' ,
855
864
title : "Geolocation" ,
856
865
active : false ,
857
866
onToggle : ( active : boolean ) => {
858
- this . geolocation . setTracking ( active )
867
+ geolocation . setTracking ( active )
859
868
geolocationLayer . setVisible ( active )
860
- if ( active ) {
861
- this . map . getView ( ) . setCenter ( this . geolocation . getPosition ( ) )
862
- }
863
869
}
864
870
} as any )
865
871
this . toolbar . addControl ( geolocationCtrl )
0 commit comments