I'm developing a React app with Vite and using Capacitor to create an Android app. My app requires the user's current location on one page to display relevant results based on proximity. For this, I'm utilizing the @capacitor/geolocation plugin.
Here's how I've implemented location retrieval:
import { Geolocation } from '@capacitor/geolocation';
async function getLocationMobile(): Promise<{ lat: number; lng: number } | undefined> {
const { coarseLocation } = await Geolocation.checkPermissions();
if (coarseLocation !== 'granted') {
const { coarseLocation } = await getLocationPermission();
if (coarseLocation === 'granted') {
return getCurrentPosition();
}
} else {
return getCurrentPosition();
}
}
async function getCurrentPosition(): Promise<{ lat: number; lng: number } | undefined> {
const position = await Geolocation.getCurrentPosition();
if ('coords' in position) {
return {
lat: position.coords.latitude,
lng: position.coords.longitude,
};
}
}
And in my AndroidManifest.xml, I've specified:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
However, the Android Play Console rejected my app for not meeting the requirements to access location in the background. I don't require background location access, only when the relevant page is open.
Is there a way to configure @capacitor/geolocation or another approach to ensure the location is only accessed while the app is in use, thus adhering to Google's policy? I'm also open to hearing about alternative methods to retrieve the user's location besides using the @capacitor/geolocation plugin.