From e224f9d7649497ba6eb57272fc6235d08446bbd6 Mon Sep 17 00:00:00 2001 From: Jakub Satora Date: Fri, 11 Oct 2024 16:24:19 +0200 Subject: [PATCH] set location work --- app/(root)/(tabs)/home.tsx | 29 +++++++++++++++++++++++++++++ package-lock.json | 9 +++++++++ package.json | 1 + 3 files changed, 39 insertions(+) diff --git a/app/(root)/(tabs)/home.tsx b/app/(root)/(tabs)/home.tsx index 7501b31..8af3e35 100644 --- a/app/(root)/(tabs)/home.tsx +++ b/app/(root)/(tabs)/home.tsx @@ -1,12 +1,41 @@ import RideCard from '@/components/RideCard' +import * as Location from "expo-location" import { useUser } from '@clerk/clerk-expo' import { FlatList, View, Text, Image, ActivityIndicator, TouchableOpacity } from 'react-native' import { SafeAreaView } from 'react-native-safe-area-context' import { icons, images } from "@/constants"; import GoogleTextInput from '@/components/GoogleTextInput'; import Map from '@/components/map'; +import { useEffect, useState } from 'react'; +import { useLocationStore } from '@/store'; export default function Page() { + const { setUserLocation, setDestinationLocation } = useLocationStore() + + ; const [hasPermission, setHasPermission] = useState(false) + + useEffect(() => { + (async () => { + let { status } = await Location.requestForegroundPermissionsAsync(); + if (status !== "granted") { + setHasPermission(false); + return; + } + + let location = await Location.getCurrentPositionAsync({}); + + const address = await Location.reverseGeocodeAsync({ + latitude: location.coords?.latitude!, + longitude: location.coords?.longitude!, + }); + + setUserLocation({ + latitude: location.coords?.latitude, + longitude: location.coords?.longitude, + address: `${address[0].name}, ${address[0].region}`, + }); + })(); + }, []); const recentRides = [ [ { diff --git a/package-lock.json b/package-lock.json index 493c1d0..9da9ab5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "expo-constants": "~16.0.2", "expo-font": "~12.0.9", "expo-linking": "~6.3.1", + "expo-location": "^17.0.1", "expo-router": "~3.5.23", "expo-secure-store": "^13.0.2", "expo-splash-screen": "~0.27.5", @@ -11083,6 +11084,14 @@ "invariant": "^2.2.4" } }, + "node_modules/expo-location": { + "version": "17.0.1", + "resolved": "https://registry.npmjs.org/expo-location/-/expo-location-17.0.1.tgz", + "integrity": "sha512-m+OzotzlAXO3ZZ1uqW5GC25nXW868zN+ROyBA1V4VF6jGay1ZEs4URPglCVUDzZby2F5wt24cMzqDKw2IX6nRw==", + "peerDependencies": { + "expo": "*" + } + }, "node_modules/expo-modules-autolinking": { "version": "1.11.2", "resolved": "https://registry.npmjs.org/expo-modules-autolinking/-/expo-modules-autolinking-1.11.2.tgz", diff --git a/package.json b/package.json index ced1f41..99141d8 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "expo-constants": "~16.0.2", "expo-font": "~12.0.9", "expo-linking": "~6.3.1", + "expo-location": "^17.0.1", "expo-router": "~3.5.23", "expo-secure-store": "^13.0.2", "expo-splash-screen": "~0.27.5",