-
Notifications
You must be signed in to change notification settings - Fork 6
/
RouteBetweenPOIs.tsx
89 lines (80 loc) · 2.62 KB
/
RouteBetweenPOIs.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
import React, {useEffect, useState} from 'react';
import {ScrollView, Text} from 'react-native';
import SitumPlugin, {Building, Poi} from '@situm/react-native';
import {SITUM_BUILDING_ID} from '../../situm';
import styles from '../styles/styles';
import {fetchBuilding} from '../Utils/CommonFetchs';
import {Card} from 'react-native-paper';
export const RouteBetweenPOIs = () => {
const [building, setBuilding] = useState<Building>();
const [indoorPOIs, setIndoorPOIs] = useState<Poi[]>();
const [route, setRoute] = useState<any>();
const [error, setError] = useState('');
const populatePOIsFromBuilding = async () => {
if (!building) {
return;
}
await SitumPlugin.fetchIndoorPOIsFromBuilding(building)
.then(_indoorPois => {
console.log(JSON.stringify(_indoorPois, null, 2));
setIndoorPOIs(_indoorPois);
})
.catch(e => {
console.error(`Situm > example > Could not fetch indoor POIs ${e}`);
});
};
const requestDirections = async () => {
//check if we have 2 pois at least
if ((indoorPOIs && indoorPOIs.length < 2) || !building) {
console.error('Situm > example > Your building has less than two POIs');
return;
}
await SitumPlugin.requestDirections(
building,
indoorPOIs![0],
indoorPOIs![1],
)
.then(_route => {
console.log(JSON.stringify(_route, null, 2));
setRoute(_route);
})
.catch(e => {
console.error(`Situm > example > Could not compute route ${e}`);
setError(e);
});
};
useEffect(() => {
// first load the building
fetchBuilding(SITUM_BUILDING_ID)
.then(setBuilding)
.catch(e => {
console.error(`Situm > example > Coult not fetch building ${e}`);
});
}, []);
useEffect(() => {
// later get pois
building && populatePOIsFromBuilding();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [building]);
useEffect(() => {
//finaly ask for directions
building && indoorPOIs && requestDirections();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [building, indoorPOIs]);
return (
<ScrollView style={{...styles.screenWrapper}}>
<Card mode="contained" style={styles.margin}>
<Card.Title title="Route" />
<Card.Content>
{indoorPOIs && indoorPOIs.length > 0 && (
<Text>
Showing route from '{indoorPOIs[0].poiName}' to '
{indoorPOIs[1].poiName}'
</Text>
)}
<Text style={styles.text}>{JSON.stringify(route) || error}</Text>
</Card.Content>
</Card>
</ScrollView>
);
};