【发布时间】:2019-07-08 01:52:12
【问题描述】:
我正在使用 ionic 4,并尝试从 GPS 获取用户位置,特别是每 1 小时间隔一次。所以它是一个每小时跟踪用户位置的应用程序。
我从他们的官方网站上使用了 Ionic 4 背景位置插件文档,并找到了安装这个插件的代码,它说这个插件即使在应用程序退出时也会运行,所以我希望它是我需要的。
我已使用以下代码将 Background-Geolocation 插件安装到 Ionic 4 应用程序:
ionic cordova plugin add cordova-plugin-mauron85-background-geolocation@alpha
npm install @ionic-native/background-mode
但是当我尝试测试它之后,在我的 app.component.ts 主文件中,代码失败了!
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
//setting options for backgroun-geolocatuion
const config: BackgroundGeolocationConfig = {
desiredAccuracy: 10,
stationaryRadius: 20,
distanceFilter: 30,
debug: true,
stopOnTerminate: false,
};
this.backgroundGeolocation.configure(config)
.subscribe((location: BackgroundGeolocationResponse) => {
// ERROR comes here ==> *subscribe* does not exist on type Promise<any> ???
alert(location.longitude);
});
this.backgroundGeolocation.start();
});
它显示了这个错误:
// ERROR comes here ==> Subscribe does not exist on type Promise<any>
由于此错误,我无法将返回的数据作为订阅数据获取。我需要每 1 小时间隔一次的用户位置数据(即使用户移动与否),但此订阅显示错误,如上所述。
如果我将subscribe 更改为then,那么它会返回一次数据,这太未定义了。
我需要此代码与订阅一起运行,以每小时获取定期跟踪的用户信息。
我已经在代码上方和主模块文件中导入了所有提供程序和构造函数。
进口:
app/app.component.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { BackgroundGeolocation } from '@ionic-native/background-geolocation/ngx';
@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
providers: [
StatusBar,
BackgroundGeolocation,
SplashScreen,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule {}
【问题讨论】:
-
你能显示这个文件的导入和构造函数吗?
-
是的,我现在编辑了我的查询。
-
如果我使用“then”代替“subscribe”,我会得到未定义的 location.longitude,并且只有一次。
标签: angular ionic-framework ionic3 ionic4