Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

android - React Native - Geolocation without timeout in Foreground service

Facing this issue and not able to find a solution for this. My objective is to track the user's location every 30 seconds to 60 seconds with a foreground service running (with a notification) so that the user knows his location is being tracked.

The foreground service is working fine, I used '@supersami/rn-foreground-service' npm packagae https://www.npmjs.com/package/@supersami/rn-foreground-service. the foreground task is running very consistently even from the app is killed or closed.

So I thought of adding the geolocation fetch inside this task to get the location of the device. I am using 'react-native-geolocation-service' npm package https://www.npmjs.com/package/react-native-geolocation-service.

After probably 30-40 seconds of killing the app. The geolocation API stops fetching the location and starts throwing an error Location request timed out. Why is this happening? And how can i stop this from happening?

Why do we have a timeout for fetching the GPS location? And with no timeout, the onError callback function doesn't get called, below is the code.

ReactNativeForegroundService.add_task(
          () => {
            Geolocation.getCurrentPosition(
              (position) => {
                console.log(position);
              },
              (error) => {
                // See error code charts below.
                console.log(error.code, error.message);
              },
              {enableHighAccuracy: true, timeout: 1000, maximumAge: 10000},
            );
          },
          {
            delay: 100,
            onLoop: true,
            taskId: 'taskid',
            onError: (e) => console.log('Error logging:', e),
          },
        );

My objective is to get the GPS coordinates with a foreground service running in any Android Device. Please help me out here, if I am doing anything wrong or missing out anything vital.

question from:https://stackoverflow.com/questions/65902006/react-native-geolocation-without-timeout-in-foreground-service

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...