forked from reggie3/react-native-webview-leaflet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
373 lines (340 loc) · 9.91 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import React from 'react';
import { StyleSheet, Text, View, Platform, Alert } from 'react-native';
import { Constants, Location, Permissions } from 'expo';
import WebViewLeaflet from './WebViewLeaflet';
import testLocations from './web/testLocations';
import Button from './Button';
import mapLayers from './web/mockMapLayers';
const geolib = require('geolib');
const emoji = ['😄', '😃', '⛔', '🎠', '🚓', '🚇'];
const animations = ['bounce', 'fade', 'pulse', 'jump', 'waggle', 'spin'];
let parkLocations = {
dw: [28.417839, -81.563808],
bg: [37.23416573, -76.63999744],
kd: [37.837329984, -77.440331572]
};
const duration = Math.floor(Math.random() * 3) + 1;
const delay = Math.floor(Math.random()) * 0.5;
const interationCount = 'infinite';
export default class App extends React.Component {
constructor() {
super();
this.state = {
location: null,
errorMessage: null,
locations: [...testLocations],
coords: undefined,
showEmojiSelectorModal: false,
mapState: {
showAttributionControl: false,
showZoomControls: false,
panToLocation: false,
zoom: 10
}
};
}
componentDidMount() {
if (Platform.OS === 'android' && !Constants.isDevice) {
this.setState({
errorMessage:
'Oops, this will not work on Sketch in an Android emulator. Try it on your device!'
});
} else {
this._getLocationAsync();
}
}
_getLocationAsync = async () => {
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
this.setState({
errorMessage: 'Permission to access location was denied'
});
}
let location = await Location.getCurrentPositionAsync({});
let locations = this.createRandomMarkers(location.coords, 0, 50000);
// center random markers around Washington DC
// let locations = this.createRandomMarkers({latitude: 38.889931, longitude: -77.009003}, 20, 10000);
this.setState({
locations: [...this.state.locations, ...locations],
location,
// center around Washington DC
// coords: [38.889931, -77.009003]
coords: [location.coords.latitude, location.coords.longitude]
});
};
// create set of location objects centered around the current user location
createRandomMarkers = (center, numberOfMarkers, radius) => {
let newMarkers = [];
for (let i = 0; i < numberOfMarkers; i++) {
// get a random location centered around the current postion
let x0 = center.longitude;
let y0 = center.latitude;
let r = radius / 111300; // = 100 meters
let u = Math.random();
let v = Math.random();
let w = r * Math.sqrt(u);
let t = 2 * Math.PI * v;
let x = w * Math.cos(t);
let y1 = w * Math.sin(t);
let x1 = x / Math.cos(y0);
let foundLatitude = y0 + y1;
let foundLongitude = x0 + x1;
newMarkers.push({
id: i,
// coords: [33.946, -91.000],
coords: [foundLatitude, foundLongitude],
icon: emoji[Math.floor(Math.random() * emoji.length)],
animation: {
name: animations[Math.floor(Math.random() * animations.length)],
duration: Math.floor(Math.random() * 3) + 1,
delay: Math.floor(Math.random()) * 0.5,
interationCount
}
});
}
return newMarkers;
};
componentDidUpdate = (prevProps, prevState) => {
// do these things once the map has loaded
if (!prevState.mapState.mapLoaded && this.state.mapState.mapLoaded) {
this.webViewLeaflet.sendMessage({
zoom: 6,
locations: this.state.locations,
showAttributionControl: this.state.mapState.showAttributionControl,
showZoomControl: this.state.mapState.showZoomControl
});
}
// do these things only if the map has been loaded
if (this.state.mapState.mapLoaded) {
// if the user's location has changed
if (prevState.coords !== this.state.coords) {
this.webViewLeaflet.sendMessage({
centerPosition: this.state.coords,
locations: [
...this.state.locations,
{
id: 0,
coords: this.state.coords,
icon: '❤️',
size: [24, 24],
animation: {
name: 'pulse',
duration: '.5',
delay: 0,
interationCount: 'infinite'
}
}
]
});
}
}
};
updateMarkerSpeed = () => {
// console.log('altering markers');
let updatedLocations = this.state.locations.map((location) => {
let updatedLocation = Object.assign({}, location, {
animation: Object.assign({}, location.animation, {
duration: location.animation.duration + 0.5
})
});
return updatedLocation;
});
this.setState({ locations: updatedLocations });
};
onMapClicked = ({ payload }) => {
console.log(`Map Clicked: app received: ${payload.coords}`);
this.showAlert('Map Clicked', `Coordinates = ${payload.coords}`);
};
onMapMarkerClicked = ({ payload }) => {
console.log(`Marker Clicked: ${payload.id}`);
this.showAlert('Marker Clicked', `Marker ID = ${payload.id}`);
this.setState(
{
clickedMarkerID: payload.id,
locations: this.state.locations.map((location) => {
if (location.id === payload.id) {
return {
...location,
icon: (location.icon = '✖️')
};
}
return location;
})
},
() => {
// send the updated locations
this.webViewLeaflet.sendMessage({
locations: this.state.locations
});
}
);
};
setEmojiForMarker = (emoji) => {
debugger;
};
onCloseEmojiSelectorModal = () => {
this.setState({ showEmojiSelectorModal: false });
};
onOpenEmojiSelectorModal = () => {
this.setState({ showEmojiSelectorModal: true });
};
showAlert = (title, body) => {
Alert.alert(
title,
body,
[{ text: 'OK', onPress: () => console.log('OK Pressed') }],
{ cancelable: false }
);
};
onZoomLevelsChange = (event) => {
console.log('onZoomLevelsChange received : ', event);
};
onResize = (event) => {
console.log('onResize received : ', event);
};
onUnload = (event) => {
console.log('onUnload received : ', event);
};
onViewReset = (event) => {
console.log('onViewReset received : ', event);
};
onLoad = (event) => {
console.log('onLoad received : ', event);
this.setState(
{
...this.state,
mapState: { ...this.state.mapState, mapLoaded: true }
},
() => {
this.webViewLeaflet.sendMessage({
mapLayers
});
}
);
}
onZoomStart = (event) => {
console.log('onZoomEnd received : ', event);
};
onMoveStart = (event) => {
console.log('onMoveStart received : ', event);
};
onZoom = (event) => {
console.log('onZoom received : ', event);
};
onMove = (event) => {
console.log('onMove received : ', event);
};
onZoomEnd = (event) => {
console.log('onZoomEnd received : ', event);
};
onMoveEnd = (event) => {
console.log('onMoveEnd received : ', event);
};
onCurrentPositionClicked = () => {
console.log('onCurrentPositionClicked received');
};
centerMap = (parkInitials) => {
console.log(parkInitials);
switch (parkInitials) {
case 'dw':
this.setState({ coords: parkLocations.dw });
break;
case 'bg':
this.setState({ coords: parkLocations.bg });
break;
case 'kd':
this.setState({ coords: parkLocations.kd });
break;
}
};
setBoundsForAllMarkers = () => {
let boundsArray = this.state.locations.map((location) => {
return {
latitude: location.coords[0],
longitude: location.coords[1]
};
});
boundsArray.push({
latitude: this.state.coords[0],
longitude: this.state.coords[1]
});
const bounds = geolib.getBounds(boundsArray);
this.webViewLeaflet.sendMessage({
bounds: [[bounds.minLat, bounds.minLng], [bounds.maxLat, bounds.maxLng]],
padding: [100, 100]
});
};
// update the map object in the component's state
onUpdateMapState = (data) => {
this.setState({
...this.state,
mapState: { ...this.mapState, ...data }
});
};
render() {
return (
<View style={styles.container}>
<View style={styles.statusBar} />
<Text
style={{
margin: 10,
fontSize: 24,
color: 'black'
}}
>
Animated Map Markers App
</Text>
<WebViewLeaflet
ref={(component) => (this.webViewLeaflet = component)}
onLoad={this.onLoad}
eventReceiver={this} // the component that will receive map events
/>
<View
style={{
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center',
paddingVertical: 8,
backgroundColor: 'rgba(255,255,255,.50)'
}}
>
<Button
onPress={() => this.centerMap('dw')}
borderWidth={0}
fontSize={30}
text={'🏰'}
/>
<Button
onPress={() => this.centerMap('bg')}
borderWidth={0}
fontSize={30}
text={'🍺'}
/>
<Button
onPress={() => this.centerMap('kd')}
borderWidth={0}
fontSize={30}
text={'👑'}
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
...StyleSheet.absoluteFillObject,
backgroundColor: '#ddff3c',
display: 'flex'
},
statusBar: {
height: Constants.statusBarHeight
},
controlButton: {
height: 40,
width: 40,
borderRadius: 5,
backgroundColor: 'dodgerblue'
}
});