How to detect big change in location?

In my app I do some expensive calculations that also include a http request based on the location. I'd like to improve the app by "detecting" if the user moved. Here are some thoughts:

1. every time I do the expensive part I save the location used for the calculation and the time

2. if more than 1 hour passed => recalculate

3. I'd like to be able to "cheaply" detect if the user "jumped" above a certain distance (let's say ~10k) and if yes => check actual distance from saved position and if > 10km => recalculate

4. I could check the actual position's distance from the saved position (let's say every 5 minutes), but it involves some floating point match calculations, so maybe there's a "cheaper" way to guess that the user "jumped"?

What I mean by jumped? For example:

- paused the activity recording, moved (maybe by car), then resumed

Not sure what I am looking for, probably some idea / trick others use. Maybe looking at speed/pace? Maybe there's some api I haven't noticed that could be used?

Top Replies

All Replies

  • If you are using GPS, you can compare two locations and determine the "as the crow flies" distance between them.  And if you lose GPS between the two locations (going through a tunnel, etc) you can still do that.  And as far as speed, I do display an alert if the speed is above a certain point as a reminder to stop/save an activity.

    When you recalculate, save the current location and time, and then if the change to the current location is at or above your thresh hold, or the refresh is over x minutes old, recalculate...  Calculating distance/direction between two points isn't that expensive, but if you are worried, only do it every minute instead of every second or something

  • How do you calculate the as the crow flies distance? I'm using the Haversine formula.

  • How do you calculate the as the crow flies distance? I'm using the Haversine formula.

    You're already doing it. The haversine formula calculates great circle distance which is synonymous with as the crow flies distance.

    https://eclipse2017.nasa.gov/crow-flies-spherical-planet

    https://en.wikipedia.org/wiki/Haversine_formula

  • Yeah, but I thought I would rather not do it every second.

  • That's easy.  Instead of doing it every second, do it every minute/5 minutes/etc.  Depending on the activity, the change in distance won't be that great even in 5 minutes..  When I do breadcrumb maps, I start with saving the current location every minute, and when the array fills, I switch to every 2,3,etc minutes.

  • Yeah, but I thought I would rather not do it every second.

    Sorry I probably misinterpreted the intent of your question. I thought you were asking how to calculate as the crow flies distance in general, but now I think you were actually asking if there was a more efficient way of doing so than the Haversine formula.

    > I thought I would rather not do it every second.

    Yeah, but you also said:

    > I could check the actual position's distance from the saved position (let's say every 5 minutes)

    I kind of took you at your word there.

    But then again, you also said:

    > - paused the activity recording, moved (maybe by car), then resumed

    This kind of implies that you might have to check more often than every 5 minutes.

    [1/x]

  • Since you mention this specific use case [pausing the activity and moving], why not initially look for "jumps" in elapsed time when the activity is paused.

    What I mean by that is if the user stops the activity and restarts it, have some threshold for the length of the pause which causes you to check for a big change in position.

    [2/x]

  • So to avoid calculating the distance deltas too often, you could apply the following heuristics:

    1. when activity is stopped, take the elapsed time (En). when activity is restarted, take the elapsed time again (En+1). If En+1 - En is greater than some threshold (idk, 5 minutes, 10 minutes, maybe 30 minutes), calculate the distance delta

    2. whether or not activity timer is running, if speed goes over a certain threshold for a certain number of [not necessarily consecutive] seconds in a certain time period, calculate the distance delta.

    3. finally, take the distance delta once every x minutes, where x is whatever you're comfortable with

    3 accomplishes your stated goal of not checking the distance delta too often.

    1 and 2 accomplish the implicit goal of not waiting too long to check the distance delta in certain cases where you think it's likely that there was a big location jump.

    I mean, maybe if you implement 2, then 1 is unnecessary, unless you are worried that the user chose some mode of transportation where GPS / speed might be unavailable [like they took a subway or something]

    [3/3]

  • https://www.movable-type.co.uk/scripts/latlong.html

    Equirectangular approximation

    If performance is an issue and accuracy less important, for small distances Pythagoras’ theorem can be used on an equi­rectangular projec­tion:*