mirror of
https://github.com/lemeow125/StudE-Frontend.git
synced 2024-11-17 06:19:25 +08:00
Added initial map structure for homepage
This commit is contained in:
parent
5729eed175
commit
d9adc45b27
2 changed files with 84 additions and 25 deletions
23
src/components/GetDistance/GetDistance.tsx
Normal file
23
src/components/GetDistance/GetDistance.tsx
Normal file
|
@ -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;
|
||||||
|
}
|
|
@ -3,46 +3,82 @@ import { View, Text } from "react-native";
|
||||||
import { useSelector } from "react-redux";
|
import { useSelector } from "react-redux";
|
||||||
import { RootState } from "../../features/redux/Store/Store";
|
import { RootState } from "../../features/redux/Store/Store";
|
||||||
import AnimatedContainer from "../../components/AnimatedContainer/AnimatedContainer";
|
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 MapView, { Marker, UrlTile } from "react-native-maps";
|
||||||
import * as Location from "expo-location";
|
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;
|
type LocationType = Location.LocationObject;
|
||||||
export default function Home() {
|
export default function Home() {
|
||||||
const [location, setLocation] = useState<LocationType | null>(null);
|
const [location, setLocation] = useState<LocationType | null>(null);
|
||||||
|
const [dist, setDist] = useState<number | null>(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(() => {
|
useEffect(() => {
|
||||||
(async () => {
|
requestLocation();
|
||||||
let { status } = await Location.requestForegroundPermissionsAsync();
|
|
||||||
if (status !== "granted") {
|
|
||||||
console.error("Permission to access location was denied");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let location = await Location.getCurrentPositionAsync({});
|
|
||||||
setLocation(location);
|
|
||||||
})();
|
|
||||||
}, []);
|
}, []);
|
||||||
|
const ustpCoords = {
|
||||||
function getInitialState() {
|
latitude: 8.4857,
|
||||||
return {
|
longitude: 124.6565,
|
||||||
latitude: 8.4857,
|
latitudeDelta: 0.000235,
|
||||||
longitude: 124.6565,
|
longitudeDelta: 0.000067,
|
||||||
latitudeDelta: 0.000235,
|
};
|
||||||
longitudeDelta: 0.000067,
|
function CustomMap() {
|
||||||
};
|
if (dist !== null && location !== null) {
|
||||||
|
if (dist <= 1) {
|
||||||
|
return <MapView style={styles.map} initialRegion={ustpCoords} />;
|
||||||
|
} else {
|
||||||
|
return (
|
||||||
|
<Text style={styles.text_white_medium}>
|
||||||
|
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}
|
||||||
|
</Text>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return (
|
||||||
|
<View>
|
||||||
|
<Text style={styles.text_white_medium}>
|
||||||
|
Please allow location permission{"\n"}
|
||||||
|
</Text>
|
||||||
|
<Button onPress={() => requestLocation()} color={colors.blue_3}>
|
||||||
|
<Text style={styles.text_white_small}>Register</Text>
|
||||||
|
</Button>
|
||||||
|
</View>
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const creds = useSelector((state: RootState) => state.user.user);
|
const creds = useSelector((state: RootState) => state.user.user);
|
||||||
return (
|
return (
|
||||||
<View style={styles.background}>
|
<View style={styles.background}>
|
||||||
<AnimatedContainer>
|
<AnimatedContainer>
|
||||||
<Text style={styles.text_white_large}>Template Homepage</Text>
|
<Text style={styles.text_white_large}>Template Homepage</Text>
|
||||||
<MapView style={styles.map} initialRegion={getInitialState()}></MapView>
|
<CustomMap />
|
||||||
<Text style={styles.text_white_medium}>
|
|
||||||
User Location:{" "}
|
|
||||||
{location
|
|
||||||
? `${location.coords.latitude}, ${location.coords.longitude}`
|
|
||||||
: "Loading..."}
|
|
||||||
</Text>
|
|
||||||
</AnimatedContainer>
|
</AnimatedContainer>
|
||||||
</View>
|
</View>
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue