Ionic 3 : geolocation isn't stable across the different Android versions


#1

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:

  1. Android 4.x ( J1 Samsung) => works Correctly

  2. 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.

  3. 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

#2

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: https://ionic.zone/cordova/update


#3

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

#4

Here: https://www.npmjs.com/package/cordova-android?activeTab=versions (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.


#5

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!


#6

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.


#7

I am facing the same geolocation issue. Sometimes geolocation returns lat long undefined.


#8

Please help on some device my getcurrentposition doesnt get call. neither in success not in failure. Did anyone get the solution for this?.