moonyeju 2023-11-15
[FIX] driving문제 수정
@10fed03f31cf73eca8b8ad8b6781130d31b9e7d5
src/component/Map.js
--- src/component/Map.js
+++ src/component/Map.js
@@ -45,9 +45,6 @@
 
   return (
     <View style={styles.mapStyle}>
-      <View style={styles.searchContainer}>
-        <Search placeholder={'도착지를 입력하세요'} />
-      </View>
       <MapView
         style={styles.mapStyle}
         provider={PROVIDER_GOOGLE}
@@ -70,7 +67,7 @@
             />
           ))}
       </MapView>
-      <View style={styles.buttonstyle}>
+      {/* <View style={styles.buttonstyle}>
         <View style={styles.markerButtonStyle}>
           <Button
             title={'마커 표시'}
@@ -82,7 +79,7 @@
             backgroundColor={PRIMERY}
           />
         </View>
-      </View>
+      </View> */}
     </View>
   );
 }
@@ -97,14 +94,7 @@
   mapStyle: {
     flex: 1,
   },
-  searchContainer: {
-    paddingTop: 10,
-    flexDirection: 'row',
-    width: '100%',
-    justifyContent: 'center',
-    alignItems: 'center',
-    paddingHorizontal: 5,
-  },
+
   buttonstyle: {
     marginHorizontal: 15,
   },
src/component/Photo.js
--- src/component/Photo.js
+++ src/component/Photo.js
@@ -10,6 +10,7 @@
 import RNFS from 'react-native-fs';
 import {url} from '../url';
 import ModalComponent from './ModalComponent';
+import {MapContext} from '../context/MapContext';
 
 export default function Photo({onUploadFile}) {
   const {onQuery, location} = useContext(MapContext);
src/context/MapContext.js
--- src/context/MapContext.js
+++ src/context/MapContext.js
@@ -1,6 +1,14 @@
 import React, {createContext, useState, useEffect} from 'react';
 import Geolocation from 'react-native-geolocation-service';
-import {Platform, PermissionsAndroid} from 'react-native';
+import {Platform, PermissionsAndroid, Alert} from 'react-native';
+import {url} from '../url';
+// import {accelerometer} from 'react-native-sensors';
+import {
+  setUpdateIntervalForType,
+  SensorTypes,
+  accelerometer,
+} from 'react-native-sensors';
+import {latLng} from 'leaflet';
 
 export const MapContext = createContext();
 
@@ -24,6 +32,7 @@
   //현위치, marker 위치 검색내용 state
   const [location, setLocation] = useState();
   const [searchQuery, setSearchQuery] = useState(null);
+  const [isAccelerometerActive, setIsAccelerometerActive] = useState(false); // 상태 추가
 
   //backedn 받아온 node
   const [startPoint, setStartPoint] = useState();
@@ -55,10 +64,84 @@
   }, []);
 
   //두좌표 거리 구하는 함수
+  function getDistance(lat1, lon1, lat2, lon2) {
+    if (lat1 == lat2 && lon1 == lon2) return 0;
+
+    var radLat1 = (Math.PI * lat1) / 180;
+    var radLat2 = (Math.PI * lat2) / 180;
+    var theta = lon1 - lon2;
+    var radTheta = (Math.PI * theta) / 180;
+    var dist =
+      Math.sin(radLat1) * Math.sin(radLat2) +
+      Math.cos(radLat1) * Math.cos(radLat2) * Math.cos(radTheta);
+    if (dist > 1) dist = 1;
+
+    dist = Math.acos(dist);
+    dist = (dist * 180) / Math.PI;
+    dist = dist * 60 * 1.1515 * 1.609344 * 1000;
+    if (dist < 100) dist = Math.round(dist / 10) * 10;
+    else dist = Math.round(dist / 100) * 100;
+
+    return dist;
+  }
 
   //목적지 send
   const onQuery = query => {
+    setSearchQuery(query);
     const {latitude, longitude} = location;
+    return fetch(`${url}/trip/trip2`, {
+      method: 'POST',
+      headers: {
+        'Content-Type': 'application/json; charset=UTF-8',
+      },
+      body: JSON.stringify({
+        dest1_x: latitude,
+        dest1_y: longitude,
+        path_end: query,
+      }),
+    })
+      .then(response => response.json())
+      .then(data => {
+        const nodes = data.nodes;
+        const start_point = data.start_point;
+        const end_point = data.end_point;
+
+        //end_point obj 변환
+        let endPointObj = {};
+        endPointObj.latitude = end_point[0];
+        endPointObj.longitude = end_point[1];
+        setEndPoint(endPointObj);
+        //nodes obj 변환
+        let nodesArr = nodes;
+        let resultNodes = nodesArr.map(i => {
+          let nodesObj = {};
+          nodesObj.latitude = i[0];
+          nodesObj.longitude = i[1];
+          return nodesObj;
+        });
+        setNodes(resultNodes);
+        //start_point obj 변환
+        let startPointObj = {};
+        startPointObj.latitude = start_point[0];
+        startPointObj.longitude = start_point[1];
+        setStartPoint(startPointObj);
+        // console.log('resultNodes', resultNodes);
+        //nodes total km
+        let total = [];
+        for (let i = 0; i < resultNodes.length - 1; i++) {
+          let km = getDistance(
+            resultNodes[i].latitude,
+            resultNodes[i].longitude,
+            resultNodes[i + 1].latitude,
+            resultNodes[i + 1].longitude,
+          );
+          total.push(km);
+        }
+        const totalKm = total.reduce((acc, curr) => acc + curr, 0);
+        const resultKm = totalKm / 1000;
+        setKm(resultKm);
+        return resultNodes;
+      });
   };
 
   const value = React.useMemo(() => ({
src/navigator/AppStack.js
--- src/navigator/AppStack.js
+++ src/navigator/AppStack.js
@@ -1,19 +1,25 @@
 import React from 'react';
 import {createNativeStackNavigator} from '@react-navigation/native-stack';
 
-import Main from '../screens/Main'
+import Main from '../screens/Main';
+import Driving from '../screens/Driving';
+import DestinationList from '../screens/DestinationList';
+import AuthStack from './AuthStack';
 
 const Stack = createNativeStackNavigator();
 
 export default function AppStack() {
   return (
     <Stack.Navigator
-      initialRouteName='Main'
+      initialRouteName="Main"
       screenOptions={{
         headerBackTitleVisible: false,
         headerShown: false,
       }}>
       <Stack.Screen name="Main" component={Main} />
+      <Stack.Screen name="DestinationList" component={DestinationList} />
+      <Stack.Screen name="Driving" component={Driving} />
+      <Stack.Screen name="Login screen" component={AuthStack} />
     </Stack.Navigator>
   );
-}
(파일 끝에 줄바꿈 문자 없음)
+}
src/screens/DestinationList.js
--- src/screens/DestinationList.js
+++ src/screens/DestinationList.js
@@ -40,7 +40,7 @@
         <Polyline coordinates={nodes} strokeWidth={10} strokeColor={PRIMERY} />
       </Map>
       <BottomDrop>
-        <View style={directionAlign}>
+        <View style={[directionAlign, {flexDirection: 'row'}]}>
           <Text style={{flex: 9}}>{searchQuery}</Text>
           <Button
             title={'운행 시작'}
src/screens/Main.js
--- src/screens/Main.js
+++ src/screens/Main.js
@@ -14,6 +14,7 @@
 import {url} from '../url';
 import {PRIMERY, RED, WHITE} from '../color';
 import ModalComponent from '../component/ModalComponent';
+import Search from '../component/Search';
 
 export default function Main({navigation}) {
   const {onQuery, searchQuery, location, nodes} = useContext(MapContext);
@@ -161,6 +162,9 @@
 
   return (
     <View style={{flex: 1}}>
+      <View style={styles.searchContainer}>
+        <Search placeholder={'도착지를 입력하세요'} />
+      </View>
       <Map
         lat={location.latitude}
         long={location.longitude}
@@ -208,7 +212,7 @@
 
 const styles = StyleSheet.create({
   container: {
-    width: '45%',
+    width: '90%',
     // marginLeft: 5,
     // justifyContent: 'center',
     // alignItems: 'center',
@@ -216,6 +220,14 @@
     bottom: 7,
     right: 15,
   },
+  searchContainer: {
+    paddingTop: 10,
+    flexDirection: 'row',
+    width: '100%',
+    justifyContent: 'center',
+    alignItems: 'center',
+    paddingHorizontal: 5,
+  },
   infoContainer: {
     marginTop: 16,
     justifyContent: 'center',
Add a comment
List