Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.36 KB | None | 0 0
  1. import React, {Component} from 'react';
  2. import {
  3. StyleSheet,
  4. TouchableOpacity,
  5. View,
  6. Dimensions,
  7. Text,
  8. PermissionsAndroid,
  9. ScrollView,
  10. Modal,
  11. ImageBackground
  12. } from "react-native";
  13. import MapView, {Marker} from "react-native-maps";
  14. import MapViewDirections from 'react-native-maps-directions';
  15. import {getPixelSize} from "../utils";
  16. import {createIconSetFromFontello} from 'react-native-vector-icons';
  17. import fontelloConfig from '../../../config.json';
  18. import {Button, Picker, Item, Icon} from "native-base";
  19. import FetchLocation from "./FetchLocation";
  20. import Loader from "../Loading/Loader";
  21.  
  22. import {connect} from "react-redux";
  23. import * as actions from "../../store/actions";
  24.  
  25. const width = Dimensions.get('window').width;
  26. const height = Dimensions.get('window').height;
  27. const Icons = createIconSetFromFontello(fontelloConfig);
  28.  
  29. class MapTrack extends Component {
  30.  
  31. state = {
  32. isLoading: true,
  33. region: null,
  34. infoLocais: false,
  35. mode: "walking",
  36. showFilters: false,
  37. };
  38.  
  39. async requestGPSPermission() {
  40. try {
  41. const granted = await PermissionsAndroid.request(
  42. PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
  43. {
  44. title: 'iLoveAveiro Permissão GPS',
  45. message: 'iLoveAveiro pretende aceder à sua localização',
  46. buttonNegative: 'Cancel',
  47. buttonPositive: 'Ok',
  48. },
  49. );
  50.  
  51. if (granted === PermissionsAndroid.RESULTS.GRANTED) {
  52. console.log("SUCCESS");
  53. this.setState({
  54. hasGPSPermission: true,
  55. });
  56. if (this.state.hasGPSPermission) {
  57. navigator.geolocation.getCurrentPosition(
  58. position => {
  59. this.mapView.animateToRegion({
  60. latitude: position.coords.latitude,
  61. longitude: position.coords.longitude,
  62. latitudeDelta: 0.0222,
  63. longitudeDelta: 0.0121,
  64. });
  65. console.log(position);
  66. },
  67. (error) => {
  68. console.log("this.state.hasGPS: ", this.state.hasGPSPermission);
  69. console.log("erro da posição: ", error)
  70. },
  71. {enableHighAccuracy: false}
  72. );
  73. }
  74. } else {
  75. console.log("ASNEIRA");
  76. }
  77. } catch (e) {
  78. console.warn("erro: ", e)
  79. }
  80. }
  81.  
  82. _setIsLoading = (boolean = false) => {
  83. this.setState(
  84. {
  85. isLoading: boolean
  86. }
  87. )
  88. };
  89.  
  90. componentDidMount() {
  91. const {navigation} = this.props;
  92. const id = navigation.getParam('id', 'undefined');
  93. this.props.getTrackPlaces(this._setIsLoading, id);
  94. this.requestGPSPermission();
  95. }
  96.  
  97. getWaypoints = (data) => {
  98. console.log("data = ", data);
  99. let content = [];
  100. if (data.length > 2) {
  101. let wayp = data.slice(1, -1);
  102. wayp.map(marker => {
  103. console.log("marker = ", marker);
  104. content = [
  105. ...content,
  106. {
  107. latitude: marker.latitude,
  108. longitude: marker.longitude
  109. }
  110. ]
  111. })
  112. }
  113. return content;
  114. };
  115.  
  116. center = () => {
  117. this.requestGPSPermission();
  118. };
  119.  
  120. render() {
  121. let data = this.props.places;
  122. const {region} = this.state;
  123.  
  124. return (
  125. !this.state.isLoading ? (
  126. <View style={{flex: 1}}>
  127. <MapView
  128. style={styles.map}
  129. showsUserLocation={true}
  130. showsMyLocationButton={false}
  131. region={region}
  132. initialRegion={{
  133. latitude: data[0].latitude,
  134. longitude: data[0].longitude,
  135. latitudeDelta: 0.0922,
  136. longitudeDelta: 0.0421,
  137. }}
  138. ref={el => (this.mapView = el)}
  139. >
  140. {data.map((coord, index) => {
  141. return (
  142. <Marker key={`coord_${index}`} coordinate={coord} title={String(coord.name)}/>)
  143. })}
  144.  
  145. {(data.length >= 2) ? (
  146.  
  147. <MapViewDirections
  148. origin={data[0]}
  149. waypoints={this.getWaypoints(data)}
  150. destination={data[data.length - 1]}
  151. apikey="xxxxxx"
  152. strokeWidth={3}
  153. mode={this.state.mode}
  154. strokeColor="orange"
  155. optimizeWaypoints={true}
  156. onStart={(params) => {
  157. console.log(`Started routing between "${params.origin}" and "${params.destination}"`);
  158. }}
  159. onReady={result => {
  160. console.log(`Distance: ${result.distance} km`);
  161. console.log(`Duration: ${result.duration} min.`);
  162. this.mapView.fitToCoordinates(result.coordinates, {
  163. edgePadding: {
  164. right: getPixelSize(50),
  165. left: getPixelSize(50),
  166. top: getPixelSize(50),
  167. bottom: getPixelSize(50),
  168. }
  169. });
  170. }}
  171. onError={(errorMessage) => {
  172. console.log(errorMessage);
  173. }}
  174. />
  175. ) : console.log("nao entrou")}
  176. </MapView>
  177.  
  178. <FetchLocation onGetLocation={this.center}/>
  179.  
  180. <Modal transparent closeOnClick={true}
  181. animationType="slide"
  182. onRequestClose={() => {
  183. this.setState({showFiltersMap: false})
  184. }}
  185. visible={this.state.showFilters}>
  186. <View style={styles.ModalView}>
  187. <Text style={{alignSelf: 'center', marginTop: 20, fontWeight: 'bold'}}>Meios de
  188. Transporte</Text>
  189. <View style={{
  190. flex: 1, flexDirection: 'row', marginTop: 20, justifyContent: 'center',
  191. alignItems: 'center'
  192. }}>
  193. <View style={{marginRight: 30}}>
  194. <Button rounded light onPress={() => {
  195. console.log("bike");
  196. this.setState({
  197. mode: "bicycling",
  198. });
  199. }} style={this.state.mode === "bicycling" ? {
  200. backgroundColor: '#FFCB64',
  201. alignSelf: 'center'
  202. } : {alignSelf: 'center'}}>
  203. <Icons name="bike" size={40}/>
  204. </Button>
  205. <Text style={{position: 'relative', top: 5, alignSelf: 'center'}}>Bicicleta</Text>
  206. </View>
  207. <View style={{marginRight: 30}}>
  208. <Button rounded light onPress={() => {
  209. this.setState({
  210. mode: "walking",
  211. })
  212. }} style={this.state.mode === "walking" ? {
  213. backgroundColor: '#FFCB64',
  214. alignSelf: 'center'
  215. } : {alignSelf: 'center'}}>
  216. <Icons name="walk" size={40}/>
  217. </Button>
  218. <Text style={{alignSelf: 'center', position: 'relative', top: 5}}>Caminhar</Text>
  219. </View>
  220. <View>
  221. <Button rounded light onPress={() => {
  222. this.setState({
  223. mode: "driving",
  224. })
  225. }} style={this.state.mode === "driving" ? {
  226. backgroundColor: '#FFCB64',
  227. alignSelf: 'center'
  228. } : {alignSelf: 'center'}}>
  229. <Icons name="car" size={40}/>
  230. </Button>
  231. <Text style={{alignSelf: 'center', position: 'relative', top: 5}}>Carro</Text>
  232. </View>
  233. </View>
  234. <View style={{flex: 1}}>
  235. <TouchableOpacity style={{position: 'relative', top: 20,}} onPress={() => {
  236. this.setState({
  237. showFilters: false
  238. })
  239. }
  240. }>
  241. <Text style={{color: '#FFCB64'}}>Fechar</Text>
  242. </TouchableOpacity>
  243. </View>
  244. </View>
  245. </Modal>
  246.  
  247. {this.state.infoLocais &&
  248. <ScrollView style={styles.placesContainer} horizontal showsHorizontalScrollIndicator={false}
  249. pagingEnabled onMomentumScrollEnd={e => {
  250. const scrolled = e.nativeEvent.contentOffset.x;
  251. const place = (scrolled > 0) ? scrolled / Dimensions.get('window').width : 0;
  252.  
  253. const {latitude, longitude} = data[place];
  254. this.mapView.animateToRegion({
  255. latitude,
  256. longitude,
  257. latitudeDelta: 0.0122,
  258. longitudeDelta: 0.0121,
  259. });
  260.  
  261. }}>
  262. {data.map(place => (
  263. <View key={place.name} style={styles.place}>
  264. <ImageBackground imageStyle={{borderRadius: 20}} style={{width: '100%', height: 200}}
  265. source={{uri: 'https://turismodocentro.pt/wp-content/uploads/2017/03/ria-aveiro_1920x1080.jpg'}}>
  266. <View style={{
  267. flexDirection: 'column',
  268. width: '100%',
  269. flex: 1,
  270. backgroundColor: 'rgba(0,0,0,0.5)',
  271. borderRadius: 20
  272. }}>
  273. <View style={{marginBottom: 30}}>
  274. <Text style={{
  275. paddingTop: 10,
  276. paddingLeft: 10,
  277. color: 'white',
  278. fontWeight: 'bold'
  279. }}>CULTURA</Text>
  280. </View>
  281. <View>
  282. <Text style={{
  283. fontSize: 25,
  284. color: 'white',
  285. paddingLeft: 10,
  286. }}>{place.name}</Text>
  287. </View>
  288. <Text></Text>
  289. <Text></Text>
  290. <View style={{
  291. width: 50,
  292. height: 50,
  293. alignSelf: 'flex-end',
  294. justifyContent: 'center',
  295. alignItems: 'center',
  296. borderRadius: 5,
  297. marginRight: 10
  298. }}>
  299.  
  300. <Icon name="info" style={{paddingTop: 5, color: 'white'}}
  301. type="SimpleLineIcons"/>
  302. <Text style={{
  303. fontWeight: 'bold',
  304. paddingBottom: 2,
  305. color: 'white'
  306. }}>INFO</Text>
  307. </View>
  308. </View>
  309. </ImageBackground>
  310. </View>
  311. ))
  312. }
  313. </ScrollView>
  314. }
  315.  
  316. <View style={styles.bottom}>
  317. <View style={styles.bottomViews}>
  318. <TouchableOpacity onPress={() => {
  319. this.props.navigation.navigate('Home')
  320. }}>
  321. <Icons style={styles.icon} name="home" size={60}/>
  322. </TouchableOpacity>
  323. </View>
  324. <View style={styles.bottomViews}>
  325. <Icons style={styles.icon} name="qr" size={60}/>
  326. </View>
  327. <View style={styles.bottomViews}>
  328. <TouchableOpacity onPress={() => this.setState({showFilters: true, infoLocais: false,})}>
  329. <Icons style={styles.icon} name="filtros" size={60}/>
  330. </TouchableOpacity>
  331. </View>
  332. <View style={styles.bottomViews}>
  333. {this.state.infoLocais ?
  334. <TouchableOpacity onPress={() => this.setState({infoLocais: false})}>
  335. <Icons style={styles.icon} name="trajetos" size={40}/>
  336. </TouchableOpacity> :
  337. <TouchableOpacity onPress={() => this.setState({infoLocais: true, filtro: false})}>
  338. <Icons style={styles.icon} name="trajetos" size={40}/>
  339. </TouchableOpacity>
  340. }
  341. </View>
  342. </View>
  343. </View>
  344. ) : (
  345. <Loader/>
  346. )
  347. )
  348. }
  349. }
  350.  
  351. const styles = StyleSheet.create({
  352. map: {
  353. ...StyleSheet.absoluteFillObject,
  354. justifyContent: 'center',
  355. position: 'absolute',
  356. },
  357. bottom: {
  358. flex: 1,
  359. flexDirection: 'row',
  360. backgroundColor: 'white',
  361. position: 'absolute',
  362. bottom: 0,
  363. width: "100%",
  364. borderTopLeftRadius: 100,
  365. borderTopRightRadius: 100,
  366. },
  367. bottomViews: {
  368. justifyContent: 'center',
  369. alignItems: 'center',
  370. width: (width / 4),
  371. },
  372. icon: {
  373. color: '#C4C4C4',
  374. },
  375. placesContainer: {
  376. width: '100%',
  377. height: 200,
  378. position: 'absolute',
  379. top: (height * 0.6),
  380. },
  381. place: {
  382. width: width - 40,
  383. maxHeight: 200,
  384. backgroundColor: '#FFF',
  385. marginHorizontal: 20,
  386. borderRadius: 20,
  387. },
  388. ModalView: {
  389. backgroundColor: 'white',
  390. height: height * 0.33,
  391. width: width * 0.8,
  392. justifyContent: 'center',
  393. alignItems: 'center',
  394. marginLeft: width * 0.1,
  395. marginTop: height * 0.33,
  396. borderRadius: 40,
  397. flexDirection: 'column',
  398. },
  399. });
  400.  
  401. function mapStateToProps(state) {
  402. return {
  403. places: state.trackPlaces.placesSuccess
  404. };
  405. }
  406.  
  407. export default connect(
  408. mapStateToProps,
  409. actions
  410. )(MapTrack);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement