【问题标题】:Ionic Capacitor Geolocation is caching position/location. Cannot fetch for new locations离子电容器地理定位是缓存位置/位置。无法获取新位置
【发布时间】:2020-01-02 22:41:57
【问题描述】:

每次我使用Geolocation自动定位设备,它总是返回第一次使用Geolocation 时定位的旧位置。这似乎是一个缓存问题。 当我在browser or android 上运行时,同样的问题。我调用下面的函数来获取坐标。

import { Plugins, Capacitor } from '@capacitor/core';


    options = {
      enableHighAccuracy: true,
      timeout: 5000,
      maximumAge: 0
    };


    public locateUser() {


      Plugins.Geolocation.getCurrentPosition(this.options)
        .then(geoPosition => {
          const coordinates: Coordinates = {
            lat: geoPosition.coords.latitude,
            lng: geoPosition.coords.longitude
          };

          console.log(coordinates.lat , coordinates.lng); // Always prints the old coordinate even if the device is in a different location
        })
        .catch(err => {
          this.isLoading = false;
          this.showErrorAlert();
        });

    }




// I also tried this function below but the same result:

    async getCurrentPosition() {
      const coordinates = await Plugins.Geolocation.getCurrentPosition({
        enableHighAccuracy: true,
        timeout: 10000,
        maximumAge: 0
      });
      alert( coordinates.coords.latitude);
      alert( coordinates.coords.longitude);
    }

【问题讨论】:

    标签: angular ionic-framework geolocation capacitor


    【解决方案1】:

    Capacitor 插件提供 2 个 Geolocation 对象,Geolocation.getCurrentPosition()Geolocation.watchPosition({}, (position, err)。为了不断的观察当前用户的位置。您需要使用Geolocation.watchPosition({}, (coordinates, err)。因此,您的代码应如下所示:

    const wait = Geolocation.watchPosition(
        {}, (coordinates, err) => {
         this.Latitude = coordinates.coords.latitude;
         this.Longitude = coordinates.coords.longitude;
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-17
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多