-
Notifications
You must be signed in to change notification settings - Fork 6
/
Wayfinding.tsx
162 lines (143 loc) · 4.65 KB
/
Wayfinding.tsx
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import React, {useEffect, useState, useRef} from 'react';
import {
AppState,
NativeEventSubscription,
SafeAreaView,
StyleSheet,
useColorScheme,
} from 'react-native';
import {Colors} from 'react-native/Libraries/NewAppScreen';
import SitumPlugin, {MapView, SitumProvider, Error} from '@situm/react-native';
import type {
OnPoiDeselectedResult,
OnPoiSelectedResult,
OnExternalLinkClickedResult,
MapViewRef,
} from '@situm/react-native';
import {SITUM_API_KEY, SITUM_BUILDING_ID} from '../../situm';
import requestPermission from '../Utils/requestPermission';
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
width: '100%',
height: '100%',
},
mapview: {
width: '100%',
height: '100%',
},
});
const Screen: React.FC = () => {
const mapViewRef = useRef<MapViewRef>(null);
const [_controller, setController] = useState<MapViewRef | null>();
// When coming from background, try to start positioning (if not running yet)
// This will ensure that, if the user enables the app permissions from the phone settings
// your application will start positioning right away
const registerAppStateListener = (): NativeEventSubscription => {
return AppState.addEventListener('change', nextAppState => {
if (nextAppState === 'active') {
if (!SitumPlugin.positioningIsRunning()) {
SitumPlugin.requestLocationUpdates();
console.log(
'Situm > example > Starting positioning after coming from background',
);
}
}
});
};
// Initialize SDK when mounting map
useEffect(() => {
let appStateListener: NativeEventSubscription;
// Set positioning configuration
SitumPlugin.setConfiguration({useRemoteConfig: true});
//Request permissions and start positioning
requestPermission()
.then(() => {
if (!SitumPlugin.positioningIsRunning()) {
SitumPlugin.requestLocationUpdates();
console.log('Situm > example > Starting positioning');
}
})
.catch(e => {
console.log(`Situm > example > Permissions rejected: ${e}`);
})
.finally(() => {
//Register listener to react to the app comming from the background
appStateListener = registerAppStateListener();
//Register callbacks
registerCallbacks();
});
// When unmounting make sure to stop positioning and remove listeners
return () => {
SitumPlugin.removeLocationUpdates();
appStateListener.remove();
};
}, []);
// Register callbacks to handle Situm SDK events
const registerCallbacks = () => {
// Handle location errors
SitumPlugin.onLocationError((err: Error) => {
console.error(
'Situm > example > Error while positioning: ',
JSON.stringify(err),
);
// Please take a look to RemoteConfig.tsx to know what kind of errors
// your app may be able to react to (and other useful callbacks as well)
// E.g. you might want to inform the user that he/she needs to grant some permission
// based on the callbacks' result
});
};
// Initialize controller
useEffect(() => {
if (!mapViewRef) {
return;
}
setController(mapViewRef.current);
}, [mapViewRef]);
const onLoad = (event: any) => {
console.log('Situm > example > Map is ready, received event: ', event);
};
const onPoiSelected = (event: OnPoiSelectedResult) => {
console.log(
'Situm > example > on poi selected detected: ' + JSON.stringify(event),
);
};
const onPoiDeselected = (event: OnPoiDeselectedResult) => {
console.log(
'Situm > example > on poi deselected detected: ' + JSON.stringify(event),
);
};
const onExternalLinkClicked = (event: OnExternalLinkClickedResult) => {
// MapView will open the external link in the system's default browser if this callback is not set.
console.log('Situm > example > click on external link: ' + event.url);
};
return (
<MapView
ref={mapViewRef}
configuration={{
buildingIdentifier: SITUM_BUILDING_ID,
situmApiKey: SITUM_API_KEY,
}}
onLoad={onLoad}
onPoiSelected={onPoiSelected}
onPoiDeselected={onPoiDeselected}
onExternalLinkClicked={onExternalLinkClicked}
/>
);
};
const App: React.FC = () => {
const isDarkMode = useColorScheme() === 'dark';
const backgroundStyle = {
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
};
return (
<SitumProvider apiKey={SITUM_API_KEY}>
<SafeAreaView style={{...styles.container, ...backgroundStyle}}>
<Screen />
</SafeAreaView>
</SitumProvider>
);
};
export default App;