-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
244 lines (215 loc) · 6.24 KB
/
App.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import React, {useRef, useState} from 'react';
import {SafeAreaView, Text, View, StyleSheet, Button} from 'react-native';
import MapView, {Callout, Marker} from 'react-native-maps';
import DemoButtons from './Components/DemoButtons';
import TopText from './Components/TopText';
import {MapStyle} from './Data/GoogleMapStyle';
const TEST_LOCATIONS = {
BUCKINGHAM_PALACE: {
latitude: 51.50206,
longitude: -0.140042,
latitudeDelta: 0.015,
longitudeDelta: 0.0121,
},
BIG_BEN: {
latitude: 51.500914,
longitude: -0.124703,
latitudeDelta: 0.015,
longitudeDelta: 0.0121,
},
LONDON_EYE: {
latitude: 51.503301,
longitude: -0.119369,
latitudeDelta: 0.015,
longitudeDelta: 0.0121,
},
};
const App = () => {
// the list of marker details to be rendered inside the map
const [mapMarkers, setMapMarkers] = useState([
{
coordinate: {
latitude: TEST_LOCATIONS.BUCKINGHAM_PALACE.latitude,
longitude: TEST_LOCATIONS.BUCKINGHAM_PALACE.longitude,
},
callout: {
text: `lat: ${TEST_LOCATIONS.BUCKINGHAM_PALACE.latitude}, lon: ${TEST_LOCATIONS.BUCKINGHAM_PALACE.longitude}`,
},
},
]);
// the current camera position
const [cameraPosition, setCameraPosition] = useState({
center: {
latitude: TEST_LOCATIONS.BUCKINGHAM_PALACE.latitude,
longitude: TEST_LOCATIONS.BUCKINGHAM_PALACE.longitude,
},
});
// the current map region. Note this is different from camera position
// we need to track and set this as the map will 'snap' back to here
// which can 'disconnect' it from the camera position
const [mapRegion, setMapRegion] = useState(TEST_LOCATIONS.BUCKINGHAM_PALACE)
const [mapType, setMapType] = useState('standard');
// the map itself, use this to call map methods
const mapRef = useRef(null);
// update the state of the camera position, and animate to it
const moveCameraTo = (latitude, longitude, durationMs = 500) => {
const newCameraPosition = {
...cameraPosition,
center: {
latitude: latitude,
longitude: longitude,
},
};
setCameraPosition(newCameraPosition);
mapRef.current.animateCamera(newCameraPosition, {duration: durationMs});
};
// add a new marker at the given coordinate
const addMarkerAtCoordinate = (latitude, longitude) => {
setMapMarkers(markers => {
return [
...markers,
{
coordinate: {
latitude: latitude,
longitude: longitude,
},
callout: {
text: `lat: ${latitude.toFixed(4)}, lon: ${longitude.toFixed(4)}`,
},
},
];
});
};
// remove a marker at the given coordinate
const removeMarkerAtCoordinate = (latitude, longitude) => {
const newMarkers = [];
mapMarkers.forEach(existingMarkerObject => {
if (
existingMarkerObject.coordinate.latitude != latitude &&
existingMarkerObject.coordinate.longitude != longitude
) {
newMarkers.push(existingMarkerObject);
}
});
setMapMarkers(() => {
return newMarkers;
});
};
// remove all markers
const clearMarkers = () => {
setMapMarkers(() => {
return [];
});
};
// toggle map type between satellite and standard
const toggleMapType = () => {
setMapType(val => {
return val === 'satellite' ? 'standard' : 'satellite'
})
}
/**
* Move the camera between the TEST_LOCATIONs
*/
const moveCameraDemoTimeouts = [];
const moveCameraDemo = () => {
moveCameraDemoTimeouts.map(val => {
clearTimeout(val);
});
moveCameraTo(
TEST_LOCATIONS.BUCKINGHAM_PALACE.latitude,
TEST_LOCATIONS.BUCKINGHAM_PALACE.longitude,
2000,
);
const timeout1 = setTimeout(() => {
moveCameraTo(
TEST_LOCATIONS.BIG_BEN.latitude,
TEST_LOCATIONS.BIG_BEN.longitude,
2000,
);
}, 2000 + 1000);
const timeout2 = setTimeout(() => {
moveCameraTo(
TEST_LOCATIONS.LONDON_EYE.latitude,
TEST_LOCATIONS.LONDON_EYE.longitude,
2000,
);
}, 2000 + 1000 + 2000 + 1000);
moveCameraDemoTimeouts.push(timeout1, timeout2);
};
// MapView onPress event handler
const onPressHandler = event => {
const {latitude, longitude} = event.nativeEvent.coordinate;
addMarkerAtCoordinate(latitude, longitude);
};
// MapView onRegionChangeComplete event handler
const onRegionChangeCompleteHandler = event => {
setMapRegion(event);
}
// helper to render Marker with appropriate props
const renderMarker = newMarkerObject => {
const markerProps = {
key: `${newMarkerObject.coordinate.latitude}:${newMarkerObject.coordinate.longitude}`,
coordinate: newMarkerObject.coordinate,
};
return (
<Marker {...markerProps}>
<Callout
onPress={event => {
const {latitude, longitude} = event.nativeEvent.coordinate;
removeMarkerAtCoordinate(latitude, longitude);
}}>
<Text style={styles.calloutText}>{newMarkerObject.callout.text}</Text>
</Callout>
</Marker>
);
};
return (
<SafeAreaView style={styles.container}>
<View style={styles.textContainer}>
<TopText />
</View>
<View style={styles.mapContainer}>
<MapView
ref={mapRef}
style={styles.map}
customMapStyle={MapStyle}
region={mapRegion}
mapType={mapType}
onRegionChangeComplete={onRegionChangeCompleteHandler}
onPress={onPressHandler}
>
{mapMarkers.map(marker => renderMarker(marker))}
</MapView>
</View>
<View style={styles.buttonsContainer}>
<DemoButtons
onMoveCameraPress={()=>{moveCameraDemo();}}
onClearMarkersPress={()=>{clearMarkers();}}
onToggleMapTypePress={()=>{toggleMapType();}}
/>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
...StyleSheet.absoluteFillObject,
},
mapContainer: {
flex: 8,
},
map: {
...StyleSheet.absoluteFillObject,
},
textContainer: {
flex: 1,
},
buttonsContainer: {
flex: 1,
flexDirection: 'row',
},
calloutText: {
color: 'black',
},
});
export default App;