I am using the native Ionic 2 Geolocation plugin and it does not work as expected. First, I add the plugin.
ionic plugin add cordova-plugin-geolocation
I then import the plugin and issue a call to get the current position.
import { Geolocation } from 'ionic-native';
import { Geoposition, GeolocationOptions } from 'ionic-native/dist/plugins/geolocation';
export class MyComponent {
protected getLocation():Promise<Geoposition> {
let options:GeolocationOptions = {
maximumAge: 0, timeout: 5000, enableHighAccuracy: false
};
return Geolocation.getCurrentPosition(options)
.catch(error => { console.error(error.message) };
}
}
I've also checked my AndroidManifest.xml.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
I noticed that the timeout is always exceeded. If I leave it to the default (timeout is set to infinity by default), then around 20 minutes later, I do actually get a geo position. Of course, 20 minutes is not ideal.
I also tried using the navigator
as follows. But I get the exact same results.
navigator.geolocation.getCurrentPosition(
position => {
console.info('using navigator');
console.info(position.coords.latitude);
console.info(position.coords.longitude);
},
error => {
console.warn('using navigator');
console.warn(error.code);
console.warn(error.message);
},
options
);
When testing on Chrome
, the code works as expected; the only difference is that I get a popup asking if I want to allow the page to access my location.
I saw a lot of people have the same problem on SO with Ionic too, but that is with version 1. I wanted to ask this question in the context of Ionic 2.
By the way, I've also updated Cordova and the version is showing up as 6.0.2.
npm update -g cordova
Version information
If location is switched off, you can use switchToLocationSettings() to open the device location settings page to allow user to enable location services, or you can use the cordova-plugin-request-location-accuracy to request the desired location accuracy without needing the user to manually do this on the location ...
Use this code:
if (navigator.geolocation) {
var options = {
enableHighAccuracy: true
};
navigator.geolocation.getCurrentPosition(position=> {
console.info('using navigator');
console.info(position.coords.latitude);
console.info(position.coords.longitude);
}, error => {
console.log(error);
}, options);
}
Remember to import Geolocation
:
import { Geolocation } from 'ionic-native';
And add plugin:
ionic plugin add cordova-plugin-geolocation
Probably I found a solution. The problem was with mine GPS module. Eventually it stopped find my location at any site. I reloaded my phone and now everything works.
So at first check if you can determine your location on site such maps.google.com. If it can't determine your location, then the problem is with your phone. Try to reload it or reflash it.
Install both cordova dependencies
cordova plugin add cordova-plugin-geolocation
cordova plugin add cordova-plugin-whitelist
Add this permissions to AndroidMainfest.xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
Please keep in mind, when running ionic cordova run android -l geolocation will throw this unsecure error. Test it on ur browser or do a simple run with
ionic cordova run android
It solved the problem for me, hope it helps someone else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With