Skip to content

Commit f997f3d

Browse files
authored
Merge pull request #162 from mobily/geolocation-bindings
Add `Geolocation` bindings
2 parents 095c6e1 + 4b2147d commit f997f3d

File tree

7 files changed

+378
-4
lines changed

7 files changed

+378
-4
lines changed

RNTester/android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
<uses-permission android:name="android.permission.INTERNET" />
77
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
8+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
89

910
<uses-sdk
1011
android:minSdkVersion="16"

RNTester/src/ExampleList.re

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ type item = {
33
displayName: string,
44
title: string,
55
description: string,
6-
examples: array(Example.t)
6+
examples: array(Example.t),
77
};
88

99
module type ExampleT = {
@@ -19,7 +19,7 @@ module MakeExample = (Example: ExampleT) => {
1919
displayName: Example.displayName,
2020
title: Example.title,
2121
description: Example.description,
22-
examples: Example.examples
22+
examples: Example.examples,
2323
};
2424
};
2525

@@ -33,10 +33,13 @@ module ImageBackground = MakeExample(ImageBackgroundExample);
3333

3434
module NetInfo = MakeExample(NetInfoExample);
3535

36+
module Geolocation = MakeExample(GeolocationExample);
37+
3638
let components: array(item) = [|
3739
Button.item("ButtonExample"),
3840
View.item("ViewExample"),
3941
WebView.item("WebViewExample"),
4042
ImageBackground.item("ImageBackground"),
41-
NetInfo.item("NetInfo")
43+
NetInfo.item("NetInfo"),
44+
Geolocation.item("Geolocation"),
4245
|];
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
open BsReactNative;
2+
3+
module GeolocationGetCurrentPositionExample = {
4+
type coords = {
5+
latitude: float,
6+
longitude: float,
7+
};
8+
type action =
9+
| UpdateCoords(coords);
10+
type state = {coords};
11+
let getCurrentPosition = (_event, {ReasonReact.send}) =>
12+
Geolocation.getCurrentPosition(
13+
position =>
14+
send(
15+
UpdateCoords({
16+
latitude: position##coords##latitude,
17+
longitude: position##coords##longitude,
18+
}),
19+
),
20+
error => Js.log(error),
21+
);
22+
let component =
23+
ReasonReact.reducerComponent("GeolocationGetCurrentPositionExample");
24+
let make = _children => {
25+
...component,
26+
initialState: () => {
27+
coords: {
28+
latitude: 0.0,
29+
longitude: 0.0,
30+
},
31+
},
32+
reducer: (action, _state) =>
33+
switch (action) {
34+
| UpdateCoords(coords) => Update({coords: coords})
35+
},
36+
render: ({state, handle}) =>
37+
Style.(
38+
<View>
39+
<View style=(style([padding(Pt(10.))]))>
40+
<TouchableOpacity onPress=(handle(getCurrentPosition))>
41+
<Text>
42+
(ReasonReact.stringToElement("Get current position"))
43+
</Text>
44+
</TouchableOpacity>
45+
<Text>
46+
(
47+
ReasonReact.stringToElement(
48+
"latitude:" ++ string_of_float(state.coords.latitude),
49+
)
50+
)
51+
</Text>
52+
<Text>
53+
(
54+
ReasonReact.stringToElement(
55+
"longitude:" ++ string_of_float(state.coords.longitude),
56+
)
57+
)
58+
</Text>
59+
</View>
60+
</View>
61+
),
62+
};
63+
};
64+
65+
module GeolocationWatchPositionExample = {
66+
type coords = {
67+
latitude: float,
68+
longitude: float,
69+
};
70+
type action =
71+
| UpdateCoords(coords);
72+
type state = {coords};
73+
let component =
74+
ReasonReact.reducerComponent("GeolocationWatchPositionExample");
75+
let make = _children => {
76+
...component,
77+
initialState: () => {
78+
coords: {
79+
latitude: 0.0,
80+
longitude: 0.0,
81+
},
82+
},
83+
reducer: (action, _state) =>
84+
switch (action) {
85+
| UpdateCoords(coords) => Update({coords: coords})
86+
},
87+
subscriptions: ({send}) => [
88+
Sub(
89+
() =>
90+
Geolocation.watchPosition(
91+
position =>
92+
send(
93+
UpdateCoords({
94+
latitude: position##coords##latitude,
95+
longitude: position##coords##longitude,
96+
}),
97+
),
98+
error => Js.log(error),
99+
),
100+
Geolocation.clearWatch,
101+
),
102+
],
103+
render: ({state}) =>
104+
Style.(
105+
<View>
106+
<View style=(style([padding(Pt(10.))]))>
107+
<Text>
108+
(
109+
ReasonReact.stringToElement(
110+
"latitude:" ++ string_of_float(state.coords.latitude),
111+
)
112+
)
113+
</Text>
114+
<Text>
115+
(
116+
ReasonReact.stringToElement(
117+
"longitude:" ++ string_of_float(state.coords.longitude),
118+
)
119+
)
120+
</Text>
121+
</View>
122+
</View>
123+
),
124+
};
125+
};
126+
127+
let title = "<GeolocationExample>";
128+
129+
let description = "GeolocationExample";
130+
131+
let displayName = "GeolocationExample";
132+
133+
let examples: array(Example.t) = [|
134+
{
135+
title: "Geolocation.getCurrentPosition(success, error, options)",
136+
description: None,
137+
render: () => <GeolocationGetCurrentPositionExample />,
138+
},
139+
{
140+
title: "Geolocation.watchPosition(success, error, options)",
141+
description: None,
142+
render: () => <GeolocationWatchPositionExample />,
143+
},
144+
|];

STATUS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
- [x] DatePickerAndroid
5959
- [x] Dimensions
6060
- [x] Easing
61-
- [ ] Geolocation
61+
- [x] Geolocation
6262
- [ ] ImageEditor
6363
- [ ] ImagePickerIOS
6464
- [ ] ImageStore

lib/js/src/geolocation.js

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/geolocation.re

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
type geolactionConfig;
2+
3+
type currentPositionConfig;
4+
5+
type watchPositionConfig;
6+
7+
type watchId = int;
8+
9+
type position = {
10+
.
11+
"coords": coords,
12+
"timestamp": float,
13+
}
14+
and coords = {
15+
.
16+
"speed": int,
17+
"longitude": float,
18+
"latitude": float,
19+
"accuracy": int,
20+
"heading": int,
21+
};
22+
23+
type error = {
24+
.
25+
"code": int,
26+
"message": string,
27+
};
28+
29+
[@bs.obj]
30+
external makeGeolocationConfig :
31+
(~skipPermissionRequests: Js.boolean=?) => geolactionConfig =
32+
"";
33+
34+
[@bs.obj]
35+
external makeCurrentPositionConfig :
36+
(~timeout: int=?, ~maximumAge: int=?, ~enableHighAccuracy: Js.boolean=?) =>
37+
currentPositionConfig =
38+
"";
39+
40+
[@bs.obj]
41+
external makeWatchPositionConfig :
42+
(
43+
~timeout: int=?,
44+
~maximumAge: int=?,
45+
~enableHighAccuracy: Js.boolean=?,
46+
~distanceFilter: int=?,
47+
~useSignificantChanges: Js.boolean=?
48+
) =>
49+
watchPositionConfig =
50+
"";
51+
52+
[@bs.val] [@bs.scope ("navigator", "geolocation")]
53+
external setRNConfiguration : geolactionConfig => unit = "";
54+
55+
let setRNConfiguration = (~skipPermissionRequests=?, ()) =>
56+
setRNConfiguration(makeGeolocationConfig(~skipPermissionRequests?));
57+
58+
[@bs.val] [@bs.scope ("navigator", "geolocation")]
59+
external requestAuthorization : unit => unit = "";
60+
61+
[@bs.val] [@bs.scope ("navigator", "geolocation")]
62+
external stopObserving : unit => unit = "";
63+
64+
[@bs.val] [@bs.scope ("navigator", "geolocation")]
65+
external getCurrentPosition :
66+
(position => unit, error => unit, currentPositionConfig) => unit =
67+
"";
68+
69+
let getCurrentPosition =
70+
(~timeout=?, ~maximumAge=?, ~enableHighAccuracy=?, success, error) =>
71+
getCurrentPosition(
72+
success,
73+
error,
74+
makeCurrentPositionConfig(~timeout?, ~maximumAge?, ~enableHighAccuracy?),
75+
);
76+
77+
[@bs.val] [@bs.scope ("navigator", "geolocation")]
78+
external watchPosition :
79+
(position => unit, error => unit, watchPositionConfig) => watchId =
80+
"";
81+
82+
let watchPosition =
83+
(
84+
~timeout=?,
85+
~maximumAge=?,
86+
~enableHighAccuracy=?,
87+
~distanceFilter=?,
88+
~useSignificantChanges=?,
89+
success,
90+
error,
91+
) =>
92+
watchPosition(
93+
success,
94+
error,
95+
makeWatchPositionConfig(
96+
~timeout?,
97+
~maximumAge?,
98+
~enableHighAccuracy?,
99+
~distanceFilter?,
100+
~useSignificantChanges?,
101+
),
102+
);
103+
104+
[@bs.val] [@bs.scope ("navigator", "geolocation")]
105+
external clearWatch : watchId => unit = "";

0 commit comments

Comments
 (0)