capacitor-community / background-geolocation

A Capacitor plugin that sends you geolocation updates, even while the app is in the background.
MIT License
188 stars 57 forks source link

Editable timeout and new parameter #34

Open medvidekPanda opened 3 years ago

medvidekPanda commented 3 years ago

Problem

There is no option to set custom timeout and setFastestInterval parameter.

Solution

diachedelic commented 3 years ago

This has been requested before, but I am reticent to include a configuration option for Android when an equivalent option does not exist for iOS. Is the distance filter option not sufficient for your needs?

medvidekPanda commented 3 years ago

OK I didn't know that. But I need timeout, because I need request position periodically. For example every 30 seconds. I understand different options for different OS should be complicated. But in Cordova / Capacitor plugins it is normal. For my pour. iOS is not important for my purposes. I work on Android app only.

diachedelic commented 3 years ago

I am sympathetic to your cause. Could you please explain to me your use case, and why a distance filter is not sufficient?

medvidekPanda commented 3 years ago

Distance filter is not sufficient, because I need get location in long interval in some situation, for example every 30 seconds, but device is on one place. Yes I can make some workaround in app controler, but I think, get position from OS api is better, when API has this feature.

diachedelic commented 3 years ago

Yes, but why do you need the location every 30 seconds if the devices has not moved? Is it just to trigger something in the UI?

medvidekPanda commented 3 years ago

Exactly as you said. App runs on the foreground / background and every X seconds send location to server.

diachedelic commented 3 years ago

Why does the server require location updates every X seconds, even if the location has not changed?

medvidekPanda commented 3 years ago

It doesn't matter at all. I have such a request from a customer. He needs to be 100% sure that the device is in place or in motion at such a time and at such an interval. When the object moves 10 km per hour, it travels a different distance than it travels 100 km per hour.

diachedelic commented 3 years ago

I see. The requests to the server are keeping the connection alive, so to speak.

diachedelic commented 3 years ago

Would you be happy if the setting was defined in strings.xml, like these?

medvidekPanda commented 3 years ago

I'm afraid not :-(. The requirement is to change the timeout from FE. So it can be up to 15 seconds or 60 seconds depending on the user.

diachedelic commented 3 years ago

There are three methods on LocationRequest which you have mentioned:

Are you looking to control every one of these?

There is also the option of pulling the Android source code into your own project. It is just two classes and a few imports. Then you can have as much control as you need.

gine commented 2 years ago

I would like a feature like this for this reason:

On my application i set distance filter to 5 meter and normally i receive a location with a speed value.

I also use this speed value to do things(example: more speed value = more volume, but it can be 'distribute more salt on the road' so if speed is 0, i must distribute 0 salt and the vehicle run at 10km/h i must distribute 1kg salt, at 20km/h 2kg salt ...)

But what happens if in less then 5 meter the vehicle stops to run? I doesn't receive any update and i continue to think that my car is in motion and i continue to distribute salt. A mount of salt :)

At this point i must control myself that the last update is old and reset my speed.

diachedelic commented 2 years ago

@gine How do you handle the situation that:

In both scenarios, location messages will not be received. Do you stop distributing salt when no messages appear for a given period?

gine commented 2 years ago

@diachedelic the salt distributor has a "stupid" PLC the try to connect to me(i am a server, yes it's strange.. can't explane now) every 100ms for N times and i must respond with phone status. If no response arrive, the plc change from manual mode to auto mode. At the moment i'm testing it with gps meter = 5 and it's impossible to get speed = 0.. i'm testisting realy now.

sure, i can put a timer in the app that each N ms look if i have a new gps data but it's better what we are asking and that you implement a new type of filter.

I will try with gps meter filter to 1m and i'll wtite what happen.. A lot of data drain a lot of cpu :(

gine commented 2 years ago

With 1mt filter work really better because the car breaking need at least 1 mt to stop the car but sometimes still happen

diachedelic commented 2 years ago

@gine, I was intrigued by your idea in #41 to set the distance filter dynamically. I do not know if it would reduce energy consumption, but it might. What do you think of this as a solution?

Callan003 commented 2 years ago

Hello,

This plugin works perfectly fine on both iOS and Android. However it drains battery very fast. My app requires user to have it's location tracked all the time (well almost all the time ... it's a delivery app).

I tried using stale position true and distance filter of 50m, but it had no impact on the battery usage.

My suggestion is to have the option to have the location checked every lets say 30 sec, then the service will go 'dormant' and not check for device location for the next 30 seconds. Might be stupid but I can't think of any other way I can save the battery usage.

Thank-you!

EDIT

I think on Android this could be achieved if if you change this line to accept an input, not a hard-codded value of '1000'.

diachedelic commented 2 years ago

I tried using stale position true and distance filter of 50m, but it had no impact on the battery usage.

That's a shame the distance filter does not help with battery drain. Was this the case on both iOS and Android? I think I have to accept that this plugin needs a setting which controls Android's setInterval. But I don't know how to help you with iOS.

On Android, it is also possible to use the PRIORITY_BALANCED_POWER_ACCURACY priority for coarser location updates, about 100m. I would be interested to know how using this priority instead affects battery usage on your device. Could you please see if that gives an improvement? It should be possible to just modify the file and rebuild.

gine commented 2 years ago

@gine, I was intrigued by your idea in #41 to set the distance filter dynamically. I do not know if it would reduce energy consumption, but it might. What do you think of this as a solution?

it can work.

josuelmm commented 2 years ago

Any news on this matter?

diachedelic commented 2 years ago

@josuelmm What is your use case?

josuelmm commented 2 years ago

Being able to change the values of: setMaxWaitTime(1000); setInterval(1000);

From Ionic

Zack-Heisnberg commented 2 years ago

Hello Sir , i am not sure if you still need fix , i did the exact same thing as i needed the same thing as you xD

so , what you need to do is simple

in BackgroundGeolocationService.java Change locationRequest.setInterval(1000); to locationRequest.setInterval(30000);

and set config distanceFilter: 0

you will receive the location every 30 seconds even if you don't move :)

, i made pull request with editable interval from capacitor

https://github.com/Zack-Heisnberg/background-geolocation

matudelatower commented 2 years ago

Hello Sir , i am not sure if you still need fix , i did the exact same thing as i needed the same thing as you xD

so , what you need to do is simple

in BackgroundGeolocationService.java Change locationRequest.setInterval(1000); to locationRequest.setInterval(30000);

and set config distanceFilter: 0

you will receive the location every 30 seconds even if you don't move :)

, i made pull request with editable interval from capacitor

https://github.com/Zack-Heisnberg/background-geolocation

Can you make a PR?

indrapalijama commented 1 year ago

Hello Sir , i am not sure if you still need fix , i did the exact same thing as i needed the same thing as you xD

so , what you need to do is simple

in BackgroundGeolocationService.java Change locationRequest.setInterval(1000); to locationRequest.setInterval(30000);

and set config distanceFilter: 0

you will receive the location every 30 seconds even if you don't move :)

, i made pull request with editable interval from capacitor

https://github.com/Zack-Heisnberg/background-geolocation

hi! is this fix applicable on android only?

matudelatower commented 1 year ago

Hello Sir , i am not sure if you still need fix , i did the exact same thing as i needed the same thing as you xD so , what you need to do is simple in BackgroundGeolocationService.java Change locationRequest.setInterval(1000); to locationRequest.setInterval(30000); and set config distanceFilter: 0 you will receive the location every 30 seconds even if you don't move :) , i made pull request with editable interval from capacitor https://github.com/Zack-Heisnberg/background-geolocation

hi! is this fix applicable on android only?

yes, its only for android

indrapalijama commented 1 year ago

Hello Sir , i am not sure if you still need fix , i did the exact same thing as i needed the same thing as you xD so , what you need to do is simple in BackgroundGeolocationService.java Change locationRequest.setInterval(1000); to locationRequest.setInterval(30000); and set config distanceFilter: 0 you will receive the location every 30 seconds even if you don't move :) , i made pull request with editable interval from capacitor https://github.com/Zack-Heisnberg/background-geolocation

hi! is this fix applicable on android only?

yes, its only for android

i see, noob question : how can i install zack's forked repo? i tried using npm install but got failed

diachedelic commented 1 year ago

Did you try npm install https://github.com/Zack-Heisnberg/background-geolocation? It worked for me.

indrapalijama commented 1 year ago

npm install https://github.com/Zack-Heisnberg/background-geolocation

still got the error dear sir, here's the error

diachedelic commented 1 year ago

Did you first remove the @capacity-community/background-geolocation package from your package.json? It can not coexist with Zack's fork.

indrapalijama commented 1 year ago

Did you first remove the @capacity-community/background-geolocation package from your package.json? It can not coexist with Zack's fork.

yes, i'm well aware of it, here's the proof

DocBO commented 9 months ago

If you stumble across this issue also, consider to change the relevant lines in your local folder node_modules/..../BackgroundGelolocationService.java before building again according to the new android API:

LocationRequest.Builder locationRequestBuilder = new LocationRequest.Builder(30000); locationRequestBuilder.setPriority(Priority.PRIORITY_BALANCED_POWER_ACCURACY); locationRequestBuilder.setIntervalMillis(30000); locationRequestBuilder.setMaxUpdateDelayMillis(30000); locationRequestBuilder.setMinUpdateDistanceMeters(distanceFilter);

This seems to result in a reasonable background action (I have not tested the battery drain over time).

1MarcoP commented 9 months ago

Hi, I just stumbled across this plugin which works really well.

For my use case, I want to periodically check the location of some users while they approach a certain destination and sync it (if the location changed) with the backend for some dispatcher to be informed. I want this to be as energy efficient as possible, as I often only have to request the location every few minutes.

So far I read that the iOS part is making difficulties regarding the timeout parameter.

I have little knowledge of iOS and swift, but just tried out if a native timer can be used.

if let interval = call.getInt("interval") {
  manager.startUpdatingLocation()
  Timer.scheduledTimer(withTimeInterval: TimeInterval(interval), repeats: true) { _ in
    manager.requestLocation()
  }
}

However, inside locationManager/didUpdateLocations I had to call locationManager.stopUpdatingLocation(). I thought this might be a big problem as the app won't send updates while in background, but in my tests it continued requesting the location every n seconds and the native timer did work surprisingly well.

As I stated, I don't have much knowledge about swift, but would like to understand what the biggest concern is? Would the native timer actually be more energy efficient?

diachedelic commented 9 months ago

I'm surprised that works in the background. What interval values did you try? Did the status bar turn blue whilst the app was in the background?

If this approach works in the background, turns out to be energy efficient, and gives accurate results, I will add an "interval" option. However,

1MarcoP commented 8 months ago

Hi, sorry for the late reply. I didn't have access to a Mac for building iOS, during the last days.

I tried 10 seconds, 30 seconds, 1 minute, 5 minutes and 15 minutes. I let the 15 minute one run for about 2h 30min in the background.

However, the status bar turned blue in the beginning, but afterwards it only turned blue again when the GPS tracking was done (every n seconds). Therefore, I am not sure if the app could be killed in the background when the phone is under a heavy load?

GeekAliAkbar commented 8 months ago

After adding this

If you stumble across this issue also, consider to change the relevant lines in your local folder node_modules/..../BackgroundGelolocationService.java before building again according to the new android API:

LocationRequest.Builder locationRequestBuilder = new LocationRequest.Builder(30000); locationRequestBuilder.setPriority(Priority.PRIORITY_BALANCED_POWER_ACCURACY); locationRequestBuilder.setIntervalMillis(30000); locationRequestBuilder.setMaxUpdateDelayMillis(30000); locationRequestBuilder.setMinUpdateDistanceMeters(distanceFilter);

This seems to result in a reasonable background action (I have not tested the battery drain over time).

I tried above solution and got this error watcher.locationRequest = locationRequest; ^ symbol: variable locationRequest location: class BackgroundGeolocationService.LocalBinder

Any idea how to solve this

DocBO commented 8 months ago

Hi, I forgot this line at the end (again, according to the new android api):

LocationRequest locationRequest = locationRequestBuilder.build();