1
1
module Geolocation
2
- ( Position , Coords
3
- , currentPosition
4
- , watchPosition, clearWatch
5
- , Options , defaultOptions
2
+ ( Location
3
+ , current
4
+ , subscribe
5
+ , unsubscribe
6
+ , Options
7
+ , defaultOptions
6
8
, Error (..)
7
- ) where
9
+ )
10
+ where
8
11
9
- import Native.Geolocation
10
- import Promise (Promise )
11
- import Time (Time )
12
+ {- | Primitive bindings to the web's [Geolocation API][geo]. You probably want
13
+ to use something higher-level than this, like the elm-effects API for
14
+ geolocation.
15
+
16
+ [geo]: https://developer.mozilla.org/en-US/docs/Web/API/Geolocation
12
17
13
- {- |
18
+ # Location
19
+ @docs Location
14
20
15
- # Current Position
16
- @docs currentPosition, Options, defaultOptions, Position, Coords, Error
21
+ # Requesting a Location
22
+ @docs current, subscribe, unsubscribe
17
23
18
- # Watch Position Over Time
19
- @docs watchPosition, clearWatch
24
+ # Options
25
+ @docs Options, defaultOptions
26
+
27
+ # Errors
28
+ @docs Error
20
29
21
30
-}
22
31
23
- type alias Position =
24
- { coords : Coords
25
- , timestamp : Time
26
- }
32
+
33
+ import Native.Geolocation
34
+ import Task exposing ( Task )
35
+ import Time exposing ( Time )
27
36
28
37
29
- {- | Coordinate data provides as much information as possible for a given
30
- device.
38
+ {- | All available details of the device's current location in the world.
31
39
32
40
* `latitude` — the latitude in decimal degrees.
33
41
* `longitude` — the longitude in decimal degrees.
34
- * `altitude` — the altitude in meters, relative to sea level, if the
35
- implementation cannot provide the data.
36
- * `accuracy` — the accuracy of the latitude and longitude properties,
37
- expressed in meters.
38
- * `altitudeAccuracy` — the accuracy of the altitude expressed in
39
- meters, if available.
40
- * `heading` — the direction in which the device is traveling, if
41
- available. This value, specified in degrees, indicates how far off from
42
- heading due north the device is. 0 degrees represents true north, 90
43
- degrees is east, 270 degrees is west, and everything in between. If speed
44
- is 0, heading is NaN.
42
+ * `accuracy` — the accuracy of the latitude and longitude, expressed in meters.
43
+ * `altitude` — the altitude in meters relative to sea level, if available.
44
+ * `altitudeAccuracy` — the accuracy of the altitude expressed in meters, if available.
45
45
* `speed` — the velocity of the device in meters per second, if available.
46
+ * `heading` — the direction in which the device is traveling, if available.
47
+ This value, specified in degrees, indicates how far off from heading due north
48
+ the device is. 0 degrees represents true north, 90 degrees is east, 270 degrees
49
+ is west, and everything in between. If speed is 0, heading is NaN.
50
+ * `timestamp` — the time that this location reading was taken in milliseconds.
46
51
-}
47
- type alias Coords =
52
+ type alias Location =
48
53
{ latitude : Float
49
54
, longitude : Float
50
- , altitude : Maybe Float
51
55
, accuracy : Float
56
+ , altitude : Maybe Float
52
57
, altitudeAccuracy : Maybe Float
53
- , heading : Maybe Float
54
58
, speed : Maybe Float
59
+ , heading : Maybe Float
60
+ , timestamp : Time
55
61
}
56
62
57
63
64
+ {- | The `current` and `subscribe` functions may fail for a variaty of reasons.
65
+
66
+ * The user may reject the request to use their location.
67
+ * It may be impossible to get a location.
68
+ * If you set a timeout in the `Options` the request may just take to long.
69
+
70
+ In each case, the browser will provide a string with additional information.
71
+ -}
58
72
type Error
59
73
= PermissionDenied String
60
- | PositionUnavailable String
74
+ | LocationUnavailable String
61
75
| Timeout String
62
76
63
77
64
- currentPosition : Options -> Promise Error Position
65
- currentPosition =
66
- Native . Geolocation . currentPosition
78
+ {- | Request the current position of the user's device. On the first request,
79
+ the user will need to give permission to access this information. A typical
80
+ request would look like this:
67
81
82
+ current defaultOptions
83
+ -}
84
+ current : Options -> Task Error Location
85
+ current =
86
+ Native . Geolocation . current
68
87
69
- watchPosition : Options -> (Position -> Promise x a ) -> (Error -> Promise y b ) -> Promise z Int
70
- watchPosition =
71
- Native . Geolocation . watchPosition
72
88
89
+ {- | Subscribe to changes in the device's position. You provide two callbacks.
90
+ One for when a location has been successfully reported, and another for when
91
+ there has been some sort of problem.
73
92
74
- clearWatch : Int -> Promise x ()
75
- clearWatch =
76
- Native . Geolocation . clearWatch
93
+ When you run the task to create a subscription, you will get back an integer
94
+ that uniquely identifies this subscription. You can give that identifier to
95
+ `unsubscribe` to stop getting updates.
96
+ -}
97
+ subscribe : Options -> (Location -> Task x a ) -> (Error -> Task y b ) -> Task z Int
98
+ subscribe =
99
+ Native . Geolocation . subscribe
77
100
78
101
102
+ {- | Each subscription is uniquely identified by an integer. You can cancel a
103
+ subscription by giving that integer to `unsubscribe`.
104
+ -}
105
+ unsubscribe : Int -> Task x ()
106
+ unsubscribe =
107
+ Native . Geolocation . unsubscribe
108
+
109
+
110
+ {- | There are a couple options you can mess with when requesting location data.
111
+
112
+ * `enableHighAccuracy` — When enabled, the device will attempt to provide
113
+ a more accurate location. This can result in slower response times or
114
+ increased power consumption (with a GPS chip on a mobile device for example).
115
+ When disabled, the device can take the liberty to save resources by responding
116
+ more quickly and/or using less power.
117
+ * `timeout` — Requesting a location can take time, so you have the option
118
+ to provide an upper bound in milliseconds on that wait.
119
+ * `maximumAge` — This API can return cached locations. If this is set
120
+ to `Just 400` you may get cached locations as long as they were read in the
121
+ last 400 milliseconds. If this is `Nothing` then the device must attempt
122
+ to retrieve the current location every time.
123
+ -}
79
124
type alias Options =
80
125
{ enableHighAccuracy : Bool
81
126
, timeout : Maybe Int
82
127
, maximumAge : Maybe Int
83
128
}
84
129
85
130
131
+ {- | The options you will want in 99% of cases. This will get you faster
132
+ results, less battery drain, no surprise failures due to timeouts, and no
133
+ surprising cached results.
134
+
135
+ { enableHighAccuracy = False
136
+ , timeout = Nothing
137
+ , maximumAge = Nothing
138
+ }
139
+ -}
86
140
defaultOptions : Options
87
141
defaultOptions =
88
142
{ enableHighAccuracy = False
89
143
, timeout = Nothing
90
144
, maximumAge = Nothing
91
- }
145
+ }
0 commit comments