地理围栏任务未在React Native应用程序中运行

问题描述

我已经创建了功能 setGeofences ,以便在应用程序的开头运行以运行地理围栏任务。

我已经将功能 isGeoRunning 添加到用于检查任务是否正在运行的按钮中。 它始终返回 false ,表示任务未运行。

未给出错误或警告。我已遵循here给出的官方指示。

我在做什么错了?

import React,{ useState,useEffect } from 'react';
import { StyleSheet,Text,View,Dimensions,Button } from 'react-native';
import MapView,{ Marker } from 'react-native-maps';
import * as Location from 'expo-location';
import { Audio } from 'expo-av';
import * as TaskManager from 'expo-task-manager';

const App = () => {

  const [region,setRegion] = useState({
    latitude: 59.334591,longitude: 18.063240,latitudeDelta: 0.4,longitudeDelta: 0.4
  });
  
  let [regions,setRegions] = useState([
    {
      title: "Test",latitude: 59.334591,radius: 100,}]);

  function setGeofences(){
    TaskManager.defineTask("Task1",({ data: { eventType,regions },error }) => {
      if (error) {
        console.log(error.message)
        alert(error.message)
        return;
      }
      if (eventType === LocationGeofencingEventType.Enter) {
        console.log("You've entered region:",region);
        alert("You've entered region:",region)
      } else if (eventType === LocationGeofencingEventType.Exit) {
        console.log("You've left region:",region);
        alert("You've left region:",region)
      }
    });
  }

  async function isGeoRunning(){
    await Location.hasStartedGeofencingAsync("Task1")
      .then((data) => alert(data))
    await TaskManager.isTaskRegisteredAsync("Task1")
      .then((data) => alert(data))
  }

  useEffect(() => {
  setGeofences()
  },[]) // Runs only once.

  return (
    <MapView
      style={{ flex: 1 }}
      region={region}
      onRegionChangeComplete={region => setRegion(region)}
    >

    <Button
      style="styleButton"
      title="checkRegion()"
      onPress={ () => { isGeoRunning() } }
      />
    ))}

    </MapView>
  );
};

export default App;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)