Ras*_*urn 12 ios cordova ionic-framework
我是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();
}
}
Run Code Online (Sandbox Code Playgroud)
然后我将其注入我的页面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();
}
}
Run Code Online (Sandbox Code Playgroud)
我的页面内容是:
<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>
Run Code Online (Sandbox Code Playgroud)
当我为iOS编译我的应用程序并在Xcode中运行它看起来像我期望的那样,当我点击"开始跟踪"按钮时,我看到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
Run Code Online (Sandbox Code Playgroud)
但是页面上显示的坐标似乎永远不会刷新并更新真实位置.
我可能错过了明显的,但看不到它,所以任何建议都会非常感激.
您现在可能已经解决了这个问题,但问题出在这一行:
BackgroundGeolocation.configure(config)
.then(this.locationUpdated) <-----
.catch((error) => {
console.log('BackgroundGeolocation error');
});
}
Run Code Online (Sandbox Code Playgroud)
所以你的回调将被触发,但它会丢失this的上下文。
您需要使用.bind()发送正确的上下文
BackgroundGeolocation.configure(config)
.then(this.locationUpdated.bind(this)) <-----
.catch((error) => {
console.log('BackgroundGeolocation error');
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1726 次 |
| 最近记录: |