-
Notifications
You must be signed in to change notification settings - Fork 88
/
component.js
416 lines (375 loc) · 11.6 KB
/
component.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// The MIT License (MIT)
//
// Copyright (c) 2015-2021 Camptocamp SA
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import angular from 'angular';
import ngeoMapFeatureOverlayMgr from 'ngeo/map/FeatureOverlayMgr.js';
import ngeoMessageNotification from 'ngeo/message/Notification.js';
import * as olEasing from 'ol/easing.js';
import {listen} from 'ol/events.js';
import olFeature from 'ol/Feature.js';
import olGeolocation from 'ol/Geolocation.js';
import olMap from 'ol/Map.js';
import olGeomPoint from 'ol/geom/Point.js';
import Polygon from 'ol/geom/Polygon.js';
import {buildStyle} from 'ngeo/options.js';
/**
* @type {angular.IModule}
* @hidden
*/
const myModule = angular.module('ngeoGeolocation', [
ngeoMapFeatureOverlayMgr.name,
ngeoMessageNotification.name,
]);
/**
* @enum {string}
* @private
* @hidden
*/
const GeolocationEventType = {
/**
* Triggered when an error occurs.
*/
ERROR: 'geolocation-error',
};
/**
* Provide a geolocation directive.
*
* Example:
*
* <button ngeo-geolocation
* ngeo-geolocation-map="ctrl.map">
* </button>
*
* See our live example: [../examples/mobilegeolocation.html](../examples/mobilegeolocation.html)
*
* @htmlAttribute {import("ol/Map.js").default} ngeo-geolocation-map The map.
* @htmlAttribute {GeolocationDirectiveOptions} ngeo-geolocation-options The options.
* @return {angular.IDirective} The Directive Definition Object.
* @ngInject
* @ngdoc directive
* @ngname ngeoGeolocation
*/
function geolocationComponent() {
return {
restrict: 'A',
scope: {
'map': '<ngeoGeolocationMap',
'loading': '=ngeoGeolocationLoading',
},
controller: 'ngeoGeolocationController',
bindToController: true,
};
}
myModule.directive('ngeoGeolocation', geolocationComponent);
/**
* @constructor
* @hidden
* @param {angular.IScope} $scope The directive's scope.
* @param {JQuery} $element Element.
* @param {angular.gettext.gettextCatalog} gettextCatalog Gettext service.
* @param {import("ngeo/map/FeatureOverlayMgr.js").FeatureOverlayMgr} ngeoFeatureOverlayMgr The ngeo feature
* overlay manager service.
* @param {import("ngeo/message/Notification.js").MessageNotification} ngeoNotification Ngeo notification
* service.
* @param {import('ngeo/options.js').ngeoGeolocationOptions} ngeoGeolocationOptions The options.
* @ngInject
* @ngdoc controller
* @ngname ngeoGeolocationController
*/
export function Controller(
$scope,
$element,
gettextCatalog,
ngeoFeatureOverlayMgr,
ngeoNotification,
ngeoGeolocationOptions
) {
this.options = ngeoGeolocationOptions;
$element.on('click', this.toggleTracking.bind(this));
/**
* @type {angular.IScope}
*/
this.$scope_ = $scope;
/**
* @type {import("ngeo/message/Notification.js").MessageNotification}
*/
this.notification_ = ngeoNotification;
/**
* @type {import("ngeo/map/FeatureOverlayMgr.js").FeatureOverlayMgr}
*/
this.ngeoFeatureOverlayMgr_ = ngeoFeatureOverlayMgr;
/**
* @type {angular.gettext.gettextCatalog}
*/
this.gettextCatalog_ = gettextCatalog;
/**
* @type {import("ol/Map.js").default}
*/
this.map;
/**
* A flag used to determine if the application has finished loading.
* @type {boolean}
*/
this.loading;
}
/**
*/
Controller.prototype.$onInit = function () {
if (!(this.map instanceof olMap)) {
throw new Error('Wrong map type');
}
/**
* @type {import("ngeo/map/FeatureOverlay.js").FeatureOverlay}
*/
this.featureOverlay_ = this.ngeoFeatureOverlayMgr_.getFeatureOverlay();
/**
* @type {import("ol/Geolocation.js").default}
*/
this.geolocation_ = new olGeolocation({
projection: this.map.getView().getProjection(),
trackingOptions: /** @type {PositionOptions} */ ({
enableHighAccuracy: true,
}),
});
if (this.options.autoRotate) {
this.autoRotateListener();
}
// Add alias for automatic i18n string collection
const gettextCatalog = this.gettextCatalog_;
// handle geolocation error.
this.geolocation_.on(
'error',
/** @type {function(?): ?} */ (
/**
* @param {GeolocationPositionError} error
*/
(error) => {
this.untrack_();
let msg;
switch (error.code) {
case 1:
msg = gettextCatalog.getString('User denied the request for Geolocation.');
break;
case 2:
msg = gettextCatalog.getString('Location information is unavailable.');
break;
case 3:
msg = gettextCatalog.getString('The request to get user location timed out.');
break;
default:
msg = gettextCatalog.getString('Geolocation: An unknown error occurred.');
break;
}
this.notification_.error(msg);
this.$scope_.$emit(GeolocationEventType.ERROR, error);
}
)
);
/**
* @type {olFeature<import("ol/geom/Geometry.js").default>}
*/
this.positionFeature_ = new olFeature();
this.positionFeature_.setStyle(buildStyle(this.options.positionFeatureStyle));
/**
* @type {olFeature<import("ol/geom/Geometry.js").default>}
*/
this.accuracyFeature_ = new olFeature();
this.accuracyFeature_.setStyle(buildStyle(this.options.accuracyFeatureStyle));
/**
* Whether to recenter the map at the position it gets updated
* @type {boolean}
*/
this.follow_ = false;
/**
* A flag used to determine whether the view was changed by me or something
* else. In the latter case, stop following.
* @type {boolean}
*/
this.viewChangedByMe_ = false;
listen(
this.geolocation_,
'change:accuracyGeometry',
/** @type {import("ol/events.js").ListenerFunction} */
(evt) => {
const geometry = this.geolocation_.getAccuracyGeometry();
if (!geometry) {
throw new Error('Missing geometry');
}
this.accuracyFeature_.setGeometry(geometry);
this.setPosition_();
}
);
listen(
this.geolocation_,
'change:position',
/** @type {import("ol/events.js").ListenerFunction} */
() => {
this.setPosition_();
}
);
const view = this.map.getView();
listen(view, 'change:center', this.handleViewChange_, this);
listen(view, 'change:resolution', this.handleViewChange_, this);
if (this.options.atLoadingTime && this.loading !== undefined) {
this.$scope_.$watch(
() => this.loading,
(newVal) => {
if (newVal === false) {
this.toggleTracking();
}
}
);
}
};
/**
*/
Controller.prototype.toggleTracking = function () {
if (this.geolocation_.getTracking()) {
// if map center is different than geolocation position, then track again
const currentPosition = this.geolocation_.getPosition();
// if user is using Firefox and selects the "not now" option, OL geolocation
// doesn't return an error
if (currentPosition === undefined) {
this.untrack_();
this.$scope_.$emit(GeolocationEventType.ERROR, null);
return;
}
// stop tracking if the position is close to the center of the map.
const view = this.map.getView();
const center = view.getCenter();
if (!center) {
throw new Error('Missing center');
}
const diff = Math.abs(currentPosition[0] - center[0]) + Math.abs(currentPosition[1] - center[1]);
if (diff < 2) {
this.untrack_();
} else {
// immediately recenter to the latest position to avoid a delay if the GPS device is slow to respond.
view.setCenter(currentPosition);
this.untrack_();
this.track_();
}
} else {
this.track_();
}
};
Controller.prototype.track_ = function () {
this.featureOverlay_.addFeature(this.positionFeature_);
this.featureOverlay_.addFeature(this.accuracyFeature_);
this.follow_ = true;
this.geolocation_.setTracking(true);
};
Controller.prototype.untrack_ = function () {
this.featureOverlay_.clear();
this.follow_ = false;
this.geolocation_.setTracking(false);
this.notification_.clear();
};
Controller.prototype.setPosition_ = function () {
const view = this.map.getView();
const position = this.geolocation_.getPosition();
if (position === undefined) {
throw new Error('Missing position');
}
const point = new olGeomPoint(position);
this.positionFeature_.setGeometry(point);
const accuracy = this.accuracyFeature_.getGeometry();
if (this.follow_) {
this.viewChangedByMe_ = true;
if (this.options.zoom || this.options.zoom === 0) {
view.setCenter(position);
view.setZoom(this.options.zoom);
} else if (accuracy instanceof Polygon) {
const size = this.map.getSize();
if (size === undefined) {
throw new Error('Missing size');
}
view.fit(accuracy, {size});
}
this.viewChangedByMe_ = false;
}
};
/**
* @param {Event|import("ol/events/Event.js").default} event Event.
*/
Controller.prototype.handleViewChange_ = function (event) {
if (this.follow_ && !this.viewChangedByMe_) {
this.follow_ = false;
}
};
// Orientation control events
Controller.prototype.autoRotateListener = function () {
let currentAlpha = 0;
if (window.hasOwnProperty('ondeviceorientationabsolute')) {
window.addEventListener(
'deviceorientationabsolute',
(event) => {
if (!(event instanceof DeviceOrientationEvent)) {
throw new Error('Wrong event type');
}
if (event.alpha !== null) {
currentAlpha = this.handleRotate_(event.alpha, currentAlpha);
}
},
true
);
} else if (window.hasOwnProperty('ondeviceorientation')) {
window.addEventListener(
'deviceorientation',
(evt) => {
// @ts-ignore: ios only
if (evt.webkitCompassHeading) {
// check for iOS property
// @ts-ignore: ios only
currentAlpha = this.handleRotate_(-evt.webkitCompassHeading, currentAlpha);
} else {
// non iOS
if (!evt.alpha) {
throw new Error('Missing evt.alpha');
}
currentAlpha = this.handleRotate_(evt.alpha - 270, currentAlpha);
}
},
true
);
} else {
console.error('Orientation is not supported on this device');
}
};
/**
* Handle rotation.
* @param {number} eventAlpha
* @param {number} currentAlpha
* @return {number}
*/
Controller.prototype.handleRotate_ = function (eventAlpha, currentAlpha) {
if (this.geolocation_.getTracking() && Math.abs(eventAlpha - currentAlpha) > 0.2) {
currentAlpha = eventAlpha;
const radAlpha = (currentAlpha * Math.PI) / 180;
this.map.getView().animate({
rotation: radAlpha,
duration: 350,
easing: olEasing.linear,
});
}
return currentAlpha;
};
myModule.controller('ngeoGeolocationController', Controller);
export default myModule;