Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import { AppRegistry, StyleSheet, Text,
  3.          View, Dimensions, Image} from 'react-native';
  4. import MapView from 'react-native-maps';
  5. import * as firebase from 'firebase';
  6.  
  7. var {width, height} = Dimensions.get('window')
  8.  
  9. const SCREEN_HEIGHT = height
  10. const SCREEN_WIDTH = width
  11. const ASPECT_RATIO = width / height
  12. const LATITUDE_DELTA = 0.005
  13. const LONGTITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO
  14.  
  15. class Map extends Component {
  16.     constructor(props) {
  17.         super(props);
  18.  
  19.         this.state = {
  20.             initialPosition: {
  21.                 latitude: 0,
  22.                 longitude: 0,
  23.                 latitudeDelta: 0,
  24.                 longitudeDelta: 0
  25.             },
  26.             markerPosition: {
  27.                 latitude: 0,
  28.                 longitude: 0
  29.             },
  30.             carPosition: {
  31.                 lat: 1,
  32.                 lon: 1
  33.             },
  34.             time: '',
  35.             date: ''
  36.         }
  37.     }
  38.     //watchID: ?number = null
  39.  
  40.     componentWillMount() {
  41.         var firebaseConfig = {
  42.           apiKey: 'AIzaSyAYDjR3JkdXWpb8xmYZiJrnpUjYQoQXHRA',
  43.           authDomain: "reactnativedata.firebase.com",
  44.           databaseURL: "https://reactnativedata.firebaseio.com",
  45.           projectId: "reactnativedata",
  46.           storageBucket: "",
  47.           messagingSenderId: ""
  48.         };
  49.  
  50.         firebase.initializeApp(firebaseConfig);
  51.        
  52.         var postDate= this.state.date;
  53.         var postTime= this.state.time;
  54.     }
  55.  
  56.     componentDidMount() {
  57.        
  58.  
  59.         this._interval = setInterval( () => {
  60.  
  61.         navigator.geolocation.getCurrentPosition((position) => {
  62.             var lat = parseFloat(position.coords.latitude)
  63.             var long = parseFloat(position.coords.longitude)
  64.  
  65.             var initialRegion={
  66.                 latitude: lat,
  67.                 longitude: long,
  68.                 latitudeDelta: LATITUDE_DELTA,
  69.                 longitudeDelta: LONGTITUDE_DELTA
  70.             }
  71.  
  72.             this.setState({initialPosition: initialRegion})
  73.             this.setState({markerPosition: initialRegion})
  74.         },
  75.         (error) => alert(JSON.stringify(error)),
  76.         {enableHighAccuracy: true, timeout: 20000, maximumAge:10000})
  77.        
  78.         }, 5000);
  79.        
  80.         // this.watchID = navigator.geolocation.watchPosition((position) => {
  81.         //     var lat = parseFloat(position.coords.latitude)
  82.         //     var long = parseFloat(position.coords.longitude)
  83.         //     var lastRegion = {
  84.         //         latitude: lat,
  85.         //         longitude: long,
  86.         //         latitudeDelta: LATITUDE_DELTA,
  87.         //         longitudeDelta: LONGTITUDE_DELTA
  88.         //     }
  89.         //     this.setState({initialPosition: lastRegion})
  90.         //     this.setState({markerPosition: lastRegion})
  91.         // },
  92.         // (error) => alert(error.message),
  93.         // { enableHighAccuracy: false, timeout: 5000, maximumAge: 1000, distanceFilter: 1 }
  94.         // )
  95.        
  96.         this.Clock = setInterval( () => this.GetTime(), 1000 );
  97.         this.Clock = setInterval( () => this.GetDate(), 1000 );
  98.         this.Clock = setInterval( () => this.upFirebase(), 1000 );
  99.         this.Clock = setInterval( () => this.getFirebase(), 1000 );
  100.     }
  101.    
  102.     componentWillUnmount() {
  103.         //navigator.geolocation.clearWatch(this.watchID)
  104.         clearInterval(this._interval);
  105.         clearInterval(this.Clock);
  106.     }
  107.  
  108.     GetDate() {
  109.         var date, day, month, year, fullDate;
  110.         date = new Date();
  111.         day = date.getDate();
  112.         if(day < 10)
  113.             {
  114.               day = '0' + day.toString();
  115.             }
  116.         month = date.getMonth()+1;
  117.         if(month < 10)
  118.             {
  119.               month = '0' + month.toString();
  120.             }
  121.         year = date.getFullYear();
  122.         fullDate = month.toString() + day.toString() + year.toString();
  123.  
  124.         this.setState({
  125.           date: fullDate
  126.         });
  127.     }
  128.  
  129.     getFirebase() {
  130.     firebase.database().ref(this.state.date + '/ASS/' + '19:46:53').once('value', (snapshot) => {
  131.         this.setState({ carPosition: snapshot.val() })
  132.         });
  133.     }
  134.  
  135.     upFirebase() {
  136.     firebase.database().ref(this.state.date + '/ASS/' + this.state.time).set({
  137.             lat: this.state.markerPosition.latitude,
  138.             lon: this.state.markerPosition.longitude
  139.         })
  140.     }
  141.  
  142.     GetTime() {
  143.         var date, TimeType, hour, minutes, seconds, fullTime;
  144.         date = new Date();
  145.         hour = date.getHours();
  146.         if(hour < 10)
  147.             {
  148.               hour = '0' + hour.toString();
  149.             }
  150.         minutes = date.getMinutes();
  151.         if(minutes < 10)
  152.             {
  153.               minutes = '0' + minutes.toString();
  154.             }
  155.         seconds = date.getSeconds();
  156.         if(seconds < 10)
  157.             {
  158.               seconds = '0' + seconds.toString();
  159.             }
  160.         fullTime = hour.toString() + ':' + minutes.toString() + ':' + seconds.toString();
  161.      
  162.         this.setState({
  163.           time: fullTime
  164.         });
  165.     }
  166.  
  167.     render() {
  168.         return (
  169.             <View style={styles.container}>
  170.                
  171.            
  172.                 <Text>{this.state.time}</Text>
  173.                 <Text>{this.state.date}</Text>
  174.                 <Text>{this.state.carPosition.lat}</Text>
  175.                 <Text>{this.state.carPosition.lon}</Text>
  176.                
  177.                
  178.             </View>
  179.         );
  180.     }
  181. }
  182.  
  183. const styles = StyleSheet.create({
  184.   container: {
  185.     flex: 1,
  186.     backgroundColor: '#fff',
  187.     alignItems: 'center',
  188.     justifyContent: 'center'
  189.   },
  190.  
  191.  
  192. });
  193.  
  194. export default Map;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement