首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Ionic 2中实现地理位置跟踪的错误

在Ionic 2中实现地理位置跟踪的错误
EN

Stack Overflow用户
提问于 2017-11-26 11:34:06
回答 1查看 192关注 0票数 0

我正在尝试在我的新项目中实现地理定位。我已经安装了以下插件并在app.module.ts中添加了它们

ionic cordova plugin add cordova-plugin-mauron85-background-geolocation npm install --save @ionic-native/background-geolocation

我正在跟踪本教程,但在home.ts中出现了错误。下面是我的home.ts代码。

代码语言:javascript
复制
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找不到名称“config”

EN

回答 1

Stack Overflow用户

发布于 2017-11-26 17:24:21

正如@SurajRao在他的评论中指出的那样,您需要将代码移动到类构造函数中或将其包装在方法中。

下面是放置在构造函数中的代码的组件:

代码语言:javascript
复制
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();
  }

}

阅读更多关于MDN上的JavaScript类的信息。

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

https://stackoverflow.com/questions/47495673

复制
相关文章

相似问题

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