【问题标题】:Why does getCurrentPositionAsync never returns anything?为什么 getCurrentPositionAsync 从不返回任何东西?
【发布时间】:2022-01-15 07:07:45
【问题描述】:

感谢您访问此帖子。 我在 React Native 获取用户位置时遇到问题。

我已经搜索了一段时间并阅读了其他人有关此功能的帖子。 看来这个函数有几个问题。

而且 react 原生的 expo 文档似乎已经过时了。 https://docs.expo.dev/versions/latest/sdk/location/ 我在用法部分使用此处的代码。

import React, { useState, useEffect } from 'react';
import { Platform, Text, View, StyleSheet } from 'react-native';
import * as Location from 'expo-location';

export default function App() {
  const [location, setLocation] = useState(null);
  const [errorMsg, setErrorMsg] = useState("");

  useEffect(async () => {
    (async () => {
      let { status } = await Location.requestForegroundPermissionsAsync();
      console.log(status);
      if (status !== 'granted') {
        console.log("denied")
        setErrorMsg('Permission to access location was denied');
        return;
      }

      console.log("status", status); // <=== always says "granted"

      let location = await Location.getCurrentPositionAsync({
        accuracy: Location.Accuracy.Highest,
        maximumAge: 10000,
        timeout: 5000
      });
      console.log({ location }) // <== never reach here.
      setLocation(location);
      setErrorMsg('No ERROR');
    })();
  }, []);

  let text = 'Waiting..';
  if (errorMsg) {
    text = errorMsg;
  } else if (location) {
    text = JSON.stringify(location);
  }

  return (
    <View>
      <Text>{text}</Text>
    </View>
  );
}

我看到一篇帖子说我必须将accuracy, maximumAge 的参数传递给getCurrentPositionAsync,而不是博览会文档中提供的{} empty object。 但仍然无法正常工作。由于getCurrentPositionAsync挂起,屏幕一直显示waiting...

当然,我相信安卓模拟器设置正确,因为我确实看到了状态日志,上面写着"granted"

非常感谢您的帮助和阅读我的帖子。

    "expo": "~43.0.2",
    "expo-location": "~13.0.4",
    "expo-status-bar": "~1.1.0",
    "react": "17.0.1",
    "react-dom": "17.0.1",
    "react-native": "0.64.3",
    "react-native-web": "0.17.1"

【问题讨论】:

    标签: android react-native geolocation


    【解决方案1】:

    您不能将 async 与 useEffect 一起使用,因此不能等待。

    您是否尝试过相反的方法?

    const [position, setPosition] = useState(false)
    // useEffect  
      Location.getCurrentPositionAsync({
        accuracy: Location.Accuracy.Highest,
        maximumAge: 10000,
        timeout: 5000
      })
      .then(res => setPosition(res))
      .catch(e => console.log(e)
    

    我也有更多的机会没有非胖箭头IIFE,你应该试试

    (function () {
          let { status } = await Location.requestForegroundPermissionsAsync();
          console.log(status);
          if (status !== 'granted') {
            console.log("denied")
            setErrorMsg('Permission to access location was denied');
            return;
          }
    
          console.log("status", status); // <=== always says "granted"
    
          let location = await Location.getCurrentPositionAsync({
            accuracy: Location.Accuracy.Highest,
            maximumAge: 10000,
            timeout: 5000
          });
          console.log({ location }) // <== never reach here.
          setLocation(location);
          setErrorMsg('No ERROR');
    })();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 2022-12-04
      • 1970-01-01
      • 1970-01-01
      • 2023-02-07
      • 2011-08-06
      相关资源
      最近更新 更多