Files
react-native-mapbox-gl/example.js
T

315 lines
10 KiB
JavaScript
Raw Normal View History

2015-06-29 21:10:40 -07:00
'use strict';
2016-07-22 16:12:56 -04:00
/* eslint no-console: 0 */
2015-06-29 21:10:40 -07:00
2016-05-06 14:19:12 -07:00
import React, { Component } from 'react';
2016-06-21 14:37:59 +03:00
import Mapbox, { MapView } from 'react-native-mapbox-gl';
2016-05-06 14:19:12 -07:00
import {
2015-06-29 21:10:40 -07:00
AppRegistry,
StyleSheet,
Text,
2016-04-18 15:42:16 -07:00
StatusBar,
2016-07-02 19:35:43 +03:00
View,
ScrollView
2016-05-06 14:19:12 -07:00
} from 'react-native';
2015-06-29 21:10:40 -07:00
2016-06-20 12:26:49 +03:00
const accessToken = 'your-mapbox.com-access-token';
2016-06-20 19:30:10 +03:00
Mapbox.setAccessToken(accessToken);
2016-06-20 12:26:49 +03:00
2016-06-20 12:44:30 +03:00
class MapExample extends Component {
state = {
center: {
latitude: 40.72052634,
longitude: -73.97686958312988
},
zoom: 11,
2016-06-20 19:30:10 +03:00
userTrackingMode: Mapbox.userTrackingMode.none,
2016-06-20 12:44:30 +03:00
annotations: [{
coordinates: [40.72052634, -73.97686958312988],
2016-07-04 00:56:42 +03:00
type: 'point',
2016-06-20 12:44:30 +03:00
title: 'This is marker 1',
subtitle: 'It has a rightCalloutAccessory too',
rightCalloutAccessory: {
2016-07-11 21:47:42 +03:00
source: { uri: 'https://cldup.com/9Lp0EaBw5s.png' },
2016-06-20 12:44:30 +03:00
height: 25,
width: 25
2015-10-07 16:16:58 -07:00
},
2016-06-20 12:44:30 +03:00
annotationImage: {
2016-07-11 21:47:42 +03:00
source: { uri: 'https://cldup.com/CnRLZem9k9.png' },
2016-06-20 12:44:30 +03:00
height: 25,
width: 25
},
id: 'marker1'
}, {
coordinates: [40.714541341726175,-74.00579452514648],
2016-07-04 00:56:42 +03:00
type: 'point',
2016-06-20 12:44:30 +03:00
title: 'Important!',
subtitle: 'Neat, this is a custom annotation image',
annotationImage: {
2016-07-11 21:47:42 +03:00
source: { uri: 'https://cldup.com/7NLZklp8zS.png' },
2016-06-20 12:44:30 +03:00
height: 25,
width: 25
},
id: 'marker2'
}, {
2016-07-04 00:56:42 +03:00
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'
2016-06-20 12:44:30 +03:00
}, {
2016-07-04 00:56:42 +03:00
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'
2016-06-20 12:44:30 +03:00
}]
};
2016-07-02 19:13:01 +03:00
onRegionDidChange = (location) => {
this.setState({ currentZoom: location.zoomLevel });
console.log('onRegionDidChange', location);
2016-06-20 12:44:30 +03:00
};
onRegionWillChange = (location) => {
2016-06-21 14:37:59 +03:00
console.log('onRegionWillChange', location);
2016-06-20 12:44:30 +03:00
};
onUpdateUserLocation = (location) => {
2016-06-21 14:37:59 +03:00
console.log('onUpdateUserLocation', location);
2016-06-20 12:44:30 +03:00
};
onOpenAnnotation = (annotation) => {
2016-06-21 14:37:59 +03:00
console.log('onOpenAnnotation', annotation);
2016-06-20 12:44:30 +03:00
};
onRightAnnotationTapped = (e) => {
2016-06-21 14:37:59 +03:00
console.log('onRightAnnotationTapped', e);
2016-06-20 12:44:30 +03:00
};
onLongPress = (location) => {
2016-06-21 14:37:59 +03:00
console.log('onLongPress', location);
2016-06-20 12:44:30 +03:00
};
onTap = (location) => {
2016-06-21 14:37:59 +03:00
console.log('onTap', location);
};
onChangeUserTrackingMode = (userTrackingMode) => {
this.setState({ userTrackingMode });
console.log('onChangeUserTrackingMode', userTrackingMode);
2016-06-20 12:44:30 +03:00
};
2016-06-20 23:11:37 +03:00
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);
});
2016-07-22 16:12:56 -04:00
}
2016-06-20 12:44:30 +03:00
2016-06-20 23:11:37 +03:00
componentWillUnmount() {
this._offlineProgressSubscription.remove();
this._offlineMaxTilesSubscription.remove();
this._offlineErrorSubscription.remove();
}
2016-06-20 19:30:10 +03:00
addNewMarkers = () => {
2016-06-21 19:49:20 +03:00
// Treat annotations as immutable and create a new one instead of using .push()
2016-06-20 19:30:10 +03:00
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,
2016-07-03 00:56:51 +03:00
'fillColor': '#000000',
2016-06-20 19:30:10 +03:00
'strokeAlpha': 1,
'id': 'new-black-polygon'
}]
});
};
updateMarker2 = () => {
2016-06-21 19:49:20 +03:00
// Treat annotations as immutable and use .map() instead of changing the array
2016-06-20 19:30:10 +03:00
this.setState({
annotations: this.state.annotations.map(annotation => {
if (annotation.id !== 'marker2') { return annotation; }
2016-06-20 19:30:10 +03:00
return {
2016-01-08 12:41:11 -08:00
coordinates: [40.714541341726175,-74.00579452514648],
'type': 'point',
title: 'New Title!',
subtitle: 'New Subtitle',
annotationImage: {
2016-07-11 21:47:42 +03:00
source: { uri: 'https://cldup.com/7NLZklp8zS.png' },
2016-01-08 12:41:11 -08:00
height: 25,
width: 25
},
id: 'marker2'
2016-06-20 19:30:10 +03:00
};
})
});
};
removeMarker2 = () => {
this.setState({
annotations: this.state.annotations.filter(a => a.id !== 'marker2')
2016-06-20 19:30:10 +03:00
});
};
render() {
StatusBar.setHidden(true);
return (
<View style={styles.container}>
2016-07-02 19:35:43 +03:00
<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}
2016-07-05 12:01:56 +03:00
styleURL={Mapbox.mapStyles.dark}
2016-07-02 19:35:43 +03:00
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>
2016-06-20 19:30:10 +03:00
<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>
2016-06-20 19:30:10 +03:00
<Text onPress={this.addNewMarkers}>
Add new marker
</Text>
<Text onPress={this.updateMarker2}>
2016-01-08 12:41:11 -08:00
Update marker2
</Text>
2016-06-21 15:11:27 +03:00
<Text onPress={() => this._map && this._map.selectAnnotation('marker1')}>
2015-12-19 12:31:03 -08:00
Open marker1 popup
2015-06-29 21:10:40 -07:00
</Text>
2016-09-21 09:50:56 +03:00
<Text onPress={() => this._map && this._map.deselectAnnotation()}>
Deselect annotation
</Text>
2016-06-20 19:30:10 +03:00
<Text onPress={this.removeMarker2}>
2015-12-19 12:31:03 -08:00
Remove marker2 annotation
</Text>
2016-06-20 19:30:10 +03:00
<Text onPress={() => this.setState({ annotations: [] })}>
2015-12-19 12:31:03 -08:00
Remove all annotations
2015-06-29 21:10:40 -07:00
</Text>
2016-06-20 19:30:10 +03:00
<Text onPress={() => this._map && this._map.setVisibleCoordinateBounds(40.712, -74.227, 40.774, -74.125, 100, 0, 0, 0)}>
2015-10-27 21:38:42 -07:00
Set visible bounds to 40.7, -74.2, 40.7, -74.1
</Text>
2016-07-02 00:29:56 +03:00
<Text onPress={() => this.setState({ userTrackingMode: Mapbox.userTrackingMode.followWithHeading })}>
Set userTrackingMode to followWithHeading
2015-11-21 12:29:09 -08:00
</Text>
2016-06-20 12:26:49 +03:00
<Text onPress={() => this._map && this._map.getCenterCoordinateZoomLevel((location)=> {
2016-01-11 13:59:43 -08:00
console.log(location);
})}>
Get location
</Text>
2016-06-20 12:26:49 +03:00
<Text onPress={() => this._map && this._map.getDirection((direction)=> {
2016-01-11 13:59:43 -08:00
console.log(direction);
})}>
Get direction
</Text>
2016-06-20 12:26:49 +03:00
<Text onPress={() => this._map && this._map.getBounds((bounds)=> {
2016-04-18 15:42:16 -07:00
console.log(bounds);
})}>
Get bounds
</Text>
2016-07-02 00:29:56 +03:00
<Text onPress={() => {
Mapbox.addOfflinePack({
name: 'test',
type: 'bbox',
bounds: [0, 0, 0, 0],
minZoomLevel: 0,
maxZoomLevel: 0,
metadata: { anyValue: 'you wish' },
2016-07-05 12:01:56 +03:00
styleURL: Mapbox.mapStyles.dark
2016-07-02 00:29:56 +03:00
}).then(() => {
console.log('Offline pack added');
}).catch(err => {
console.log(err);
});
}}>
2016-04-18 15:42:16 -07:00
Create offline pack
</Text>
2016-07-02 00:29:56 +03:00
<Text onPress={() => {
Mapbox.getOfflinePacks()
.then(packs => {
console.log(packs);
})
.catch(err => {
console.log(err);
});
}}>
2016-04-18 15:42:16 -07:00
Get offline packs
</Text>
2016-07-02 00:29:56 +03:00
<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);
});
}}>
2016-04-18 15:42:16 -07:00
Remove pack with name 'test'
</Text>
2016-06-20 19:30:10 +03:00
<Text>User tracking mode is {this.state.userTrackingMode}</Text>
2015-06-29 21:10:40 -07:00
</View>
);
}
2016-06-20 12:44:30 +03:00
}
2015-06-29 21:10:40 -07:00
2016-06-20 12:44:30 +03:00
const styles = StyleSheet.create({
2015-06-29 21:10:40 -07:00
container: {
2016-07-02 19:35:43 +03:00
flex: 1,
alignItems: 'stretch'
},
map: {
flex: 1
},
scrollView: {
2015-06-29 21:10:40 -07:00
flex: 1
}
});
2016-06-20 12:26:49 +03:00
AppRegistry.registerComponent('YourAppName', () => MapExample);