2017-07-21 91 views
0

我试图按照Josh Morony的示例代码, 但我似乎无法找到如何解决此问题。无法解析LocationTracker的所有参数

我从控制台得到一个错误,它显示在webrowser上,甚至在设备上。

我已经加入了插件:

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

以下是错误消息

Uncaught Error: Can't resolve all parameters for LocationTracker: ([object Object], [object Object], [object Object], ?).at syntaxError (file:///android_asset/www/build/vendor.js:79310:34) 
    at CompileMetadataResolver._getDependenciesMetadata (file:///android_asset/www/build/vendor.js:92647:35) 
    at CompileMetadataResolver._getTypeMetadata (file:///android_asset/www/build/vendor.js:92515:26) 
    at CompileMetadataResolver._getInjectableMetadata (file:///android_asset/www/build/vendor.js:92501:21) 
    at CompileMetadataResolver.getProviderMetadata (file:///android_asset/www/build/vendor.js:92791:40) 
    at file:///android_asset/www/build/vendor.js:92720:49 
    at Array.forEach (native) 
    at CompileMetadataResolver._getProvidersMetadata (file:///android_asset/www/build/vendor.js:92681:19) 
    at CompileMetadataResolver.getNgModuleMetadata (file:///android_asset/www/build/vendor.js:92336:50) 
    at JitCompiler._loadModules (file:///android_asset/www/build/vendor.js:103400:66) 

页/家庭/ home.ts

import { Component } from '@angular/core'; 
import { NavController } from 'ionic-angular'; 
import { LocationTracker } from '../../providers/location-tracker/location-tracker'; 

@Component({ 
    selector: 'page-home', 
    templateUrl: 'home.html' 
}) 
export class HomePage { 

constructor(public navCtrl: NavController, public locationTracker: LocationTracker) { 

    this.startTracking(); 
    } 

    startTracking(){ 
    this.locationTracker.startTracking(); 
    } 

    stopTracking(){ 
    this.locationTracker.stopTracking(); 
    } 
} 

/页/家庭/家。 html

<ion-content padding> 
    <ion-label>Current Latitude: {{locationTracker.lat}</ion-label> 
    <ion-label>Current Longitude: {{locationTracker.lng}}</ion-label> 
</ion-content> 

/providers/location-tracker/location-tracker.ts

import { Injectable, NgZone } from '@angular/core'; 
import { BackgroundGeolocation } from '@ionic-native/background-geolocation'; 
import { Geolocation, Geoposition } from '@ionic-native/geolocation'; 
import 'rxjs/add/operator/map'; 
import 'rxjs/add/operator/filter'; 

@Injectable() 
export class LocationTracker { 

    public watch: any;  
    public lat: number = 0; 
    public lng: number = 0; 

    constructor(public zone: NgZone, public backgroundGeolocation: BackgroundGeolocation, public geolocation: Geolocation, public geoposition: Geoposition) { 

    } 

    startTracking() { 

    // Background Tracking 

    let config = { 
     desiredAccuracy: 0, 
     stationaryRadius: 20, 
     distanceFilter: 10, 
     debug: true, 
     interval: 2000 
    }; 

    this.backgroundGeolocation.configure(config).subscribe((location) => { 

     console.log('BackgroundGeolocation: ' + location.latitude + ',' + location.longitude); 

     // Run update inside of Angular's zone 
     this.zone.run(() => { 
     this.lat = location.latitude; 
     this.lng = location.longitude; 
     }); 

    }, (err) => { 

     console.log(err); 

    }); 

    // Turn ON the background-geolocation system. 
    this.backgroundGeolocation.start(); 


    // Foreground Tracking 

    let options = { 
     frequency: 3000, 
     enableHighAccuracy: true 
    }; 

    this.watch = this.geolocation.watchPosition(options).filter((p: any) => p.code === undefined).subscribe((position: Geoposition) => { 

     console.log(position); 

     // Run update inside of Angular's zone 
     this.zone.run(() => { 
     this.lat = position.coords.latitude; 
     this.lng = position.coords.longitude; 
     }); 

    }); 
    } 

    stopTracking() { 
    console.log('stopTracking'); 

    this.backgroundGeolocation.finish(); 
    this.watch.unsubscribe(); 
    } 
} 

应用程序/ app.module.ts

import { BrowserModule } from '@angular/platform-browser'; 
import { ErrorHandler, NgModule } from '@angular/core'; 
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular'; 
import { MyApp } from './app.component'; 
import { HomePage } from '../pages/home/home'; 
import { StatusBar } from '@ionic-native/status-bar'; 
import { LocationTracker } from '../providers/location-tracker/location-tracker'; 
import { BackgroundGeolocation } from '@ionic-native/background-geolocation'; 
import { Geolocation } from '@ionic-native/geolocation'; 


@NgModule({ 
    declarations: [ 
    MyApp, 
    HomePage 
    ], 
    imports: [ 
    BrowserModule, 
    IonicModule.forRoot(MyApp), 
    HttpModule 
    ], 
    bootstrap: [IonicApp], 
    entryComponents: [ 
    MyApp, 
    HomePage 
    ], 
    providers: [ 
    StatusBar 
    {provide: ErrorHandler, useClass: IonicErrorHandler} 
    LocationTracker, 
    BackgroundGeolocation, 
    Geolocation 
    ] 
}) 
export class AppModule {} 
+0

它可以是离子2或3,但不能同时: )在标签 – onetwo12

+0

嗨onetwo12, 你指的是标签? –

+0

我明白你的意思了。我假设这个错误可能在两者中。 –

回答

2

如果taje看看错误,它说,第四参数无法注入

Uncaught Error: Can't resolve all parameters for LocationTracker: ([object Object], [object Object], [object Object], ?) 

看你的代码从注射

constructor(public zone: NgZone, 
      public backgroundGeolocation: BackgroundGeolocation, 
      public geolocation: Geolocation, 
      public geoposition: Geoposition) { 
} 

第四个是这个Geoposition,看起来你不是'提供'的NgModule。

所以,只要你不使用这个“geoposition”实例/对象,你可以简单地从构造函数中删除:

constructor(public zone: NgZone, 
      public backgroundGeolocation: BackgroundGeolocation, 
      public geolocation: Geolocation) { 
} 
+0

不错..你的解释帮助我理解如何更好地阅读错误... – JGFMK

+0

非常感谢!我现在看到它。一旦我在明天进行测试,我会做出答案。 –

+0

你解释得很好,准确。 –

相关问题