【问题标题】:Geolocation - Ionic 3 weather app works correctly if I start with GPS enabled地理位置 - 如果我从启用 GPS 开始,Ionic 3 天气应用程序可以正常工作
【发布时间】:2019-09-26 22:49:34
【问题描述】:

如果我从启用 GPS 开始,我的 Ionic 3 天气应用程序可以正常工作。

如果我在 GPS 关闭的情况下启动应用程序,并订阅收听更改(请参阅下面的 watch.subscribe),但手动启用它不会恢复纬度和经度。我需要帮助来控制 GPS,即“听”开和关事件,以便能够对其进行操作。谢谢!

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
 });

【问题讨论】:

    标签: ionic3


    【解决方案1】:

    手机的行为也会因您的准确度而异(例如:在 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);
           });
       });
    }
    

    - 已经处理 getCurrentPosition 错误并改变精度选项以在执行 watchPosition 之前回调的构造函数:

    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();
                }
            });
        }
    

    - 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 方法:

    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);
                });
            });
        }
    

    -提示 创建一个 ReplaySubject 来存储最后一个正确的坐标值。它并不总是成功返回,因此建议搜索最新的成功并在当前失败时将它们返回给您的订阅者。

    例如:

    public currentLocation = new ReplaySubject<Geoposition>(2);
    

    也祝福你!

    【讨论】:

    • 谢谢!虽然代码对于我的 Ionic 3 级别来说似乎很困难,但我要开始工作了 祝福你
    • 是的,我能理解你。但是一点一点地读。意图评论每个部分以获得主要理解。有什么问题,可以再给我写信。 ;)(如果有用的话,给答案打分:P)。 -真诚的
    猜你喜欢
    • 2016-05-13
    • 1970-01-01
    • 2019-07-02
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 2014-06-22
    相关资源
    最近更新 更多