javascript 你能用 React Native 跟踪背景地理定位吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54811976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:27:22  来源:igfitidea点击:

Can you track background geolocation with React Native?

javascriptandroidreactjsreact-nativegeolocation

提问by Grant Isdale

Problem

问题

I'd like to be able to track a users location even when the app is no longer in the foreground (e.g. The user has switch to another app or switched to the home screen and locked their phone).

即使应用程序不再处于前台(例如,用户已切换到另一个应用程序或切换到主屏幕并锁定他们的手机),我也希望能够跟踪用户的位置。

The use case would be a user tracking a run. They could open the app and press 'start' at the beginning of their run, then switch or minimise the app (press the home button) and lock the screen. At the end of the run they could bring the app into the foreground and press 'stop' and the app would tell them distance travelled on the run.

用例是跟踪跑步的用户。他们可以打开应用程序并在运行开始时按“开始”,然后切换或最小化应用程序(按主页按钮)并锁定屏幕。在跑步结束时,他们可以将应用程序带到前台并按“停止”,应用程序会告诉他们在跑步过程中行驶的距离。

Question

问题

Is tracking background geolocation possible on both iOS and Android using pure react native?

是否可以使用纯 React Native 在 iOS 和 Android 上跟踪后台地理定位?

The react native docs on geolocation (https://facebook.github.io/react-native/docs/geolocation) are not very clear or detailed. The documented linked above eludes to background geolocation on iOS (without being fully clear) but does not mention Android.

关于地理定位的 react native 文档(https://facebook.github.io/react-native/docs/geolocation)不是很清楚或详细。上面的文档链接没有提到 iOS 上的背景地理定位(没有完全清楚),但没有提到 Android。

Would it be best that I use Expo?

我最好使用 Expo 吗?

回答by Erik Toor

UPDATE 2019 EXPO 33.0.0:

2019 年世博会 33.0.0 更新:

Expo first deprecated it for their SDK 32.0.0 to meet app store guidelines but then reopened it in SDK 33.0.0.

Expo 首先在他们的 SDK 32.0.0 中弃用它以满足应用商店指南,然后在 SDK 33.0.0 中重新打开它

Since, they have made it super easy to be able to implement background location. Use this code snippet that I used to make background geolocation work.

从那时起,它们使实现后台定位变得非常容易。使用我用来使后台地理定位工作的这段代码片段。

import React from 'react';
import { Text, TouchableOpacity } from 'react-native';
import * as TaskManager from 'expo-task-manager';
import * as Location from 'expo-location';

const LOCATION_TASK_NAME = 'background-location-task';

export default class Component extends React.Component {
  onPress = async () => {
    await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
      accuracy: Location.Accuracy.Balanced,
      timeInterval: 5000,
    });
  };

  render() {
    return (
      <TouchableOpacity onPress={this.onPress} style={{marginTop: 100}}>
        <Text>Enable background location</Text>
      </TouchableOpacity>
    );
  }
}

TaskManager.defineTask(LOCATION_TASK_NAME, ({ data, error }) => {
  if (error) {
    alert(error)
    // Error occurred - check `error.message` for more details.
    return;
  }
  if (data) {
    const { locations } = data; 
    alert(JSON.stringify(locations); //will show you the location object
    //lat is locations[0].coords.latitude & long is locations[0].coords.longitude
    // do something with the locations captured in the background, possibly post to your server with axios or fetch API 
  }
});

The code works like a charm. One thing to note is that you cannot use geolocation in the Expo App. However, you can use it in your standalone build. Consequently, if you want to use background geolocation you have to use this code and then do expo build:iosand upload to the appstore in order to be able to get a users background location.

代码就像一个魅力。需要注意的一件事是您不能在 Expo App 中使用地理定位。但是,您可以在独立构建中使用它。因此,如果您想使用后台地理定位,您必须使用此代码,然后expo build:ios上传到应用商店,以便能够获取用户的后台位置。

Additionally, note that you mustinclude

此外,请注意您必须包括

"UIBackgroundModes":[
          "location",
          "fetch"
        ]

In the info.plistsection of your app.jsonfile.

info.plist您的app.json文件部分。

回答by neydroid

Yes is possible, but not using Expo, there are two modules that I've seen:

是的,但不使用 Expo,我见过两个模块:

This is a comercial one, you have to buy a license https://github.com/transistorsoft/react-native-background-geolocation

这是一个商业的,你必须购买许可证https://github.com/transistorsoft/react-native-background-geolocation

And this https://github.com/mauron85/react-native-background-geolocation

而这个https://github.com/mauron85/react-native-background-geolocation

回答by Cristobal Miranda Ureta

The Expo Team release a new feature in SDK 32 that allow you tracking in background the location.

Expo 团队在 SDK 32 中发布了一项新功能,允许您在后台跟踪位置。

https://expo.canny.io/feature-requests/p/background-location-tracking

https://expo.canny.io/feature-requests/p/background-location-tracking

回答by McMurphy

Webkit is currently evaluating a Javascript-only solution. You can add your voice here

Webkit 目前正在评估仅使用 Javascript 的解决方案。你可以在这里添加你的声音

For a fully documented proof-of-concept example please see Brotkrumen.

有关完整记录的概念验证示例,请参阅Brotkrumen