首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Geolocation -如果我在启用GPS的情况下启动,Ionic 3天气应用程序可以正常工作

Geolocation -如果我在启用GPS的情况下启动,Ionic 3天气应用程序可以正常工作
EN

Stack Overflow用户
提问于 2019-09-26 21:01:51
回答 1查看 80关注 0票数 0

如果我在启用GPS的情况下启动,我的Ionic 3天气应用程序可以正常工作。

如果我在关闭全球定位系统的情况下启动应用程序,并订阅了监听更改(参见下面的watch.subscribe),尽管随后手动启用它,但它无法恢复纬度和经度。我想帮助控制GPS,也就是“监听”on和off事件,以便能够对其进行操作。谢谢!

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

回答 1

Stack Overflow用户

发布于 2019-09-26 22:49:34

此外,电话的行为也会因准确度的不同而有所不同(例如:在iOS中,有必要在enableHighAccuracy).中"true"

我也有很多关于定位功能的问题,并最终解决了它作为下一个:

创建api.provider并注入到组件中:

-geo选项声明:

代码语言:javascript
复制
private geoOptions: GeolocationOptions = {
     enableHighAccuracy: true,
     timeout: 6000,
     maximumAge: 12000
};

从构造函数调用的-onInit:

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

代码语言:javascript
复制
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封装方法:

代码语言:javascript
复制
 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方法:

代码语言:javascript
复制
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来存储最后一个右余弦值。

例如:

代码语言:javascript
复制
public currentLocation = new ReplaySubject<Geoposition>(2);

也祝你幸福!

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

https://stackoverflow.com/questions/58117399

复制
相关文章

相似问题

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