File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
import React from 'react';
//import {Alert} from 'react-native';
import Geolocation from '@react-native-community/geolocation';
import {getTokenFromStorage, verifyTokens} from './loginUtils';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {NativeStackNavigationProp} from '@react-navigation/native-stack';
import {RootStackParam} from '../utils/RootStackParam';
// API 주소
const url = 'http://takensoftai.iptime.org:15857/action/gps_update';
export const getCurrentPosition = async (
tripId:String,
navigation: NativeStackNavigationProp<RootStackParam>,
) => {
try {
const token = await getTokenFromStorage();
if (!token) {
await AsyncStorage.removeItem('token');
verifyTokens(navigation);
return;
}
let initialTimeout = 10000;
Geolocation.getCurrentPosition(
async pos => {
const longitude = JSON.stringify(pos.coords.longitude);
const latitude = JSON.stringify(pos.coords.latitude);
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
const milliSeconds = date.getMilliseconds().toString().padStart(3, '0');
const date_now = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliSeconds}`;
console.log('trip_id:', tripId, '현재 위치: ', longitude, latitude, '현재 시간:', date_now);
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `${token}`,
},
body: JSON.stringify({
trip_id: tripId,
location_x: longitude,
location_y: latitude,
timestamp: date_now
}),
});
const data = await response.json();
console.log('대답 : ', data);
if (!response.ok) {
console.log('서버 응답 실패 : ', data.msg || response.statusText);
}
if (data.result === 'fail') {
await AsyncStorage.removeItem('token');
}
} catch (error) {
console.log('서버 요청 오류:', error);
}
},
//error => Alert.alert('GetCurrentPosition Error', JSON.stringify(error)),
error => {
console.log('GetCurrentPosition Error:', error);
},
{
enableHighAccuracy: false,
timeout: initialTimeout,
maximumAge: 10000,
},
);
} catch (error) {
console.log('토큰 가져오기 오류:', error);
}
};
export default function GetCurrentLocation() {
React.useEffect(() => {
Geolocation.requestAuthorization();
}, []);
return null;
}