0

我创建了函数setGeofences在应用程序启动时运行以运行地理围栏任务。

我已将函数isGeoRunning添加到一个按钮以检查任务是否正在运行。它总是返回false,这意味着任务没有运行。

没有给出错误或警告。我已按照此处给出的官方说明进行操作。

我究竟做错了什么?

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,
      longitude: 18.063240,
      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;
4

1 回答 1

0

我完全误解了文档。我不应该以这种方式使用该功能。我只应该运行:

Location.startGeofencingAsync("Task1", regions)
于 2020-09-29T14:20:30.547 回答