GPS在Cordova应用程序中的背景中坐标 [英] GPS coordinates in background in cordova app

查看:114
本文介绍了GPS在Cordova应用程序中的背景中坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用ionic/cordova进行混合应用程序. 该应用程序需要具有以下功能:每隔1分钟左右对其位置执行一次ping通我们的后端,如果附近有任何有趣的事情,后端API就会回答. 如果答案是肯定的,则该应用程序将在本地通知中排队,这有望使用户打开该应用程序. 当应用程序处于后台模式甚至手机处于锁定状态时,都需要使用此功能. 该应用必须能够同时部署到应用商店,Google Play和Windows Phone.

I'm currently doing a hybrid app using ionic/cordova. The app needs functionality where it pings our backend with its location every 1 minute or so and the backend API will answer if anything interesting is nearby. If the answer is yes the app will queue a local notification which hopefully will make the user open the app. This functionality is needed when the app is in background mode and even when the phone is locked. The app needs to be able to be deployed to both app store, google play and eventually windows phone.

我目前正在使用这三个插件的组合:

I'm currently using a combination of these three plugins:

https://www.npmjs.com/package/cordova-plugin-geolocation -用于定位 https://github.com/katzer/cordova-plugin-background-mode -对于背景模式 https://github.com/katzer/cordova-plugin-local-notifications -用于本地通知

https://www.npmjs.com/package/cordova-plugin-geolocation - for location https://github.com/katzer/cordova-plugin-background-mode - for bg mode https://github.com/katzer/cordova-plugin-local-notifications - for local notifications

当设备未锁定(因此它可以在前景和背景模式下运行)时,当前在Android上可以使用,但是当设备锁定时,它无法获取GPS坐标.

This currently works on Android when the device is not locked (so it works in foreground and background mode) but when the device is locked it is unable to get the GPS coordinates.

我的代码当前如下所示:

My code currently looks like this:

        // Enable background worker          
        (cordova as any).plugins.backgroundMode.enable();

   intervalPromise = $interval(intervalWork, 30000, 0, false);

    function intervalWork() {
        $log.log('Trying to fetch pos');

        var options = { maximumAge: 30000, timeout: 30000, enableHighAccuracy: false };

        navigator.geolocation.getCurrentPosition(success,
            err,
            options);
    }

    function success(pos) {
        $log.log("lat: " + pos.coords.latitude + " long: " + pos.coords.longitude);

        var Checkin = $resource(ApiDataEndpoint.url + 'checkin/:lat/:lng/', {});

        var res= Checkin.get({ lat: pos.coords.latitude, lng: pos.coords.longitude });

               if (res) { 

                $cordovaLocalNotification.schedule({
                    id: 1,
                    title: 'test',
                    text: 'test',
                }).then(function(result) {
                    $log.log("ok");
                });
            };
         }

所以...我的问题是:

So... my questions are:

1)如何在设备锁定时使解决方案正常工作(即使设备锁定但返回超时也会调用getCurrentPosition)?

1) How to get the solution to work when my device is locked (the getCurrentPosition is called even when device is locked but returns timeout)?

2)是否可以使此解决方案在iOS上运行?

2) Is it possible to get this solution to work on iOS?

3)这样制作的应用是否会在Google Play和应用商店中获得批准?

3) Will an app made this way be approved in google play and app store?

4)如果该项目注定要失败,我有什么选择?

4) If the project is doomed what are my alternatives?

我真的需要这方面的帮助!

I really need help on this one!

推荐答案

所以我目前有一个应用程序可以解决上面列出的所有问题,这是我正在使用的插件:

So I currently have an app that addresses all the issues you listed above and here's the plugin I'm using:

https://github.com/mauron85/cordova-plugin-background-geolocation

  1. 该插件利用了watchPosition()而不是getCurrentPosition(),因为此插件花费的时间太长,无法持续ping设备并消耗更多的电池电量.

  1. The plugin makes use of watchPosition() not getCurrentPosition() as this one takes too long to constantly ping the device and consumes more battery power.

这绝对适用于Android& iOS,但恕我直言,它在精度和保持活动性方面比Android更好.

This will definitely work for Android & iOS but IMHO it plays nicer with Android than the latter, as far as precision and the keep alive functionality.

我没问题将它插入Google Play,Apple确实允许使用此插件,Apple商店中有许多使用该插件的应用程序,但是Apple可能会最初拒绝它,并询问应用程序的背景使用意图,然后,您必须对应用程序在后台执行的操作提出上诉,并确保它不会无限期运行(这是我的经验).

I got it into Google Play no problem, Apple does allow this plugin, there are a number of apps using this plugin in the Apple store but Apple will probably initially reject it and ask the apps intention of background usage, you will then have to make an appeal as for what the app is doing in the background and make sure that it doesn't run indefinitely (this was my experience).

a.您还将要确保您指出Apple Pepe,即用户可以通过某种方式关闭背景地理位置跟踪.我假设有吗?这是使用插件的主要问题.

a. You're going to also want to make sure you point out to the Apple peeps that there is a way for the User to turn the background geolocation tracking off. I'm assuming there is? That's their main issue with the usage of the plugin.

祝你好运.

这篇关于GPS在Cordova应用程序中的背景中坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆