@@ -18,6 +18,14 @@ define([
18
18
minZoom : 3 ,
19
19
maxZoom : 18 ,
20
20
zoom : 17 ,
21
+ geolocationZoom : 12 ,
22
+ } ;
23
+
24
+ const geoStatus = {
25
+ READY : 'ready' ,
26
+ PENDING : 'pending' ,
27
+ ERROR : 'error' ,
28
+ DENIED : 'denied' ,
21
29
} ;
22
30
23
31
return Cliche . extend ( {
@@ -119,7 +127,17 @@ define([
119
127
this . yearRefreshMarkersBind = this . yearRefreshMarkers . bind ( this ) ;
120
128
this . yearRefreshMarkersTimeout = null ;
121
129
122
- this . infoShow = ko . observable ( true ) ;
130
+ // Geolocation
131
+ this . geolocationStatus = ko . observable ( geoStatus . READY ) ;
132
+
133
+ if ( 'permissions' in navigator ) {
134
+ navigator . permissions . query ( { name : 'geolocation' } ) . then ( result => {
135
+ if ( result . state === 'denied' ) {
136
+ // Use of geolocation is already denied for this site.
137
+ this . geolocationStatus ( geoStatus . DENIED ) ;
138
+ }
139
+ } ) ;
140
+ }
123
141
124
142
this . layers . push ( {
125
143
id : 'osm' ,
@@ -876,6 +894,39 @@ define([
876
894
877
895
return false ;
878
896
} ,
897
+ isGeolocationSupported : function ( ) {
898
+ return ! ! ( 'geolocation' in navigator ) ;
899
+ } ,
900
+ showMyLocation : function ( ) {
901
+ // Geolocate current position. Query position even if we know
902
+ // that user denied it already, in Chrome for example this will show
903
+ // location icon in status bar, making easier to find
904
+ // where to change this setting. Don't query if there is a pending
905
+ // request already.
906
+ if ( this . geolocationStatus ( ) !== geoStatus . PENDING ) {
907
+ this . geolocationStatus ( geoStatus . PENDING ) ;
908
+
909
+ const success = function ( position ) {
910
+ this . geolocationStatus ( geoStatus . READY ) ;
911
+ this . map . setView ( new L . LatLng ( position . coords . latitude , position . coords . longitude ) ,
912
+ defaults . geolocationZoom , { animate : true } ) ;
913
+ } . bind ( this ) ;
914
+
915
+ const error = function error ( err ) {
916
+ if ( err . code === err . PERMISSION_DENIED ) {
917
+ // User denied geolocation.
918
+ this . geolocationStatus ( geoStatus . DENIED ) ;
919
+ } else {
920
+ // Position unavilable due to timeout or device internal error.
921
+ this . geolocationStatus ( geoStatus . ERROR ) ;
922
+ }
923
+
924
+ console . warn ( `Geolocation error: ${ err . message } ` ) ;
925
+ } . bind ( this ) ;
926
+
927
+ navigator . geolocation . getCurrentPosition ( success , error , { maximumAge : 30000 , timeout : 10000 } ) ;
928
+ }
929
+ } ,
879
930
copyGeo : function ( data , evt ) {
880
931
if ( this . point . geo ( ) ) {
881
932
// Temporaly hide custom tooltip so it does not overlap flashing one.
0 commit comments