mogzzer

Untitled

Feb 18th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. import React from 'react';
  2. import {
  3. TouchableWithoutFeedback,
  4. StyleSheet,
  5. Keyboard,
  6. PermissionsAndroid,
  7. Platform,
  8. View
  9. } from 'react-native';
  10.  
  11. import TheMap from '../components/TheMap';
  12. import PlaceInput from '../components/PlaceInput';
  13. import axios from 'axios';
  14. import PolyLine from '@mapbox/polyline';
  15. import {Polyline} from 'react-native-maps'
  16. import Geolocation from 'react-native-geolocation-service';
  17.  
  18.  
  19. class MainMap extends React.Component{
  20. constructor(props){
  21. super(props);
  22. this.state={
  23. hasMapPermission: false,
  24. userLatitude: 0,
  25. userLongitude: 0,
  26. destinationCoords: []
  27. }
  28. this.showDirectionOnMap = this.showDirectionOnMap.bind(this);
  29. }
  30.  
  31. componentDidMount(){
  32. this._requestUserLocation();
  33. }
  34.  
  35. getUserLocation() {
  36. this.setState({hasMapPermission: true})
  37. Geolocation.watchPosition(
  38. (position) => {
  39. // let curLoc = {
  40. // userLatitude: position.coords.latitude,
  41. // userLongitude: position.coords.longitude,
  42. // latitudeDelta: 0.045,
  43. // longitudeDelta: 0.045,
  44. // }
  45. this.setState({
  46. userLatitude: position.coords.latitude,
  47. userLongitude: position.coords.longitude,
  48. })
  49. console.log(position);
  50. },
  51. (error) => {
  52. // See error code charts below.
  53. console.log(error.code, error.message);
  54. },
  55. { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
  56.  
  57. );
  58.  
  59. }
  60.  
  61. async _requestUserLocation(){
  62. try{
  63. if(Platform.OS === 'android'){
  64. const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION);
  65. if(granted === PermissionsAndroid.RESULTS.GRANTED){
  66. this.getUserLocation();
  67. }else{
  68. this.getUserLocation();
  69. }
  70. }
  71. } catch(err){
  72. console.warn(err);
  73. }
  74. }
  75.  
  76. async showDirectionOnMap(placeId){
  77. const { userLatitude, userLongitude } = this.state;
  78. try{
  79. let result = await axios.get(
  80. `https://maps.googleapis.com/maps/api/directions/json?key=AIzaSyDV6GaU80hyu9abT6hgm5P5MFrbq9o8MaE&origin=${userLatitude},${userLongitude}&destination=place_id:${placeId}`
  81. );
  82. console.log(result.data);
  83. let points = PolyLine.decode(
  84. result.data.routes[0].overview_polyline.points
  85. )
  86. let latLng = points.map(point => ({
  87. latitude: point[0],
  88. longitude: point[1],
  89. }));
  90. this.setState({destinationCoords: latLng})
  91. console.log(latLng);
  92. } catch(err){
  93. console.error(err);
  94. }
  95. }
  96.  
  97. hideKeyboard(){
  98. Keyboard.dismiss();
  99. }
  100.  
  101. render(){
  102. let polyline = this.state.destinationCoords.length > 0
  103. ? ( <Polyline coordinates={this.state.destinationCoords}/> )
  104. : null;
  105. //if(this.state.hasMapPermission === true){
  106. return(
  107. <TouchableWithoutFeedback
  108. onPress={this.hideKeyboard}
  109. >
  110. <View style={styles.container}>
  111. <PlaceInput
  112. showDirectionOnMap={this.showDirectionOnMap}
  113. userLatitude={this.state.userLatitude}
  114. userLongitude={this.state.userLongitude}
  115. />
  116. <TheMap
  117. userLatitude={this.state.userLatitude}
  118. userLongitude={this.state.userLongitude}
  119. >
  120. {polyline}
  121. </TheMap>
  122. </View>
  123. </TouchableWithoutFeedback>
  124. )
  125. }
  126. }
  127. //}
  128.  
  129. export default MainMap;
  130.  
  131. const styles = StyleSheet.create({
  132. container:{
  133. flex: 1
  134. }
  135. })
Advertisement
Add Comment
Please, Sign In to add comment