我不明白为什么这个插件不能在我的应用程序中工作: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()“块:
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:
this.backgroundGeolocation.start().then((state) => {
console.log('state: ' + state);
});我正在使用appflow创建apk,并在三星的S8 /Android7.0上进行测试,它甚至没有提示我授予任何访问权限,就像它对普通地理位置的访问一样。我希望获得backgroundGeolocation许可,但不会弹出任何东西。
当我尝试实现常规的地理定位插件(https://ionicframework.com/docs/native/geolocation)时,一切都很好,我第一次启动应用程序时就获得了地理定位许可,我可以将lat/lng保存到我的DB中。
发布于 2021-01-05 12:59:17
你已经安装了
desiredAccuracy: 10
要使它工作,你需要有新的位置,10米的距离,从旧的。
https://stackoverflow.com/questions/63608415
复制相似问题