首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >离子5/角9:@mauron85/cordova-plugin-background-geolocation,不启动()

离子5/角9:@mauron85/cordova-plugin-background-geolocation,不启动()
EN

Stack Overflow用户
提问于 2020-08-27 02:48:31
回答 1查看 1.7K关注 0票数 0

我不明白为什么这个插件不能在我的应用程序中工作:https://ionicframework.com/docs/native/background-geolocation

我的代码:https://plnkr.co/edit/LAfbfU5edGhQmmkg?open=lib%2Fapp.ts&deferRun=1

它运行startBackgroundGeolocation(),但从未输入".subscribe",甚至没有在第54行的app.component.ts中输入".then()“块:

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
import { AngularFireDatabase } from '@angular/fire/database';
import * as firebase from 'firebase';
import * as moment from 'moment-timezone';
import { Platform } from '@ionic/angular';
import {
  BackgroundGeolocation,
  BackgroundGeolocationConfig,
  BackgroundGeolocationResponse,
  BackgroundGeolocationEvents
} from '@ionic-native/background-geolocation/ngx';

import { AuthService } from './auth/auth.service';


@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss']
})
export class AppComponent implements OnInit {
  positionSubscription: Subscription;

  constructor(
    private platform: Platform,
    private dbRt: AngularFireDatabase,
    private backgroundGeolocation: BackgroundGeolocation
  ) {
    this.initializeApp();
  }

  initializeApp() {
    this.platform.ready().then(() => {
      this.startBackgroundGeolocation();
    });
  }

  startBackgroundGeolocation() {
    const config: BackgroundGeolocationConfig = {
      desiredAccuracy: 10,
      stationaryRadius: 1,
      distanceFilter: 0,
      interval: 1000,
      fastestInterval: 1000,
      activitiesInterval: 1000,
      stopOnStillActivity: false,
      startForeground: true,
      startOnBoot: true,
      debug: true, //  enable this hear sounds for background-geolocation life-cycle.
      stopOnTerminate: false // enable this to clear background location settings when the app terminates
    };

    this.backgroundGeolocation.configure(config).then(() => {
      this.backgroundGeolocation
        .on(BackgroundGeolocationEvents.location)
        .subscribe((location: BackgroundGeolocationResponse) => {
          console.log(location);

          if (location.speed === undefined) {
            location.speed = 0;
          }

          this.dbRt.database
            .ref(`/gps/${this.authService.userprofile.value.uid}`)
            .push({
              date: moment.tz('Indian/Mauritius').format('DD-MM-YYYY HH:mm:ss'),
              timestamp: firebase.database.ServerValue.TIMESTAMP,
              platform: 'background GPS !!!'
            })
            .then((res) => {
              // this.backgroundGeolocation.finish(); // FOR IOS ONLY
            })
            .catch((error) => {
              // this.backgroundGeolocation.finish(); // FOR IOS ONLY
            });

          // IMPORTANT:  You must execute the finish method here to inform the native plugin that you're finished,
          // and the background-task may be completed.  You must do this regardless if your operations are successful or not.
          // IF YOU DON'T, ios will CRASH YOUR APP for spending too much time in the background.
        });
    });

    // start recording location
    this.backgroundGeolocation.start().then((state) => {
          console.log('state: ' + state);
    });
  }
}

它似乎甚至没有在第84行启动backgroundGeolocation:

代码语言:javascript
复制
this.backgroundGeolocation.start().then((state) => {
     console.log('state: ' + state);
});

我正在使用appflow创建apk,并在三星的S8 /Android7.0上进行测试,它甚至没有提示我授予任何访问权限,就像它对普通地理位置的访问一样。我希望获得backgroundGeolocation许可,但不会弹出任何东西。

当我尝试实现常规的地理定位插件(https://ionicframework.com/docs/native/geolocation)时,一切都很好,我第一次启动应用程序时就获得了地理定位许可,我可以将lat/lng保存到我的DB中。

EN

回答 1

Stack Overflow用户

发布于 2021-01-05 12:59:17

你已经安装了

desiredAccuracy: 10

要使它工作,你需要有新的位置,10米的距离,从旧的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63608415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档