GPS position when mobile app is in background (with ionicframework)

吃可爱长大的小学妹 提交于 2019-12-03 04:37:14

I just finished writing a new cordova background geolocation plugin for ios and android, its free and simple! try it out:

https://github.com/pmwisdom/cordova-background-geolocation-services

Ionic Usage :

//Wait for cordova / ionic ready to use the plugin
ionic.Platform.ready(function(){
    //Make sure to get at least one GPS coordinate in the foreground before starting background services
    navigator.geolocation.getCurrentPosition();

    //1. Get plugin
    var bgLocationServices =  window.plugins.backgroundLocationServices;

    //2. Configure Plugin
    bgLocationServices.configure({
         desiredAccuracy: 20, 
         distanceFilter: 5, 
         notificationTitle: 'BG Plugin', 
         notificationText: 'Tracking',
         debug: true, 
         interval: 9000, 
         fastestInterval: 5000
    });

    //3. Register a callback for location updates, this is where location objects will be sent in the background
    bgLocationServices.registerForLocationUpdates(function(location) {
         console.log("We got a BG Update" + JSON.stringify(location));
    }, function(err) {
         console.log("Error: Didn't get an update", err);
    });

    //4. Start the Background Tracker. When you enter the background tracking will start, and stop when you enter the foreground.
    bgLocationServices.start();


    ///later, to stop
    bgLocationServices.stop();

});

It's possible with a plugin you're referring as a $600+ premium plugin.

People usually forget that older versions are also available, including last viable Android/iOS version: https://github.com/christocracy/cordova-plugin-background-geolocation/tree/0.3.7

ionic plugin add cordova-plugin-background-geolocation@1.0.5

Currently, other distinct versions do not exist, everything else is just a fork of this original plugin.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!