Hello Every Body,
At first, Honestly, I’m disappointed about the Ionic Native documentation after adding geolocation plugin so that there is many thing not mentioned in the docs like the lines to add on AndroidManifest.xml to allow the use of GPS to our App.
So lets start, I’m working with an Ionic project based on geolocation and after testing my App on four real devices, I distinguished three different behaviors:
-
Android 4.x ( J1 Samsung) => works Correctly
-
Android 5.0.1 => The first time I install the App all works as expected but when I disable GPS and enable it again, the geolocation returns a position which is far about 1Km of my position and it still bugged until I restart the Phone and then it works correctly again until I restart GPS.
-
Android 5.1 => Same senario of the Anrdoid 5.0.1 but after turning GPS OFF and enabling it again, the geolocation still returning an error “timeout” until I restart the Phone.
Please I have to know if there is no solution or I can continue with this plugin ?
#Home.ts:
import { Component, ViewChild, ElementRef } from '@angular/core';
import { NavController, Platform } from 'ionic-angular';
import { Geolocation } from '@ionic-native/geolocation';
declare const google;
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
@ViewChild('map') mapElement: ElementRef;
map: any;
constructor(public navCtrl: NavController,
private platform:Platform,
private geolocation:Geolocation) {
}
ionViewDidLoad(){
new Promise(resolve => {
setTimeout(() => {
this.initMap();
resolve();
},2000);
})
.then(()=>{
setTimeout(() => {
this.getGeolocation();
},2000);
});
}
initMap() {
// map implementation
}
getGeolocation(){
this.platform.ready().then(() => {
alert('start geolocation');
var options = {
timeout: 20000,
enableHighAccuracy:true,
};
this.geolocation.getCurrentPosition(options).then(resp => {
alert('done');
console.log(resp.coords.latitude);
console.log(resp.coords.longitude);
let origin = new google.maps.LatLng( resp.coords.latitude,resp.coords.longitude);
this.addMarker(origin);
this.map.setCenter(origin);
}).catch((error) => {
this.debugError(error);
alert('error geoloc');
});
});
}
addMarker(location) {
//addMarker implementation
}
debugError(object){
var output = '';
for (var property in object) {
output += property + ': ' + object[property]+'; ';
}
alert(output);
console.log(output);
}
}
#ionic info:
cli packages: (/usr/lib/node_modules)
@ionic/cli-utils : 1.19.2
ionic (Ionic CLI) : 3.20.0
global packages:
cordova (Cordova CLI) : 7.1.0
local packages:
@ionic/app-scripts : 3.1.8
Cordova Platforms : android 6.3.0
Ionic Framework : ionic-angular 3.9.2
System:
Android SDK Tools : 26.1.1
Node : v8.9.4
npm : 5.7.1
OS : Linux 4.13
Environment Variables:
ANDROID_HOME : xxx
Misc:
backend : pro
cli packages: (/usr/lib/node_modules)
@ionic/cli-utils : 1.19.2
ionic (Ionic CLI) : 3.20.0
global packages:
cordova (Cordova CLI) : 7.1.0
local packages:
@ionic/app-scripts : 3.1.8
Cordova Platforms : android 6.3.0
Ionic Framework : ionic-angular 3.9.2
System:
Android SDK Tools : 26.1.1
Node : v8.9.4
npm : 5.7.1
OS : Linux 4.13
Environment Variables:
ANDROID_HOME : xxx/Sdk
Misc:
backend : pro
Your Cordova tooling seems to be out of date, which might be causing all kinds of problems.
You can read about how to figure out the current Cordova versions and how to update CLI, platforms and plugins here: How to update Cordova CLI, Platforms and Plugins · ionic.zone
Hi Sujan,
At the beginning I want to thank you for your helpful post,
I start by updating Cordova to 8.0.0 but I get an issue on updating the platform. So when I make update with the commands that I found in the article:
cordova platform rm android
cordova platform add android@7.1.0
Using cordova-fetch for cordova-android@7.1.0
(node:9550) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): CordovaError: Failed to fetch platform cordova-android@7.1.0
Probably this is either a connection problem, or platform spec is incorrect.
Check your connection and platform name/version/URL.
Error: npm: Command failed with exit code 1 Error output:
npm ERR! path /home/karimengineer/Downloads/Ionic/geoloc2/node_modules/.bin/create
npm ERR! code EEXIST
npm ERR! Refusing to delete /home/karimengineer/Downloads/Ionic/geoloc2/node_modules/.bin/create: ../cordova-android/bin/create symlink target is not controlled by npm /home/karimengineer/Downloads/Ionic/geoloc2/node_modules/cordova-android
npm ERR! File exists: /home/karimengineer/Downloads/Ionic/geoloc2/node_modules/.bin/create
npm ERR! Move it away, and try again.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/karimengineer/.npm/_logs/2018-03-28T14_16_37_787Z-debug.log
(node:9550) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Where to find the cordova-android@X.X.X available versions ?
When I make simply :
cordova platform rm android
cordova platform add android
It installs automatically the 6.3.0
$ ionic cordova platform add android
> cordova platform add android --save
Using cordova-fetch for cordova-android@6.3.0
Here: cordova-android - npm (although this is also described in the article I linked to)
That is a strange error. Never seen before and doesn’T make too much sense.
You could delete node_modules
, then execute the commands and finally run npm install
again.
The your update to Cordova CLI 8.0.0 did not work, that one would install 7.0.0 by default.
Hi Sujan, The adding of the plaftform android 7.1.0 finally succeed by deleting node_modules and package_lock.json and making npm i
after that ionic cordova platform add android
=> automatically set cordova-android@7.0.0 , and I have changed it manually to 7.1.0,
Fianally I have testen with cordova-android@7.0.0 & 7.1.0 and the geolocation plugin behavior still the same (require the restart of the phone to work correctly for most android devices).
Have you any idea about that ?
I think that the problem is on the geolocation plugin, What do you think about trying to downgrade the plugin version and testing ??
Otherwise, How can I contact Cordova plugin support ? is that any way to pass them the issue ?
OMG Sorry I forgot to post my ionic info:
cli packages: (/usr/lib/node_modules)
@ionic/cli-utils : 1.19.2
ionic (Ionic CLI) : 3.20.0
global packages:
cordova (Cordova CLI) : 8.0.0
local packages:
@ionic/app-scripts : 3.1.8
Cordova Platforms : android 7.1.0
Ionic Framework : ionic-angular 3.9.2
System:
Android SDK Tools : 26.1.1
Node : v8.9.4
npm : 5.7.1
OS : Linux 4.13
Environment Variables:
ANDROID_HOME : /home/karimengineer/Android/Sdk
Misc:
backend : pro
Thank you very much Sujan!
Great, best post your new ionic info
just to make sure.
In general it is always a good idea to open an issue with the Github repo of the Cordova plugin if you have problems. I know that Geolocation on Android is strange sometimes, maybe this is one of these cases.
1 Like
I am facing the same geolocation issue. Sometimes geolocation returns lat long undefined.
Please help on some device my getcurrentposition doesnt get call. neither in success not in failure. Did anyone get the solution for this?.
Hi all, well i have been following this topic and you conversation searching for a solution to my own problem (why geolocation isnt working on android oreo (8.0+)), after a lot of try and fail, found that the problem are the permissions… so the solution at least for my problem in the constructor of app.component.ts declare requestPermissions (hope it will be useful):
//app.components.ts
import { AndroidPermissions } from '@ionic-native/android-permissions';
.
.
.
constructor(
public platform: Platform,
...
androidPermissions: AndroidPermissions) {
platform.ready().then(()=>{
androidPermissions.requestPermissions([
androidPermissions.PERMISSION.ACCESS_COARSE_LOCATION,
androidPermissions.PERMISSION.ACCESS_FINE_LOCATION,
]);
...
});
}
ionic info
global packages:
@ionic/cli-utils : 1.5.0
Cordova CLI : 8.1.2 (cordova-lib@8.1.1)
Ionic CLI : 3.5.0
local packages:
@ionic/app-scripts : 1.3.12
Cordova Platforms : android 7.1.4
Ionic Framework : ionic-angular 3.5.0
System:
Node : v6.9.0
OS : Windows 10
npm : 3.10.8
Finally When I post this issue it was android 5.0.0, and the I end up finding that the issue is general issue, I find it in google maps also, when I’m in my position the geolocation every 15 sec change my position and put me in 50 meter error max, I tried also the geolocation when I’m in car and I found that the error become more and more when I run fast.