capacitor-fused-location

npm version Vulnerabilities License

Description

Geolocation plugin that uses the fused location service instead of the native API.

Getting a location under android is quite difficult. The standard API implemented now in capacitor returns the GPS provider which results in never getting a position indoors. This is not the case under iOS. A better way under Android is the FusedLocationProvider which already handles that.

This plugin currently depends on RxLocation and was initially inspired by this issue/pull request

Supported platforms

  • Android

Android setup

  • ionic start my-cap-app --capacitor
  • cd my-cap-app
  • npm install --save @jonoj/capacitor-fused-location
  • mkdir www && touch www/index.html
  • npx cap add android
  • npx cap sync android
  • npx cap open android
  • [extra step] in android case we need to tell Capacitor to initialise the plugin:

on your MainActivity.java file add import com.jonoj.plugin.FusedLocation; and then inside the init callback add(FusedLocation.class);

Now you should be set to go. Try to run your client using ionic cap run android --livereload.

Tip: every time you change a native code you may need to clean up the cache (Build > Clean Project | Build > Rebuild Project) and then run the app again.

Example

import {FusedLocationPlugin} from '@jonoj/capacitor-fused-location';

const fusedLocation = new FusedLocationPlugin();

class FusedFGeolocationExample {
  async getFusedLocation() {
        if (!(this.platform.is('hybrid') && !this.platform.is('android'))) {
            console.log('only android support!');
            return;
        }
    const coordinates = await fusedLocation.getCurrentPosition();
    console.log('Current', coordinates);
  }

  watchPosition() {
    if (!(this.platform.is('hybrid') && !this.platform.is('android'))) {
        console.log('only android support!');
        return;
    }
    const wait = fusedLocation.watchPosition({}, (position, err) => {
    })
  }
}

Testing

Manually tested against the following platforms:

  • Android emulator 10.0 (API Level 28)
  • Android emulator 7.0 (API Level 24)
  • Android device 6.0 (API Level 23)