反应原生博览定位:如何让后台定位服务更频繁地更新? [英] React Native expo-location: How to make the background location service update more often?

查看:29
本文介绍了反应原生博览定位:如何让后台定位服务更频繁地更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EXPO/Reaction Native构建一个体育应用程序,并试图找出一种在应用程序处于后台时跟踪用户位置的好方法。我已经使用EXPO-Location(https://docs.expo.io/versions/latest/sdk/location/#locationstartlocationupdatesasynctaskname-options)构建了一个解决方案,它成功地接收了位置更新,我甚至在EventEmitter的帮助下成功地将位置更新发送到了UI。

这里的问题是,在将位置更新发送到UI之前,世博会位置任务会将位置更新延迟很长一段时间(例如12分钟)。尽管将所有相关选项设置为零或非常小,但仍是如此。

我很愿意使用EXPO Location,因为我让它基本正常工作,但令人难以置信的是,库似乎缺少一个选项/工具来强制后台任务足够频繁地发送更新(例如每5秒发送一次)。

如果有人有办法让世博会后台位置足够频繁地发送最新消息,我将不胜感激。现在,尽管设置了我在文档中找到的所有相关选项和参数,但它还是会在我想要的时候发送更新&qot;,大约每5或12分钟发送一次。

我已经得出结论,世博背景位置实际上是坏的,我应该切换到另一个位置库(https://github.com/mauron85/react-native-background-geolocation)。但是,我正在使用世博会管理的工作流,并且安装此mauron85位置库(否则非常有希望)不起作用,因为它需要手动设置依赖关系-->;我需要从世博会管理的工作流中弹出-->;弹出以一种我不知道如何解决的方式破坏我的项目结构。真让人沮丧!

我的代码相关部分:

顶部:

import * as Location from 'expo-location';
import * as TaskManager from 'expo-task-manager';
import EventEmitter from 'EventEmitter'

const locationEmitter = new EventEmitter();

const BACKGROUND_LOCATION_TRACKER = 'BACKGROUND_LOCATION_TRACKER'
const LOCATION_UPDATE = 'LOCATION_UPDATE'

请求位置权限后的ComponentDidmount:

await Location.startLocationUpdatesAsync(BACKGROUND_LOCATION_TRACKER, {
            accuracy: LocationAccuracy.BestForNavigation,
            timeInterval: 0,  // all set to 0 to make it update as often as possible!!! doesn't help
            distanceInterval: 0,
            deferredUpdatesInterval: 0, 
            deferredUpdatesDistance: 0,
            showsBackgroundLocationIndicator: true,
            foregroundService: {
                notificationTitle: 'title',
                notificationBody: 'recording',
                notificationColor: '#008000',
            },
            // pausesUpdatesAutomatically: true,

        });

    locationEmitter.on(LOCATION_UPDATE, (locationData) => {
        console.log('locationEmitter locationUpdate fired! locationData: ', locationData);
        let coordinatesAmount = locationData.newRouteCoordinates.length - 1;
        this.setState({
            latitude: locationData.newRouteCoordinates[coordinatesAmount - 1].latitude,
            longitude: locationData.newRouteCoordinates[coordinatesAmount - 1].longitude,
            routeCoordinates: this.state.routeCoordinates.concat(locationData.newRouteCoordinates)
        })
    })

定义位置任务:

TaskManager.defineTask(BACKGROUND_LOCATION_TRACKER, async ({ data, error }) => {
    if (error) {
        console.error(error);
        return;
    }

    if (data) {
        const { locations } = data;
        console.log('backgroundLocationTracker received new locations: ', locations)

        // const [location] = locations;
        const locationsLength = locations.length;

        const newRouteCoordinates = [];
        // const totalNewDistance = 0.0;

        for (i = 0; i < locationsLength; i++) {
            const { latitude, longitude } = locations[i].coords;
            const tempCoords = {
                latitude,
                longitude,
            };
            newRouteCoordinates.push(tempCoords);
            // totalNewDistance += GLOBAL.screen1.calcDistance(newRouteCoordinates[i], newRouteCoordinates[i - 1])  
        };

        console.log('backgroundLocationTracker: latitude ', locations[locationsLength - 1].coords.latitude,
            ', longitude: ', locations[locationsLength - 1].coords.longitude, ', routeCoordinates: ', newRouteCoordinates,
            ', prevLatLng: ', newRouteCoordinates[locationsLength - 1]);

        let locationData = { newRouteCoordinates }

        locationEmitter.emit(LOCATION_UPDATE, locationData)

    }

});

正如我所说,它全部正常工作(!)从这个意义上说,我确实可以从后台获取位置更新到UI。这里唯一的问题是我想不出如何让后台任务更频繁地发送位置更新!它只是持续收集大量的50多个位置更新,甚至持续10多分钟,然后才会费心将它们发送到UI!

感谢您的帮助,谢谢。

推荐答案

我以为我也必须退出世博会,但我更改了选项,现在它就像我预期的那样更新了。 问题在于精确度,我换成了导航的最佳模式,就像你的例子一样,而且很管用。 以下是我的选项,我认为您应该尝试定义一些毫秒,不要将间隔留在0,也不要设置距离间隔或延迟距离:

{accuracy: Location.Accuracy.BestForNavigation,
            timeInterval: 1000,
            showsBackgroundLocationIndicator: true,
            foregroundService:{
                notificationTitle: "Covid Tracker",
                notificationBody: "Rastreando sua localização",
                notificationColor: "#AA1111"
            },
            deferredUpdatesInterval: 100
        }

这篇关于反应原生博览定位:如何让后台定位服务更频繁地更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆