diff --git a/src/routes/Home/Home.tsx b/src/routes/Home/Home.tsx index bbe1b6e..b5eac06 100644 --- a/src/routes/Home/Home.tsx +++ b/src/routes/Home/Home.tsx @@ -6,7 +6,6 @@ import MapView, { Callout, 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 { AnimatedMapView } from "react-native-maps/lib/MapView"; type LocationType = Location.LocationObject; export default function Home() { const [location, setLocation] = useState(null); @@ -31,34 +30,36 @@ export default function Home() { return; } if (status == "granted") { - let location = await Location.getCurrentPositionAsync({}); - if (location) { - setLocation(location); - getDistance(location); + let newLocation = await Location.getCurrentPositionAsync({}); + if (newLocation) { + // Only update location state if user's location has changed + if ( + !location || + newLocation.coords.latitude !== location.coords.latitude || + newLocation.coords.longitude !== location.coords.longitude + ) { + setLocation(newLocation); + GetDistanceRoundedOff(newLocation); + } } } } - // Refresh every 10 seconds + // Refresh every 15 seconds useEffect(() => { const interval = setInterval(() => { requestLocation(); - }, 10000); + }, 15000); return () => clearInterval(interval); }); - // Refresh when user moves location - useEffect(() => { - requestLocation(); - }, [location]); - // Run when screen loads useEffect(() => { requestLocation(); }, []); - async function getDistance(location: LocationType) { + async function GetDistanceRoundedOff(location: LocationType) { let dist = GetDistance( location.coords.latitude, location.coords.longitude, @@ -70,10 +71,10 @@ export default function Home() { function CustomMap() { if (dist && location) { - if (dist <= 2) { + if (dist >= 2) { // Just switch this condition for map debugging return ( - console.log(location)} + draggable + onDragEnd={(e) => { + const newLocation = e.nativeEvent.coordinate; + const distance = GetDistance( + newLocation.latitude, + newLocation.longitude, + location.coords.latitude, + location.coords.longitude + ); + console.log("Distance:", distance); + if (distance <= 0.1) { + // If the new location is within 100 meters of the actual location, update the location state + setLocation({ + ...location, + coords: { + ...location.coords, + latitude: newLocation.latitude, + longitude: newLocation.longitude, + }, + }); + } else { + // If the new location is more than 100 meters away from the actual location, reset the marker to the actual location + setLocation({ + ...location, + }); + } + }} pinColor={colors.primary_1} > @@ -125,7 +152,7 @@ export default function Home() { - + ); } else { return ( @@ -134,7 +161,7 @@ export default function Home() { You are too far from USTP {"\n"} Get closer to use Stud-E - - + {dist}km away from USTP {"\n"}