微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

React Native expo-location:如何让后台位置服务更新更频繁?

如何解决React Native expo-location:如何让后台位置服务更新更频繁?

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

现在的问题是,Expo Location 任务在向 UI 发送位置更新之前将位置更新延迟了很长时间(例如 12 分钟)。尽管将所有相关选项设置为零或非常小,但情况仍然如此。

我很想使用 Expo Location,因为我大部分时间都在使用它,但令人难以置信的是,该库似乎缺少一个选项/工具来强制后台任务足够频繁地发送更新(例如 5 秒一次)。

如果有人有真正让世博会背景位置足够频繁地发送更新的解决方案,我将不胜感激。现在它会“在我喜欢的时候”发送更新,大约每 5 或 12 分钟一次,尽管设置了我在文档中找到的所有相关选项和参数。

我已经得出结论,Expo 背景位置实际上已损坏,我应该切换到另一个位置库 (https://github.com/mauron85/react-native-background-geolocation)。但是,我正在使用 Expo 管理的工作流,并且安装这个 mauron85 位置库(否则真的很有希望)不起作用,因为它需要手动设置依赖项 --> 我需要从 Expo 管理的工作流中弹出 --> 弹出破坏了我的项目结构一种我不知道如何解决方法。真郁闷!

我的代码的相关部分:

顶部:

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 多分钟,然后才费心将它们发送到用户界面!

感谢所有帮助,谢谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。