From d9adc45b27f13735be6b33395740ceeca805be2a Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Fri, 7 Jul 2023 14:59:04 +0800 Subject: [PATCH] Added initial map structure for homepage --- src/components/GetDistance/GetDistance.tsx | 23 ++++++ src/routes/Home/Home.tsx | 86 +++++++++++++++------- 2 files changed, 84 insertions(+), 25 deletions(-) create mode 100644 src/components/GetDistance/GetDistance.tsx diff --git a/src/components/GetDistance/GetDistance.tsx b/src/components/GetDistance/GetDistance.tsx new file mode 100644 index 0000000..d3af390 --- /dev/null +++ b/src/components/GetDistance/GetDistance.tsx @@ -0,0 +1,23 @@ +export default function GetDistance( + lat1: number, + lon1: number, + lat2: number, + lon2: number +) { + var R = 6371; // km + var dLat = toRad(lat2 - lat1); + var dLon = toRad(lon2 - lon1); + var lat1 = toRad(lat1); + var lat2 = toRad(lat2); + + var a = + Math.sin(dLat / 2) * Math.sin(dLat / 2) + + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2); + var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); + var distance = R * c; + return distance; +} + +export function toRad(value: number) { + return (value * Math.PI) / 180; +} diff --git a/src/routes/Home/Home.tsx b/src/routes/Home/Home.tsx index 6aed10a..9ec4010 100644 --- a/src/routes/Home/Home.tsx +++ b/src/routes/Home/Home.tsx @@ -3,46 +3,82 @@ import { View, Text } from "react-native"; import { useSelector } from "react-redux"; import { RootState } from "../../features/redux/Store/Store"; import AnimatedContainer from "../../components/AnimatedContainer/AnimatedContainer"; -import React, { useState, useEffect } from "react"; +import { useState, useEffect } from "react"; import MapView, { Marker, UrlTile } from "react-native-maps"; import * as Location from "expo-location"; +import GetDistance from "../../components/GetDistance/GetDistance"; +import Button from "../../components/Button/Button"; +import { colors } from "../../styles"; type LocationType = Location.LocationObject; export default function Home() { const [location, setLocation] = useState(null); + const [dist, setDist] = useState(null); + async function requestLocation() { + let { status } = await Location.requestForegroundPermissionsAsync(); + if (status !== "granted") { + console.error("Permission to access location was denied"); + return; + } else { + getLocation(); + } + } + + async function getLocation() { + let location = await Location.getCurrentPositionAsync({}); + setLocation(location); + let dist = GetDistance( + location.coords.latitude, + location.coords.longitude, + 8.4857, + 124.6565 + ); + setDist(Math.round(dist)); + } useEffect(() => { - (async () => { - let { status } = await Location.requestForegroundPermissionsAsync(); - if (status !== "granted") { - console.error("Permission to access location was denied"); - return; - } - let location = await Location.getCurrentPositionAsync({}); - setLocation(location); - })(); + requestLocation(); }, []); - - function getInitialState() { - return { - latitude: 8.4857, - longitude: 124.6565, - latitudeDelta: 0.000235, - longitudeDelta: 0.000067, - }; + const ustpCoords = { + latitude: 8.4857, + longitude: 124.6565, + latitudeDelta: 0.000235, + longitudeDelta: 0.000067, + }; + function CustomMap() { + if (dist !== null && location !== null) { + if (dist <= 1) { + return ; + } else { + return ( + + You must be within the campus to use StudE {"\n"} + {dist}km away from USTP {"\n"} + User Location: + {"\n"} Latitude {location.coords.latitude} + {"\n"} Longitude {location.coords.longitude} + + ); + } + } else { + return ( + + + Please allow location permission{"\n"} + + + + ); + } } const creds = useSelector((state: RootState) => state.user.user); return ( Template Homepage - - - User Location:{" "} - {location - ? `${location.coords.latitude}, ${location.coords.longitude}` - : "Loading..."} - + );