File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
import React, {createContext, useState, useEffect} from 'react';
import Geolocation from 'react-native-geolocation-service';
import {Platform, PermissionsAndroid} from 'react-native';
export const MapContext = createContext();
// //위치 정보 수집 권한 요청
async function requestPermission() {
try {
if (Platform.OS === 'ios') {
return await Geolocation.requestAuthorization('always');
}
if (Platform.OS === 'android') {
return await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
);
}
} catch (e) {
console.log(e);
}
}
export default function MapProvider({children}) {
//현위치, marker 위치 검색내용 state
const [location, setLocation] = useState();
const [searchQuery, setSearchQuery] = useState(null);
//backedn 받아온 node
const [startPoint, setStartPoint] = useState();
const [endPoint, setEndPoint] = useState({latitude: 0, longitude: 0});
const [nodes, setNodes] = useState([]);
// 거리 환산
const [km, setKm] = useState();
//현위치 받아오기기
useEffect(() => {
requestPermission().then(result => {
console.log({result});
if (result === 'granted') {
Geolocation.watchPosition(
position => {
setLocation(position.coords);
},
error => {
console.log(error);
},
{
fastestInterval: 1000,
},
);
}
});
}, []);
//두좌표 거리 구하는 함수
//목적지 send
const onQuery = query => {
const {latitude, longitude} = location;
};
const value = React.useMemo(() => ({
onQuery,
location,
searchQuery,
setSearchQuery,
startPoint,
endPoint,
nodes,
setNodes,
km,
}));
return <MapContext.Provider value={value}>{children}</MapContext.Provider>;
}