panhui 5 years ago
parent
commit
eeabb66c18
1 changed files with 18 additions and 4 deletions
  1. 18 4
      Utils/MapUtils.js

+ 18 - 4
Utils/MapUtils.js

@@ -4,6 +4,19 @@ import * as TaskManager from 'expo-task-manager';
 import request from './RequestUtils';
 import { alert } from './TotastUtils';
 
+const LOCATION_TASK_NAME = 'background-location-task';
+
+TaskManager.defineTask(LOCATION_TASK_NAME, ({ data, error }) => {
+  if (error) {
+    // Error occurred - check `error.message` for more details.
+    return;
+  }
+  if (data) {
+    const { locations } = data;
+    console.log(data);
+    // do something with the locations captured in the background
+  }
+});
 const key = 'c4faf80125b298f93bbc1477db10e69c';
 const tengxunKey = 'GLFBZ-ZR2W6-76XSA-MF7CQ-GDJ6Z-6FB5K';
 
@@ -14,10 +27,10 @@ async function getLocation() {
   return Location.requestPermissionsAsync()
     .then((res) => {
       if (res.status === 'granted') {
-        return Location.getCurrentPositionAsync({
-          enableHighAccuracy: true,
-          timeout: 30000,
-          accuracy: Location.Accuracy.Highest,
+        return Location.getCurrentPositionAsync(LOCATION_TASK_NAME, {
+          enableHighAccuracy: false,
+          timeout: 5000,
+          maximumAge: 10000,
         });
       } else {
         return Promise.reject();
@@ -59,6 +72,7 @@ async function getLocation() {
       }
     })
     .catch((e) => {
+      console.log(e);
       return Promise.resolve({
         addressName: '定位失败',
         location: {