question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android Capacitor geolocation not updating on getCurrentPosition

See original GitHub issue

Description of the problem:

I have two methods in my component for getting geolocation, one with capacitor, one with html5/js. The method with html5/js returns a new location each time I call it, the capacitor one does not, it either returns no location or the same location each time. There seems to be a correlation, if I get the location with html5 or even switch to another app that gets location, then the capacitor location will update, but subsequent calls will return the same coordinates/accuracy over an over, even if I move.

Affected platform

  • Android

OS of the development machine

  • Windows

Other information: Ionic/Angular 4.0.1

Testing on device with ‘ionic build’, ‘npx cap sync android’ and ‘npx cap open android’ debugging via android studio on samsung galaxy s7.

Capacitor version: 1.0.0-beta.17

node version: v10.15.0,

npm version: 6.4.1

Steps to reproduce:

async getCurrentPosition() {
    const { Geolocation } = Plugins;
    Geolocation.getCurrentPosition({timeout: 30000, enableHighAccuracy: true}).then(position => {
      this.position = position;
    }, error => console.log(error));
  }

  getCurrentPostionNavigator() {
    navigator.geolocation.getCurrentPosition((position) => {
      this.navPosition = position;
    }, err => {
      console.error(err);
      this.navPositionErr = `${err.code} ${err.message}`;
    }, { timeout: 30000, enableHighAccuracy: true });
  }
<ion-card>
    <ion-card-header>Capacitor Location</ion-card-header>
    <ion-card-content>
      <ion-button (click)="getCurrentPosition()">Get Position</ion-button>
      <div *ngIf="position">
        Lat: {{position.coords.latitude}}<br />
        Lng: {{position.coords.longitude}}<br />
        Accuracy: {{position.coords.accuracy}}
      </div>
      <span *ngIf="positionErr">{{positionErr}}</span>
    </ion-card-content>
  </ion-card>

  <ion-card>
    <ion-card-header>HTML5 Location</ion-card-header>
    <ion-card-content>
      <ion-button (click)="getCurrentPostionNavigator()">Get Position</ion-button>
      <div *ngIf="navPosition">
        Lat: {{navPosition.coords.latitude}}<br />
        Lng: {{navPosition.coords.longitude}}<br />
        Accuracy: {{navPosition.coords.accuracy}}
      </div>
      <span *ngIf="navPositionErr">{{navPositionErr}}</span>
    </ion-card-content>
  </ion-card>

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
derWebdesignercommented, Nov 8, 2019

@coffeymatt Is your example code working on a real Android device? I tried it multiple times now and watchPosition() is simply not working on Android for me. I also did set the timeout to 10000, but it never stops loading. Are you sure that this number is miliseconds? The documentation is unfortunately not saying anything about the dimensional unit.

@REPTILEHAUS Since this isn’t working on Android at all for me I guess the simulator should show the exact same behavior.

@jcesarmobile I am very sorry to ask, but if on Android getCurrentPosition() isn’t returning the current position, watchPosition() isn’t working at all and “Fused Location Provider” is planned for 2.0… is there any other option to solve this issue right now? Thank you very much for your feedback, really appreciate that.

3reactions
derWebdesignercommented, Dec 7, 2019

@ChariereFiedler Unfortunately not yet. getCurrentPosition() is somehow getting the geo position, but not the current one and only the last requested one by Google Maps for example, no matter which options I choose.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Geolocation fails in Android 11 (API 30) - Capacitor
I'm having a hard time debugging this issue. I'm using Capacitor's geolocation functionality (and have been for a while).
Read more >
Capacitor GeoLocation No Results On Certain Android Devices
Issue. We are using the Capacitor Geolocation plugin with the getCurrentPosition() method to capture the users current location.
Read more >
Geolocation Capacitor Plugin API
The Geolocation API provides simple methods for getting and tracking the current position of the device using GPS, along with altitude, heading, ...
Read more >
Geolocation.clearWatch() - Web APIs | MDN
The Geolocation.clearWatch() method is used to unregister location/error monitoring handlers previously installed using Geolocation.
Read more >
@capacitor/geolocation - npm
The Geolocation API provides simple methods for getting and tracking the current position of the device using GPS, along with altitude, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found