我是Ionic和Typescript的新手,如果我有关于脸的屁股,请道歉!
我尝试使用Ionic 2构建一个简单的iOS和Android应用程序,只显示用户当前位置,并在移动时进行更新。
我面临的问题是,当我似乎正在通过UI获取坐标位置时,永远不会更新以显示更新的位置。
我使用cordova-plugin-mauron85-background-geolocation插件创建了一个提供程序作为location.ts:
import { Injectable, NgZone} from '@angular/core';
import { BackgroundGeolocation } from 'ionic-native';
@Injectable()
export class LocationProvider {
public message: any = "I'm new here";
public latitude:number = 0.0;
public longitude:number = 0.0;
private zone: NgZone;
constructor() {
this.initialiseLocationManager();
}
initialiseLocationManager() {
let config = {
desiredAccuracy: 1,
stationaryRadius: 1,
distanceFilter: 1,
interval:1000,
activityType:'AutomotiveNavigation',
debug: true,
stopOnTerminate: true,
};
BackgroundGeolocation.configure(config)
.then(this.locationUpdated)
.catch((error) => {
console.log('BackgroundGeolocation error');
});
}
private locationUpdated(location) {
console.log('******* NEW LOCATION ********');
this.zone.run(() => {
this.latitude = location.latitude;
this.longitude = location.longitude;
});
BackgroundGeolocation.finish(); // FOR IOS ONLY
}
private error(error) {
console.log('ERROR');
}
public startTracking() {
BackgroundGeolocation.start();
}
public stopTracking() {
BackgroundGeolocation.stop();
}
}
然后我将此注入我的页面test.ts:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {LocationProvider} from '../../providers/location/location';
@Component({
templateUrl: 'build/pages/test/test.html',
})
export class TestPage {
constructor(private nav: NavController, private locationProvider:LocationProvider) {
}
startTracking() {
this.locationProvider.startTracking();
}
stopTracking() {
this.locationProvider.stopTracking();
}
}
我的页面内容是:
<ion-header>
<ion-navbar>
<button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title>Test</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<button (click)="startTracking()">
Start Tracking
</button>
<button (click)="stopTracking()">
Stop Tracking
</button>
<ion-item>Latitude / Longitude</ion-item>
<ion-item>
{{locationProvider.latitude}},{{locationProvider.longitude}}
</ion-item>
</ion-content>
当我为iOS编译我的应用程序并在Xcode中运行时,它看起来像我期望的那样,当我点击&#34;开始跟踪&#34;按钮我看到Xcode控制台中的条目表明正确地获得了位置,即
2016-08-06 12:37:17:370 Test[3264:62403] Better location found: Location: id=0 time=140310177131584 lat=51.8724580445 lon=-2.26443678245263 accu=5 aaccu=-1 speed=-1 head=-1 alt=0 type=current
2016-08-06 12:37:17:371 Test[3264:62403] LocationManager queue Location: id=0 time=140310177131584 lat=51.8724580445 lon=-2.26443678245263 accu=5 aaccu=-1 speed=-1 head=-1 alt=0 type=current
2016-08-06 12:37:18:370 Test[3264:62403] LocationManager didUpdateLocations (operationMode: 1)
2016-08-06 12:37:18:370 Test[3264:62403] Location age 0.001122
2016-08-06 12:37:18:370 Test[3264:62403] Better location found: Location: id=0 time=140310177372752 lat=51.8726980292 lon=-2.26504136905789 accu=5 aaccu=-1 speed=-1 head=-1 alt=0 type=current
2016-08-06 12:37:18:370 Test[3264:62403] LocationManager queue Location: id=0 time=140310177372752 lat=51.8726980292 lon=-2.26504136905789 accu=5 aaccu=-1 speed=-1 head=-1 alt=0 type=current
但页面上显示的坐标似乎永远不会刷新并更新真实位置。
我可能错过了显而易见的,但无法看到它,所以任何建议都会非常感激。
答案 0 :(得分:0)
您现在可能已经解决了这个问题,但问题出在这一行:
BackgroundGeolocation.configure(config)
.then(this.locationUpdated) <-----
.catch((error) => {
console.log('BackgroundGeolocation error');
});
}
所以你的回调会被解雇,但它会失去 this 的上下文。
您需要使用 .bind()
发送相应的正确上下文BackgroundGeolocation.configure(config)
.then(this.locationUpdated.bind(this)) <-----
.catch((error) => {
console.log('BackgroundGeolocation error');
});
}