javascript getCurrentPositionAsync和watchPositionAsync之间的差异以及为什么没有显示任何内容

ijxebb2r  于 2022-12-25  发布在  Java
关注(0)|答案(1)|浏览(95)

我试着得到用户的位置,并让它不断更新和显示在屏幕上。我只是不明白为什么这是行不通的:(
下面是我的代码:

import { Alert, Linking, Text, View } from "react-native";
import React, { useEffect, useState } from "react";

import * as Location from "expo-location";

export default function DateFinder() {
  const [hasForegroundPermissions, setHasForegroundPermissions] =
    useState(null);
  const [userLocation, setUserLocation] = useState(null);
  const [textPosition, setTextPosition] = useState("");

  useEffect(() => {
    const AccessLocation = async () => {
      function appSettings() {
        console.warn("Open settigs pressed");
        if (Platform.OS === "ios") {
          Linking.openURL("app-settings:");
        } else RNAndroidOpenSettings.appDetailsSettings();
      }

      const appSettingsALert = () => {
        Alert.alert(
          "Allow Wassupp to Use your Location",
          "Open your app settings to allow Wassupp to access your current position. Without it, you won't be able to use the love compass",
          [
            {
              text: "Cancel",
              onPress: () => console.warn("Cancel pressed"),
            },
            { text: "Open settings", onPress: appSettings },
          ]
        );
      };

      const foregroundPermissions =
        await Location.requestForegroundPermissionsAsync();
      if (
        foregroundPermissions.canAskAgain == false ||
        foregroundPermissions.status == "denied"
      ) {
        appSettingsALert();
      }
      setHasForegroundPermissions(foregroundPermissions.status === "granted");
      if (hasForegroundPermissions == true) {
        console.warn("bob");
        const location = await Location.watchPositionAsync(
          { enableHighAccuracy: true, accuracy: Location.Accuracy.Highest },
          (location) => {
            setUserLocation(location);
            setTextPosition(JSON.stringify(location));
          }
        );

        console.warn("bob");
      }
    };

    AccessLocation().catch(console.error);
  }, []);

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

  const styles = StyleSheet.create({
    background: {
      backgroundColor: COLORS.background_Pale,
      flex: 1,
      // justifyContent: "flex-start",
      //alignItems: "center",
    },
    image: {
      flex: 1,
      // height: null,
      // width: null,
      //alignItems: "center",
    },
    scrollView: {
      backgroundColor: COLORS.background_Pale,
    },
  });
}

可悲的是,似乎什么也没发生,但我不知道为什么。文本中没有显示任何内容,watchPositionAsync也没有更新。也许我需要做些什么来更新屏幕,因为useEffect只在每次屏幕更新时运行,但我似乎不知道我应该做什么。

ubby3x7f

ubby3x7f1#

我有过不得不处理世博会选址问题的不幸经历......我的回答没有任何技术解释,它只是一个似乎每个人都愿意忽略的bug。
设置任何精度、间隔、距离或延迟似乎会使整个监视器停止工作。除非您使用accuracyactivityType并将它们设置为BestForNavigationFitness,例如:

const location = await Location.watchPositionAsync(
    {
        accuracy: Location.Accuracy.BestForNavigation,
        activityType: Location.ActivityType.Fitness
    },
    (location) => {
        setUserLocation(location);
        setTextPosition(JSON.stringify(location));
    }
);

我个人在使用startLocationUpdatesAsync执行任务时有过更好的体验:

TaskManager.defineTask("recorder", ({ data, error }) => {
    if(error)
        return console.error(JSON.stringify(error));
    
    if(data) {
        const { locations } = data;

        // trigger a call to your component here
    }
});

// ...

Location.startLocationUpdatesAsync("recorder", {
    accuracy: Location.Accuracy.BestForNavigation,
    activityType: Location.ActivityType.Fitness
});

相关问题