-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeoLocation.ts
74 lines (71 loc) · 1.75 KB
/
GeoLocation.ts
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
import { Angle } from "./Angle";
/**
* Name: LatLng
* Purpose: Type definition for geographic coordinates
*/
type LatLng = {
lat: number;
lng: number;
};
type LatLngCallback = (position: LatLng) => void;
/**
* Name: GeoLocation
* Purpose: Use navigator.geolocation to get latitude and longitude
*/
class GeoLocation {
public position: LatLng;
protected status: number;
public onset: LatLngCallback;
constructor() {
this.position = {lat:0,lng:0};
this.status = 2;
this.onset = function(position: LatLng){alert("GeoLocation.onset({ lat:"+position.lat+", lng:"+position.lng+"})")};
}
public reset() {
this.position = {lat:0,lng:0};
this.status = 2;
}
protected coords2latLng: PositionCallback = (pos: GeolocationPosition) => {
this.position = {lat: pos.coords.latitude, lng:pos.coords.longitude};
this.status = 0;
this.onset(this.position);
}
public isLocationResponse() {
return this.status?false:true;
}
public isLocationResponseWaiting() {
return (this.status==1)?true:false;
}
public getPosition() {
return this.position;
}
public setPosition(pos: LatLng) {
this.position.lat = pos.lat;
this.position.lng = pos.lng;
this.status = 2;
}
public getPositionAngle() {
return {lat:new Angle(this.position.lat),lng:new Angle(this.position.lng)};
}
public setPositionAngle(pos: any) {
this.position.lat = pos.lat.dec;
this.position.lng = pos.lng.dec;
this.status = 2;
}
public getLocation() {
switch (this.status) {
case 2:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(this.coords2latLng);
} else {
throw "Geolocation is not supported by this browser.";
}
this.status = 1;
break;
case 1:
case 0:
break;
}
return this.status;
}
}