README
capacitor-gray-geolocation
Description
Geolocation plugin that uses the fused location service instead of the native API.
Getting a location under android is quite difficult. The standard API implemented now in capacitor returns the GPS provider which results in never getting a position indoors. This is not the case under iOS. A better way under Android is the FusedLocationProvider which already handles that.
This Plugin also has method calls to turn on location.
Supported platforms
- Android
- Web
Android setup
npm i capacitor-gray-geolocation
ionic build
npx cap copy
npx cap sync
npx cap open android
[extra step]
in android case we need to tell Capacitor to initialise the plugin:
on your
MainActivity.java
file addimport com.grayhat.geolocation.GrayGeolocation;
and then inside the init callbackadd(GrayGeolocation.class);
Now you should be set to go. Try to run your client using npx cap open android
.
Tip: every time you change a native code you may need to clean up the cache (Build > Clean Project | Build > Rebuild Project) and then run the app again.
Example
import { Plugins } from "@capacitor/core";
import "capacitor-gray-geolocation";
const { GrayGeolocation } = Plugins;
// ...code
//function to get location (handle errors yourself)
getLocation = async() => {
let deviceInfo = await Plugins.Device.getInfo();
if(deviceInfo.platform === "ios") {
//use the capacitor Plugins.Geolocation for geolocation
return;
}
let locationOn = await GrayGeolocation.turnLocationOn();
if(locationOn.res) {
let coords = await GrayGeolocation.getCurrentPosition();
console.log("latitude :",coords.latitude,"longitude :",coords.longitude);
return coords;
}
else{
//failed to turn on location : User declined
}
}
Testing
Manually tested against the following platforms:
- Android device 10.0 (API Level 29)