首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在中不运行Geofencing

在中不运行Geofencing
EN

Stack Overflow用户
提问于 2020-09-29 14:04:47
回答 1查看 253关注 0票数 0

我已经创建了函数setGeofences,以便在应用程序开始时运行,以运行地理位置任务。

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

没有错误或警告。我遵循了给这里的官方指示。

我做错了什么?

代码语言:javascript
复制
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;
EN

回答 1

Stack Overflow用户

发布于 2020-09-29 14:20:30

我完全误解了文件。我不应该以这种方式使用这个函数。我只应该跑:

代码语言:javascript
复制
Location.startGeofencingAsync("Task1", regions)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64121294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档