forked from nitaliano/react-native-mapbox-gl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
348 lines (333 loc) · 11.4 KB
/
example.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
'use strict';
/* eslint no-console: 0 */
import React, { Component } from 'react';
import Mapbox, { MapView } from '@mapbox/react-native-mapbox-gl';
import {
AppRegistry,
StyleSheet,
Text,
StatusBar,
View,
ScrollView
} from 'react-native';
const accessToken = 'your-mapbox.com-access-token';
Mapbox.setAccessToken(accessToken);
class MapExample extends Component {
state = {
center: {
latitude: 40.72052634,
longitude: -73.97686958312988
},
zoom: 11,
userTrackingMode: Mapbox.userTrackingMode.none,
annotations: [{
coordinates: [40.72052634, -73.97686958312988],
type: 'point',
title: 'This is marker 1',
subtitle: 'It has a rightCalloutAccessory too',
rightCalloutAccessory: {
source: { uri: 'https://cldup.com/9Lp0EaBw5s.png' },
height: 25,
width: 25
},
annotationImage: {
source: { uri: 'https://cldup.com/CnRLZem9k9.png' },
height: 25,
width: 25
},
id: 'marker1'
}, {
coordinates: [40.714541341726175,-74.00579452514648],
type: 'point',
title: 'Important!',
subtitle: 'Neat, this is a custom annotation image',
annotationImage: {
source: { uri: 'https://cldup.com/7NLZklp8zS.png' },
height: 25,
width: 25
},
id: 'marker2'
}, {
coordinates: [[40.76572150042782,-73.99429321289062],[40.743485405490695, -74.00218963623047],[40.728266950429735,-74.00218963623047],[40.728266950429735,-73.99154663085938],[40.73633186448861,-73.98983001708984],[40.74465591168391,-73.98914337158203],[40.749337730454826,-73.9870834350586]],
type: 'polyline',
strokeColor: '#00FB00',
strokeWidth: 4,
strokeAlpha: .5,
id: 'foobar'
}, {
coordinates: [[40.749857912194386, -73.96820068359375], [40.741924698522055,-73.9735221862793], [40.735681504432264,-73.97523880004883], [40.7315190495212,-73.97438049316406], [40.729177554196376,-73.97180557250975], [40.72345355209305,-73.97438049316406], [40.719290332250544,-73.97455215454102], [40.71369559554873,-73.97729873657227], [40.71200407096382,-73.97850036621094], [40.71031250340588,-73.98691177368163], [40.71031250340588,-73.99154663085938]],
type: 'polygon',
fillAlpha: 1,
strokeColor: '#ffffff',
fillColor: '#0000ff',
id: 'zap'
}]
};
onRegionDidChange = (location) => {
this.setState({ currentZoom: location.zoomLevel });
console.log('onRegionDidChange', location);
};
onRegionWillChange = (location) => {
console.log('onRegionWillChange', location);
};
onUpdateUserLocation = (location) => {
console.log('onUpdateUserLocation', location);
};
onOpenAnnotation = (annotation) => {
console.log('onOpenAnnotation', annotation);
};
onRightAnnotationTapped = (e) => {
console.log('onRightAnnotationTapped', e);
};
onLongPress = (location) => {
console.log('onLongPress', location);
};
onTap = (location) => {
console.log('onTap', location);
};
onChangeUserTrackingMode = (userTrackingMode) => {
this.setState({ userTrackingMode });
console.log('onChangeUserTrackingMode', userTrackingMode);
};
componentWillMount() {
this._offlineProgressSubscription = Mapbox.addOfflinePackProgressListener(progress => {
console.log('offline pack progress', progress);
});
this._offlineMaxTilesSubscription = Mapbox.addOfflineMaxAllowedTilesListener(tiles => {
console.log('offline max allowed tiles', tiles);
});
this._offlineErrorSubscription = Mapbox.addOfflineErrorListener(error => {
console.log('offline error', error);
});
}
componentWillUnmount() {
this._offlineProgressSubscription.remove();
this._offlineMaxTilesSubscription.remove();
this._offlineErrorSubscription.remove();
}
addNewMarkers = () => {
// Treat annotations as immutable and create a new one instead of using .push()
this.setState({
annotations: [ ...this.state.annotations, {
coordinates: [40.73312,-73.989],
type: 'point',
title: 'This is a new marker',
id: 'foo'
}, {
'coordinates': [[40.749857912194386, -73.96820068359375], [40.741924698522055,-73.9735221862793], [40.735681504432264,-73.97523880004883], [40.7315190495212,-73.97438049316406], [40.729177554196376,-73.97180557250975], [40.72345355209305,-73.97438049316406], [40.719290332250544,-73.97455215454102], [40.71369559554873,-73.97729873657227], [40.71200407096382,-73.97850036621094], [40.71031250340588,-73.98691177368163], [40.71031250340588,-73.99154663085938]],
'type': 'polygon',
'fillAlpha': 1,
'fillColor': '#000000',
'strokeAlpha': 1,
'id': 'new-black-polygon'
}]
});
};
updateMarker2 = () => {
// Treat annotations as immutable and use .map() instead of changing the array
this.setState({
annotations: this.state.annotations.map(annotation => {
if (annotation.id !== 'marker2') { return annotation; }
return {
coordinates: [40.714541341726175,-74.00579452514648],
'type': 'point',
title: 'New Title!',
subtitle: 'New Subtitle',
annotationImage: {
source: { uri: 'https://cldup.com/7NLZklp8zS.png' },
height: 25,
width: 25
},
id: 'marker2'
};
})
});
};
removeMarker2 = () => {
this.setState({
annotations: this.state.annotations.filter(a => a.id !== 'marker2')
});
};
render() {
StatusBar.setHidden(true);
return (
<View style={styles.container}>
<MapView
ref={map => { this._map = map; }}
style={styles.map}
initialCenterCoordinate={this.state.center}
initialZoomLevel={this.state.zoom}
initialDirection={0}
rotateEnabled={true}
scrollEnabled={true}
zoomEnabled={true}
showsUserLocation={false}
styleURL={Mapbox.mapStyles.dark}
userTrackingMode={this.state.userTrackingMode}
annotations={this.state.annotations}
annotationsAreImmutable
onChangeUserTrackingMode={this.onChangeUserTrackingMode}
onRegionDidChange={this.onRegionDidChange}
onRegionWillChange={this.onRegionWillChange}
onOpenAnnotation={this.onOpenAnnotation}
onRightAnnotationTapped={this.onRightAnnotationTapped}
onUpdateUserLocation={this.onUpdateUserLocation}
onLongPress={this.onLongPress}
onTap={this.onTap}
/>
<ScrollView style={styles.scrollView}>
{this._renderButtons()}
</ScrollView>
</View>
);
}
_renderButtons() {
return (
<View>
<Text onPress={() => this._map && this._map.setDirection(0)}>
Set direction to 0
</Text>
<Text onPress={() => this._map && this._map.setZoomLevel(6)}>
Zoom out to zoom level 6
</Text>
<Text onPress={() => this._map && this._map.setCenterCoordinate(48.8589, 2.3447)}>
Go to Paris at current zoom level {parseInt(this.state.currentZoom)}
</Text>
<Text onPress={() => this._map && this._map.setCenterCoordinateZoomLevel(35.68829, 139.77492, 14)}>
Go to Tokyo at fixed zoom level 14
</Text>
<Text onPress={() => this._map && this._map.easeTo({ pitch: 30 })}>
Set pitch to 30 degrees
</Text>
<Text onPress={this.addNewMarkers}>
Add new marker
</Text>
<Text onPress={this.updateMarker2}>
Update marker2
</Text>
<Text onPress={() => this._map && this._map.selectAnnotation('marker1')}>
Open marker1 popup
</Text>
<Text onPress={() => this._map && this._map.deselectAnnotation()}>
Deselect annotation
</Text>
<Text onPress={this.removeMarker2}>
Remove marker2 annotation
</Text>
<Text onPress={() => this.setState({ annotations: [] })}>
Remove all annotations
</Text>
<Text onPress={() => this._map && this._map.setVisibleCoordinateBounds(40.712, -74.227, 40.774, -74.125, 100, 0, 0, 0)}>
Set visible bounds to 40.7, -74.2, 40.7, -74.1
</Text>
<Text onPress={() => this.setState({ userTrackingMode: Mapbox.userTrackingMode.followWithHeading })}>
Set userTrackingMode to followWithHeading
</Text>
<Text onPress={() => this._map && this._map.getCenterCoordinateZoomLevel((location)=> {
console.log(location);
})}>
Get location
</Text>
<Text onPress={() => this._map && this._map.getDirection((direction)=> {
console.log(direction);
})}>
Get direction
</Text>
<Text onPress={() => this._map && this._map.getBounds((bounds)=> {
console.log(bounds);
})}>
Get bounds
</Text>
<Text onPress={async () => {
try {
await Mapbox.initializeOfflinePacks();
await Mapbox.addOfflinePack({
name: 'test',
type: 'bbox',
bounds: [42.00273287349021, 12.635713745117073, 41.74068098333959, 12.153523284912126],
minZoomLevel: 4,
maxZoomLevel: 15,
metadata: { anyValue: 'you wish' },
styleURL: Mapbox.mapStyles.dark
});
console.log('Offline pack added');
} catch (e) {
console.log(e);
}
}}>
Create offline pack
</Text>
<Text onPress={() => {
Mapbox.getOfflinePacks()
.then(packs => {
console.log(packs);
})
.catch(err => {
console.log(err);
});
}}>
Get offline packs
</Text>
<Text onPress={() => {
Mapbox.suspendOfflinePack('test')
.then(info => {
if (info.suspended) {
console.log('Suspended', info.suspended);
} else {
console.log('No packs to suspend');
}
})
.catch(err => {
console.log(err);
});
}}>
Pause/Suspend pack with name 'test'
</Text>
<Text onPress={() => {
Mapbox.resumeOfflinePack('test')
.then(info => {
if (info.resumed) {
console.log('Resumed', info.resumed);
} else {
console.log('No packs to resume');
}
})
.catch(err => {
console.log(err);
});
}}>
Resume pack with name 'test'
</Text>
<Text onPress={() => {
Mapbox.removeOfflinePack('test')
.then(info => {
if (info.deleted) {
console.log('Deleted', info.deleted);
} else {
console.log('No packs to delete');
}
})
.catch(err => {
console.log(err);
});
}}>
Remove pack with name 'test'
</Text>
<Text>User tracking mode is {this.state.userTrackingMode}</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'stretch'
},
map: {
flex: 1
},
scrollView: {
flex: 1
}
});
AppRegistry.registerComponent('YourAppName', () => MapExample);