繁体   English   中英

电容器 iOS Geolocation watchPostion kCLErrorDomain error 1 while permission granted

[英]Capacitor iOS Geolocation watchPostion kCLErrorDomain error 1 while permission granted

我使用的是旧版本的电容器地理定位,v1.3.1,最近切换到 watchPosition 实现,但偶尔会造成 position 为 null 或未定义的情况,即使设备显示位置图标对应用程序处于活动状态也是如此。 我试图通过回退到较慢的 getCurrentPosition function 来解决这个问题,但仍然存在。 有没有人遇到过这个问题? 这是钩子的要点。

https://gist.github.com/billpull/8bc6e49872cfee29aa5cef193b59c835

useCurrentPosition.ts

const useCurrentPosition = (): GeoWatchPositionResult => {
  const [position, setPosition] = useState<Position>();
  const [watchId, setWatchId] = useState("");
  const [error, setError] = useState();

  const clearWatch = () => {
    if (watchId) {
      clearPosition({ id: watchId });
      setWatchId("");
    }
  };

  const startWatch = async () => {
    if (!watchId) {
      const id = await watchPosition(async (pos: Position | null, err) => {
        if (err) {
          setError(err);
        }
        if (pos) {
          setPosition(pos);
        } else {
          const newPosition = await getCurrentPosition();
          setPosition(newPosition);
        }
      });
      setWatchId(id);
    }
  };

  useEffect(() => {
    startWatch();

    return () => clearWatch();
  }, []);

  return { currentPosition: position, error };
};

即使 watchPosition 仍然在我收到 kCLErrorDomain 错误 1 的时间间隔内返回位置数据。网上说这意味着权限被拒绝,但电话只是处于睡眠模式并非如此。 有没有办法专门捕获此错误? 我应该清除手表并在出现此错误时重新启动它吗?

编辑:

我做过的一次尝试是在手表中使用 try catch,但我仍然遇到了这个问题。

const useCurrentPosition = (): GeoWatchPositionResult => {
  const [position, setPosition] = useState<Position>();
  const [watchId, setWatchId] = useState("");
  const [error, setError] = useState();

  const clearWatch = () => {
    if (watchId) {
      clearPosition({ id: watchId });
      setWatchId("");
    }
  };

  const startWatch = async () => {
    if (!watchId) {
      const id = await watchPosition(async (pos: Position | null, err) => {
       try {
        if (err) {
          setError(err);
        }
        if (pos) {
          setPosition(pos);
        } else {
          const newPosition = await getCurrentPosition();
          setPosition(newPosition);
        }
       } catch (ex) {
         await requestPermission();
         clearWatch();
         await startWatch();
       }
      });
      setWatchId(id);
    }
  };

  useEffect(() => {
    startWatch();

    return () => clearWatch();
  }, []);

  return { currentPosition: position, error };
};

我认为您应该使用此代码片段来检查当前的 position。

import { Geolocation, Geoposition } from '@ionic-native/geolocation/ngx';

constructor(private geolocation: Geolocation) { }

...

let watch = this.geolocation.watchPosition();
watch.subscribe((data) => {
 // data can be a set of coordinates, or an error (if an error occurred).
 // data.coords.latitude
 // data.coords.longitude
});

您还可以使用以下代码片段来获取当前的 position。

this.geolocation.getCurrentPosition().then((resp) => {
 // resp.coords.latitude
 // resp.coords.longitude
}).catch((error) => {
  console.log('Error getting location', error);
});

如果要处理权限被拒绝错误,处理权限被拒绝错误的最佳方法是在尝试访问任何位置数据之前先检查应用程序的权限状态。 这可以通过使用 Capacitor Permissions API 来完成。如果已授予权限,您可以继续使用 watchPosition 或 getCurrentPosition API。 如果权限被拒绝,您可以提示用户再次请求权限。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM