如果我在启用GPS的情况下启动,我的Ionic 3天气应用程序可以正常工作。
如果我在关闭全球定位系统的情况下启动应用程序,并订阅了监听更改(参见下面的watch.subscribe),尽管随后手动启用它,但它无法恢复纬度和经度。我想帮助控制GPS,也就是“监听”on和off事件,以便能够对其进行操作。谢谢!
this.geolocation.getCurrentPosition().then((resp) => {
this.global.latitude = resp.coords.latitude
this.global.longitude = resp.coords.longitude
}).catch((error) => {
alert('Error getting location ' + error);
});
let watch = this.geolocation.watchPosition();
watch.subscribe((data) => {
this.global.latitude = data.coords.latitude
this.global.longitude = data.coords.longitude
});发布于 2019-09-26 22:49:34
此外,电话的行为也会因准确度的不同而有所不同(例如:在iOS中,有必要在enableHighAccuracy).中"true"
我也有很多关于定位功能的问题,并最终解决了它作为下一个:
创建api.provider并注入到组件中:
-geo选项声明:
private geoOptions: GeolocationOptions = {
enableHighAccuracy: true,
timeout: 6000,
maximumAge: 12000
};从构造函数调用的-onInit:
public onInit(): Promise<any> {
return new Promise<any>((resolve: (value?: boolean | any) => void, reject: (reason?: any) => void) => {
this.getCurrentPosition().subscribe((response: any) => {
resolve(response);
}, (err: any) => {
reject(err);
});
});
}在执行watchPosition之前已经处理getCurrentPosition错误并更改要回调的精度选项的-constructor
constructor(public api: Api, private gps: Geolocation, private toast: ToastController) {
const locationSubscription = this.isLocationActive.subscribe((response: any) => {
const initExecute = () => {
this.onInit()
.then((val: any) => {
this.watchPosition().subscribe();
locationSubscription.unsubscribe();
}).catch((reason: any) => {
// if fails, turn enableHighAccuracy options (some Android devices do not have HighAccuracy available)
this.geoOptions.enableHighAccuracy = !this.geoOptions.enableHighAccuracy;
initExecute();
});
};
if (response && (response as boolean).toString().toLowerCase() === "true") {
initExecute();
}
});
}-the currentPosition封装方法:
public getCurrentPosition(): Observable<Geoposition> {
// Create an Observable that will start listening to geolocation updates
// when a consumer subscribes
return Observable.create((observer: Observer<Geoposition>) => {
this.gps.getCurrentPosition(this.geoOptions).then((response: any) => { // your todo validator
// this.validateGeolocationResponse(err, observer, true);
}).catch((err: any) => {
// your todo validator
// this.validateGeolocationResponse(err, observer, true);
});
// When the consumer unsubscribes, clean up data ready for next subscription.
// return { unsubscribe() { navigator.geolocation.clearWatch(watchId); }};
});
}最后,watchPosition -and方法:
public watchPosition(): Observable<Geoposition> {
return Observable.create((observer: Observer<Geoposition>) => {
this.gps.watchPosition(this.geoOptions).subscribe((response: any) => {
// your validation method if coords are returned
this.validateGeolocationResponse(response, observer);
}, (err: any) => {
// your validation method if coords are returned
this.validateGeolocationResponse(err, observer, true);
});
});
}-Tip创建一个ReplaySubject来存储最后一个右余弦值。
例如:
public currentLocation = new ReplaySubject<Geoposition>(2);也祝你幸福!
https://stackoverflow.com/questions/58117399
复制相似问题