forked from bramus/react-native-maps-directions-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
126 lines (110 loc) · 3.68 KB
/
App.js
File metadata and controls
126 lines (110 loc) · 3.68 KB
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
import React, { Component } from 'react';
import { Dimensions, StyleSheet, View, Text } from 'react-native';
import MapView from 'react-native-maps';
import MapViewDirections from 'react-native-maps-directions';
const { width, height } = Dimensions.get('window');
const ASPECT_RATIO = width / height;
const LATITUDE = 37.771707;
const LONGITUDE = -122.4053769;
const LATITUDE_DELTA = 0.0922;
const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO;
const GOOGLE_MAPS_APIKEY = '';
import { NativeModules } from 'react-native';
const reactNativeVersion = NativeModules.PlatformConstants.reactNativeVersion;
const reactNativeVersionString = reactNativeVersion ? `${reactNativeVersion.major}.${reactNativeVersion.minor}.${reactNativeVersion.patch}${reactNativeVersion.prerelease ? ' pre-release' : ''}` : '';
const reactNativeMapsVersion = require('./node_modules/react-native-maps/package.json').version;
const reactNativeMapsDirectionsVersion = require('./node_modules/react-native-maps-directions/package.json').version;
const styles = StyleSheet.create({
versionBox: {
position: 'absolute',
bottom: 0,
right: 0,
flexDirection: 'row',
justifyContent: 'flex-end',
},
versionText: {
padding: 4,
backgroundColor: '#FFF',
color: '#000',
},
});
class Example extends Component {
constructor(props) {
super(props);
this.state = {
coordinates: [
"Twitter HQ, Market Street, San Francisco, CA, USA",
"Apple Park Visitor Center",
],
};
this.mapView = null;
}
onMapPress = (e) => {
this.setState({
coordinates: [
...this.state.coordinates,
e.nativeEvent.coordinate,
],
});
}
onReady = (result) => {
this.mapView.fitToCoordinates(result.coordinates, {
edgePadding: {
right: (width / 10),
bottom: (height / 10),
left: (width / 10),
top: (height / 10),
},
});
}
onError = (errorMessage) => {
console.log(errorMessage); // eslint-disable-line no-console
}
setDistance(distance, duration_in_traffic) {
// console.log('setDistance');
this.setState({
distance: parseFloat(distance),
durationInTraffic: parseInt(duration_in_traffic)
});
}
render() {
return (
<View style={StyleSheet.absoluteFill}>
<MapView
initialRegion={{
latitude: LATITUDE,
longitude: LONGITUDE,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
}}
style={StyleSheet.absoluteFill}
ref={c => this.mapView = c} // eslint-disable-line react/jsx-no-bind
onPress={this.onMapPress}
>
<MapViewDirections
origin={this.state.coordinates[0]}
destination={this.state.coordinates[this.state.coordinates.length-1]}
waypoints={this.state.coordinates.slice(1,-1)}
mode='DRIVING'
apikey={GOOGLE_MAPS_APIKEY}
language='en'
strokeWidth={4}
strokeColor="black"
onStart={(params) => {
console.log(`Started routing between "${params.origin}" and "${params.destination}"${(params.waypoints.length ? " using waypoints: " + params.waypoints.join(', ') : "")}`);
}}
onReady={this.onReady}
onError={(errorMessage) => {
console.log(errorMessage);
}}
resetOnChange={false}
/>
</MapView>
<View style={styles.versionBox}>
<Text style={styles.versionText}>RN {reactNativeVersionString}, RNM: {reactNativeMapsVersion}, RNMD: {reactNativeMapsDirectionsVersion}</Text>
</View>
</View>
);
}
}
export default Example;