File size: 4,321 Bytes
71638d4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | import * as Location from 'expo-location';
import { Linking, Alert, Platform } from 'react-native';
import { LocationData } from '../types';
export async function checkLocationServicesEnabled(): Promise<boolean> {
const enabled = await Location.hasServicesEnabledAsync();
return enabled;
}
export async function requestLocationPermission(): Promise<boolean> {
const { status } = await Location.requestForegroundPermissionsAsync();
return status === 'granted';
}
export async function ensureLocationEnabled(): Promise<{ enabled: boolean; permission: boolean }> {
const enabled = await checkLocationServicesEnabled();
if (!enabled) {
return { enabled: false, permission: false };
}
const permission = await requestLocationPermission();
return { enabled: true, permission };
}
export async function promptEnableLocation(): Promise<void> {
Alert.alert(
'Location Required',
'GPS must be enabled to report issues. This ensures accurate location data and prevents fraudulent reports.',
[
{ text: 'Cancel', style: 'cancel' },
{
text: 'Open Settings',
onPress: () => {
if (Platform.OS === 'ios') {
Linking.openURL('app-settings:');
} else {
Linking.openSettings();
}
}
},
]
);
}
export async function getCurrentLocation(
minAccuracy: number = 20,
timeout: number = 30000
): Promise<LocationData> {
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
const location = await Location.getCurrentPositionAsync({
accuracy: Location.Accuracy.BestForNavigation,
});
if (location.coords.accuracy !== null && location.coords.accuracy <= minAccuracy) {
return {
latitude: location.coords.latitude,
longitude: location.coords.longitude,
accuracy: location.coords.accuracy,
heading: location.coords.heading ?? undefined,
altitude: location.coords.altitude ?? undefined,
};
}
await new Promise((resolve) => setTimeout(resolve, 1000));
}
const location = await Location.getCurrentPositionAsync({
accuracy: Location.Accuracy.High,
});
return {
latitude: location.coords.latitude,
longitude: location.coords.longitude,
accuracy: location.coords.accuracy ?? 999,
heading: location.coords.heading ?? undefined,
altitude: location.coords.altitude ?? undefined,
};
}
export function isLocationAccurate(accuracy: number, threshold: number = 15): boolean {
return accuracy <= threshold;
}
export async function watchLocationWithGpsCheck(
onLocationUpdate: (location: LocationData) => void,
onGpsStatusChange: (enabled: boolean) => void,
accuracyThreshold: number = 15
): Promise<() => void> {
let subscription: Location.LocationSubscription | null = null;
let gpsCheckInterval: ReturnType<typeof setInterval> | null = null;
const checkGpsAndStart = async () => {
const enabled = await checkLocationServicesEnabled();
onGpsStatusChange(enabled);
if (enabled && !subscription) {
subscription = await Location.watchPositionAsync(
{
accuracy: Location.Accuracy.BestForNavigation,
timeInterval: 1000,
distanceInterval: 1,
},
(newLocation) => {
const accuracy = newLocation.coords.accuracy ?? 999;
onLocationUpdate({
latitude: newLocation.coords.latitude,
longitude: newLocation.coords.longitude,
accuracy: accuracy,
heading: newLocation.coords.heading ?? undefined,
altitude: newLocation.coords.altitude ?? undefined,
});
}
);
} else if (!enabled && subscription) {
subscription.remove();
subscription = null;
}
};
await checkGpsAndStart();
gpsCheckInterval = setInterval(async () => {
const enabled = await checkLocationServicesEnabled();
onGpsStatusChange(enabled);
if (!enabled && subscription) {
subscription.remove();
subscription = null;
} else if (enabled && !subscription) {
await checkGpsAndStart();
}
}, 3000);
return () => {
if (subscription) {
subscription.remove();
}
if (gpsCheckInterval) {
clearInterval(gpsCheckInterval);
}
};
}
|