Advertisement
zidniryi

waypoint.js

Nov 30th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. import React, {Component} from 'react'
  2. import {Dimensions, StyleSheet, View, Text} from 'react-native'
  3. import MapView, {Marker} from 'react-native-maps'
  4.  
  5. import MapViewDirections from 'react-native-maps-directions'
  6.  
  7. const {width, height} = Dimensions.get('window')
  8. const ASPECT_RATIO = width / height
  9. const LATITUDE = 37.771707
  10. const LONGITUDE = -122.4053769
  11. const LATITUDE_DELTA = 0.0922
  12. const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO
  13.  
  14. const GOOGLE_MAPS_APIKEY = 'AIzaSyDcinNkJoLn-QX9u7M7f31JyqDDkH3PN1Y'
  15.  
  16. const styles = StyleSheet.create({
  17. versionBox: {
  18. position: 'absolute',
  19. bottom: 0,
  20. right: 0,
  21. flexDirection: 'row',
  22. justifyContent: 'flex-end',
  23. },
  24. versionText: {
  25. padding: 4,
  26. backgroundColor: '#FFF',
  27. color: '#000',
  28. },
  29. })
  30.  
  31. class HomeScreen extends Component {
  32. constructor(props) {
  33. super(props)
  34.  
  35. this.state = {
  36. coordinates: [
  37. {latitude: 37.78988546653938, longitude: -122.39009787303877},
  38. {latitude: 37.78006076834474, longitude: -122.41924072042876},
  39. {latitude: 37.79451419030897, longitude: -122.40813171242338},
  40. ],
  41. }
  42. this.mapView = null
  43. }
  44.  
  45. onMapPress = (e) => {
  46. this.setState({
  47. coordinates: [...this.state.coordinates, e.nativeEvent.coordinate],
  48. })
  49. }
  50.  
  51. onReady = (result) => {
  52. this.mapView.fitToCoordinates(result.coordinates, {
  53. edgePadding: {
  54. right: width / 10,
  55. bottom: height / 10,
  56. left: width / 10,
  57. top: height / 10,
  58. },
  59. })
  60. }
  61.  
  62. onError = (errorMessage) => {
  63. console.log(errorMessage) // eslint-disable-line no-console
  64. }
  65.  
  66. setDistance(distance, duration_in_traffic) {
  67. // console.log('setDistance');
  68. this.setState({
  69. distance: parseFloat(distance),
  70. durationInTraffic: parseInt(duration_in_traffic),
  71. })
  72. }
  73.  
  74. render() {
  75. console.log(this.state.coordinates.slice(1, -1), 'MEs')
  76. return (
  77. <View style={StyleSheet.absoluteFill}>
  78. <MapView
  79. initialRegion={{
  80. latitude: LATITUDE,
  81. longitude: LONGITUDE,
  82. latitudeDelta: LATITUDE_DELTA,
  83. longitudeDelta: LONGITUDE_DELTA,
  84. }}
  85. style={StyleSheet.absoluteFill}
  86. ref={(c) => (this.mapView = c)} // eslint-disable-line react/jsx-no-bind
  87. // onPress={this.onMapPress}
  88. >
  89. <MapViewDirections
  90. origin={this.state.coordinates[0]}
  91. destination={
  92. this.state.coordinates[this.state.coordinates.length - 1]
  93. }
  94. waypoints={this.state.coordinates.slice(1, -1)}
  95. optimizeWaypoints={true}
  96. mode="DRIVING"
  97. apikey={GOOGLE_MAPS_APIKEY}
  98. language="en"
  99. strokeWidth={4}
  100. strokeColor="black"
  101. onStart={(params) => {
  102. console.log(
  103. `Started routing between "${params.origin}" and "${
  104. params.destination
  105. }"${
  106. params.waypoints.length
  107. ? ' using waypoints: ' + params.waypoints.join(', ')
  108. : ''
  109. }`,
  110. )
  111. }}
  112. onReady={this.onReady}
  113. onError={(errorMessage) => {
  114. console.log(errorMessage)
  115. }}
  116. resetOnChange={false}
  117. />
  118.  
  119. <Marker
  120. draggable
  121. onDragEnd={(e) => setx({x: e.nativeEvent.coordinate})}
  122. coordinate={this.state.coordinates[0]}
  123. title={'Lokasi Customer'}
  124. showsUserLocation={true}
  125. pinColor="red"
  126. />
  127.  
  128. {this.state.coordinates.map((item, index) => {
  129. console.log(item, 'Saya')
  130. return (
  131. <Marker
  132. draggable
  133. // onDragEnd={(e) => setx({x: e.nativeEvent.coordinate})}
  134. coordinate={{
  135. latitude: item.latitude,
  136. longitude: item.longitude,
  137. }}
  138. title={`${index}-Saya`}
  139. showsUserLocation={true}
  140. pinColor="blue"
  141. onPress={() => alert(`${index}-Saya`)}
  142. />
  143. )
  144. })}
  145. </MapView>
  146. <View style={styles.versionBox}>
  147. {/* <Text style={styles.versionText}>
  148. RN {reactNativeVersionString}, RNM: {reactNativeMapsVersion}, RNMD:{' '}
  149. {reactNativeMapsDirectionsVersion}
  150. </Text> */}
  151. </View>
  152. </View>
  153. )
  154. }
  155. }
  156.  
  157. export default HomeScreen
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement