forked from gregsramblings/getAccurateCurrentPosition
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Leaflet.AccuratePosition.js
133 lines (107 loc) · 3.67 KB
/
Leaflet.AccuratePosition.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
/**
* Leaflet.AccuratePosition aims to provide an accurate device location when simply calling map.locate() doesn’t.
* https://github.com/m165437/Leaflet.AccuratePosition
*
* Greg Wilson's getAccurateCurrentPosition() forked to be a Leaflet plugin
* https://github.com/gwilson/getAccurateCurrentPosition
*
* Copyright (C) 2013 Greg Wilson, 2014 Michael Schmidt-Voigt
*/
L.Map.include({
_defaultAccuratePositionOptions: {
maxWait: 10000,
desiredAccuracy: 20
},
findAccuratePosition: function (options) {
if (!navigator.geolocation) {
this._handleAccuratePositionError({
code: 0,
message: 'Geolocation not supported.'
});
return this;
}
this._accuratePositionEventCount = 0;
this._accuratePositionOptions = L.extend(this._defaultAccuratePositionOptions, options);
this._accuratePositionOptions.enableHighAccuracy = true;
this._accuratePositionOptions.maximumAge = 0;
if (!this._accuratePositionOptions.timeout)
this._accuratePositionOptions.timeout = this._accuratePositionOptions.maxWait;
var onResponse = L.bind(this._checkAccuratePosition, this),
onError = L.bind(this._handleAccuratePositionError, this),
onTimeout = L.bind(this._handleAccuratePositionTimeout, this);
this._accuratePositionWatchId = navigator.geolocation.watchPosition(
onResponse,
onError,
this._accuratePositionOptions);
this._accuratePositionTimerId = setTimeout(
onTimeout,
this._accuratePositionOptions.maxWait);
},
_handleAccuratePositionTimeout: function() {
navigator.geolocation.clearWatch(this._accuratePositionWatchId);
if (typeof this._lastCheckedAccuratePosition !== 'undefined') {
this._handleAccuratePositionResponse(this._lastCheckedAccuratePosition);
} else {
this._handleAccuratePositionError({
code: 3,
message: 'Timeout expired'
});
}
return this;
},
_cleanUpAccuratePositioning: function () {
clearTimeout(this._accuratePositionTimerId);
navigator.geolocation.clearWatch(this._accuratePositionWatchId);
},
_checkAccuratePosition: function (pos) {
var accuracyReached = pos.coords.accuracy <= this._accuratePositionOptions.desiredAccuracy;
this._lastCheckedAccuratePosition = pos;
this._accuratePositionEventCount = this._accuratePositionEventCount + 1;
if (accuracyReached && (this._accuratePositionEventCount > 1)) {
this._cleanUpAccuratePositioning();
this._handleAccuratePositionResponse(pos);
} else {
this._handleAccuratePositionProgress(pos);
}
},
_prepareAccuratePositionData: function (pos) {
var lat = pos.coords.latitude,
lng = pos.coords.longitude,
latlng = new L.LatLng(lat, lng),
latAccuracy = 180 * pos.coords.accuracy / 40075017,
lngAccuracy = latAccuracy / Math.cos(Math.PI / 180 * lat),
bounds = L.latLngBounds(
[lat - latAccuracy, lng - lngAccuracy],
[lat + latAccuracy, lng + lngAccuracy]);
var data = {
latlng: latlng,
bounds: bounds,
timestamp: pos.timestamp
};
for (var i in pos.coords) {
if (typeof pos.coords[i] === 'number') {
data[i] = pos.coords[i];
}
}
return data;
},
_handleAccuratePositionProgress: function (pos) {
var data = this._prepareAccuratePositionData(pos);
this.fire('accuratepositionprogress', data);
},
_handleAccuratePositionResponse: function (pos) {
var data = this._prepareAccuratePositionData(pos);
this.fire('accuratepositionfound', data);
},
_handleAccuratePositionError: function (error) {
var c = error.code,
message = error.message ||
(c === 1 ? 'permission denied' :
(c === 2 ? 'position unavailable' : 'timeout'));
this._cleanUpAccuratePositioning();
this.fire('accuratepositionerror', {
code: c,
message: 'Geolocation error: ' + message + '.'
});
}
});