-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgeolocation.js
More file actions
49 lines (43 loc) · 1.27 KB
/
geolocation.js
File metadata and controls
49 lines (43 loc) · 1.27 KB
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
Geolocation = function() {
if (Geolocation.prototype.instance)
return Geolocation.prototype.instance;
Geolocation.prototype.instance = this;
this.lat = null;
this.lng = null;
this.error = null;
this.maximumAge = 3000;
this.timeout = 10000;
};
Geolocation.getInstance = function() {
var geoInstance = new Geolocation();
geoInstance.localize();
return geoInstance;
};
Geolocation.prototype.localize = function() {
var self = this;
if (!navigator.geolocation) {
self.error = "Geolocation is not supported by browser";
return self;
}
navigator.geolocation.getCurrentPosition(onSuccess, onError, {
maximumAge: this.maximumAge,
timeout: this.timeout,
enableHighAccuracy: true
});
function onSuccess(position) {
self.lat = position.coords.latitude;
self.lng = position.coords.longitude;
self.error = null;
}
function onError(error) {
if (error.code === error.TIMEOUT) {
navigator.geolocation.getCurrentPosition(onSuccess, onError, {
maximumAge: this.maximumAge,
timeout: 2 * this.timeout,
enableHighAccuracy: false
});
}
self.error = error;
}
return this;
};