我正在尝试在我的新项目上实现地理位置。我已安装以下插件并将其添加到app.module.ts
ionic cordova plugin add cordova-plugin-mauron85-background-geolocation
npm install --save @ionic-native/background-geolocation
我正在关注this tutorial,但在home.ts
中收到错误。以下是我的home.ts
代码。
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { BackgroundGeolocation, BackgroundGeolocationConfig, BackgroundGeolocationResponse } from '@ionic-native/background-geolocation';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(private backgroundGeolocation: BackgroundGeolocation,public navCtrl: NavController) {
}
const config: BackgroundGeolocationConfig = {
desiredAccuracy: 10,
stationaryRadius: 20,
distanceFilter: 30,
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)
.subscribe((location: BackgroundGeolocationResponse) => {
console.log(location);
// 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 HTTP request is successful or not.
// IF YOU DON'T, ios will CRASH YOUR APP for spending too much time in the background.
//this.backgroundGeolocation.finish(); // FOR IOS ONLY
});
// start recording location
this.backgroundGeolocation.start();
// If you wish to turn OFF background-tracking, call the #stop method.
this.backgroundGeolocation.stop();
}
错误在这一行:this.backgroundGeolocation.configure(config)
。在this
,它正在说;
[ts]意外的令牌。期望构造函数,方法,访问器或属性。
在配置上它说:
[ts]找不到名称'config'
答案 0 :(得分:0)
当@SurajRao指向他的评论时,你需要在类构造函数中移动代码或将其包装在方法中。
这是你的组件,代码放在构造函数中:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { BackgroundGeolocation, BackgroundGeolocationConfig, BackgroundGeolocationResponse } from '@ionic-native/background-geolocation';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(private backgroundGeolocation: BackgroundGeolocation,public navCtrl: NavController) {
const config: BackgroundGeolocationConfig = {
desiredAccuracy: 10,
stationaryRadius: 20,
distanceFilter: 30,
debug: true,
stopOnTerminate: false,
};
this.backgroundGeolocation.configure(config)
.subscribe((location: BackgroundGeolocationResponse) => {
console.log(location);
});
this.backgroundGeolocation.start();
this.backgroundGeolocation.stop();
}
}