-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
ExpoLocation.web.ts
108 lines (99 loc) · 2.73 KB
/
ExpoLocation.web.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
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
import { EventEmitter } from 'expo-core';
interface Coordinates {
latitude: number,
longitude: number,
altitude?: number,
accuracy?: number,
altitudeAccuracy?: number,
heading?: number,
speed?: number,
};
interface Position {
coords: Coordinates,
timestamp: number,
};
interface PermissionResult {
status: string,
};
class GeocoderError extends Error {
code: string;
constructor() {
super('Geocoder service is not available for this device.');
this.code = 'E_NO_GEOCODER';
}
}
const emitter = new EventEmitter({} as any);
function positionToJSON(position: any): Position | null {
if (!position) return null;
const { coords = {}, timestamp } = position;
return {
coords: {
latitude: coords.latitude,
longitude: coords.longitude,
altitude: coords.altitude,
accuracy: coords.accuracy,
altitudeAccuracy: coords.altitudeAccuracy,
heading: coords.heading,
speed: coords.speed,
},
timestamp,
};
}
export default {
get name(): string {
return 'ExpoLocation';
},
async getProviderStatusAsync(): Promise<{ locationServicesEnabled: boolean }> {
return {
locationServicesEnabled: 'geolocation' in navigator,
};
},
async getCurrentPositionAsync(options: Object): Promise<Position | null> {
return new Promise<Position | null>((resolve, reject) =>
navigator.geolocation.getCurrentPosition(
position => resolve(positionToJSON(position)),
reject,
options
)
);
},
async removeWatchAsync(watchId): Promise<void> {
navigator.geolocation.clearWatch(watchId);
},
async watchDeviceHeading(headingId): Promise<void> {
console.warn('Location.watchDeviceHeading: is not supported on web');
},
async geocodeAsync(): Promise<Array<any>> {
throw new GeocoderError();
},
async reverseGeocodeAsync(): Promise<Array<any>> {
throw new GeocoderError();
},
async watchPositionImplAsync(watchId: string, options: Object): Promise<string> {
return new Promise<string>(resolve => {
// @ts-ignore
watchId = global.navigator.geolocation.watchPosition(
location => {
emitter.emit('Exponent.locationChanged', { watchId, location: positionToJSON(location) });
},
null,
options
);
resolve(watchId);
});
},
async requestPermissionsAsync(): Promise<PermissionResult> {
return new Promise<PermissionResult>(resolve => {
navigator.geolocation.getCurrentPosition(
() => resolve({ status: 'granted' }),
({ code }) => {
if (code === 1 /* PERMISSION_DENIED */) {
resolve({ status: 'denied' });
} else {
resolve({ status: 'undetermined' });
}
}
);
});
},
};