I am trying to get my current location in ionic2 with the following code:
Geolocation.getCurrentPosition() .then((resp) => {
let position = new google.maps.LatLng(resp.coords.latitude, resp.coords.longitude);
...
}) .catch((error)=>{
console.log(error)
})
However android studio gives me the following error when running on android device:
E/cr_LocationProvider: Caught security exception while registering for location updates from the system. The application does not have sufficient geolocation permissions.
If I hard code the coordinates, the map works!
Anyone had the same issue?
edit:
I have the following in my android manifest:
In my index.html (this should not be a problem since I used the same script src to test without geolocation and it worked) <script src="https://maps.googleapis.com/maps/api/js?key=MY_REAL_KEY&libraries=places,DirectionsService,DirectionsRenderer,Geocoder"></script>
Chances are what happened is you installed an older version of cordova-android, which did not have the changes needed for geolocation permissions. I have tested out the latest release and it seems to work fine.
cli packages: (/usr/local/lib/node_modules)
@ionic/cli-plugin-proxy : 1.5.5
@ionic/cli-utils : 1.19.0
ionic (Ionic CLI) : 3.19.0
global packages:
cordova (Cordova CLI) : 6.5.0
local packages:
@ionic/app-scripts : 2.0.2
Cordova Platforms : android 6.1.2 ios 4.3.1 windows 4.4.3
Ionic Framework : ionic-angular 3.5.3
System:
Android SDK Tools : 25.2.3
Node : v6.11.2
npm : 5.3.0
OS : macOS High Sierra
Xcode : Xcode 9.2 Build version 9C40b
Environment Variables:
ANDROID_HOME : /Users/ronaiza.cardoso/Library/Android/sdk
HTTP_PROXY : not set
http_proxy : not set
HTTPS_PROXY : not set
https_proxy : not set
IONIC_HTTP_PROXY : not set
PROXY : not set
proxy : not set
Misc:
backend : pro
The error you’re encountering seems to be related to insufficient geolocation permissions in your Android application. You’ve declared the necessary permissions in your AndroidManifest.xml file, which is a good start. However, you should also ensure that you have requested runtime permissions in your Ionic application because Android requires explicit user consent for location access.
To request runtime permissions in an Ionic application, you can use the Cordova plugin ``cordova-plugin-android-permissions. Here’s how you can do it:
Request location permissions in your code before attempting to use the Geolocation API. You can add this code to your Ionic component where you want to use geolocation:
import { AndroidPermissions } from '@ionic-native/android-permissions/ngx';
constructor(private androidPermissions: AndroidPermissions) {}
requestLocationPermission() {
this.androidPermissions.requestPermissions([
this.androidPermissions.PERMISSION.ACCESS_FINE_LOCATION,
this.androidPermissions.PERMISSION.ACCESS_COARSE_LOCATION
]).then((result) => {
if (result.hasPermission) {
// You have permission; you can now use Geolocation.getCurrentPosition()
} else {
console.error('Location permission denied');
}
}).catch((error) => {
console.error('Error requesting location permission', error);
});
}
getLocation() {
this.requestLocationPermission();
Geolocation.getCurrentPosition().then((resp) => {
let position = new google.maps.LatLng(resp.coords.latitude, resp.coords.longitude);
// ... your code here
}).catch((error) => {
console.log(error);
});
}
In this code, we first import the AndroidPermissions plugin, then request the required location permissions using requestPermissions(). If the user grants the permissions, you can proceed with Geolocation.getCurrentPosition().
Remember to call the getLocation() function when you want to retrieve the user’s location.
Also, make sure you have imported the AndroidPermissions module in your app module (app.module.ts) and added it to the providers array. This enables the injection of the AndroidPermissions service.
After implementing these changes, your Ionic application should properly request and handle location permissions on Android devices, allowing you to use the Geolocation API without issues.
I tried this on my location and it seems to be working fine
Thanks, for me : Android 33 adding permission do androidManifest.xml works well, why do you need this extra step ?
My only problem using capacitor geolocation, is the watch method… which is very slow in acquiring the coordinates, and there is no parameter to tweak it.
I need near real-time tracking of the device…like google maps, but I’m using leaflet.
I improve the acquisition rate, by changing the code of the plugin geolocation, but it’s some sort of a hack… I need a bulletproof solution.