[Location Plugin] Tracking distance (location Plugin)

[Location Plugin] Tracking distance (location Plugin)

  
Forge Component
(17)
Published on 2016-12-15 by OutSystems R&D
17 votes
Published on 2016-12-15 by OutSystems R&D

Hello,

I need to track the distance in KM using the Location Plugin. In this case i have a entity that saves all the "WatchPosition" (latitude and longitude).

I need help to calculate the first location and calculate the distance when the device change is position or other solution to count km.

Regards,

Nuno Bernardo

Hi Nuno,

Out there you have some algorithms to do this, one example is http://en.wikipedia.org/wiki/Haversine_formula

Because this is a mobile component I would suggest you to use the Javascript code:

function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
  var R = 6371; // Radius of the earth in km
  var dLat = deg2rad(lat2-lat1);  // deg2rad below
  var dLon = deg2rad(lon2-lon1); 
  var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2)
    ; 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c; // Distance in km
  return d;
}

function deg2rad(deg) {
  return deg * (Math.PI/180)
}


More info: http://www.movable-type.co.uk/scripts/latlong.html


Cheers

Hello, guys,

The Haversine formula...such memories! :)

I'd say that using it is definitely the way to go, but let me just give you a friendly reminder that the WatchPosition logice may not work properly on the background (since it's resources may be free and the plugin is not ready to run on the background).

Best regards,

Carlos Simões

Hi Carlos,

Can you elaborate a bit on "it's resources may be free and the plugin is not ready to run on the background"?

I have been using WatchPosition purely because by just trying to take a reading when required results in grossly inaccurate positions.  The GPS needs to be kept running all the time so that when you need a position, it is accurate.  Using WatchPosition, we continuously update a lat/lon so that it is always available when we need it.  This appears to work fine but occasionally I have noticed a number of points that are just plain BAD.

Are you saying Android may just occasionally free up some resources that WatchPosition is using causing intermittent problems?

Regards,

Lester

Hello, Lester,

I was referring to the fact that mobile applications, even when not explicitly closed, may have some of its resources freed.

In the case of OutSystems mobile apps, since most logic runs on top of a WebView, which is essentially a browser, that means that it may be closed to free up resources and the logic may stop running. From my experience, using modern devices, this usually happens when you open other apps and actively use them.

About the accuracy of the recorded positions: I haven't looked into this recently, but there were reports a while ago of GPS trackers switching off completely when the screen was turned off. Maybe this could explain it?

Best regards,

Carlos Simões

Hi Carlos,

Thanks - yes I was afraid that was what you meant.

As I mentioned, we start the GPS running and just continuously record position so that it is available when we need it.  My first implementation was a disaster when just calling GetLocation() whenever we wanted a location.  The GPS appears to power down until needed and then provides the first sloppy fix it has which was often up to 200m out!  Calling GetLocation a number of times in rapid succession appeared to be marginally better but the real fix was to make sure it stayed on by using WatchPosition.

I have not yet noticed errors with regard to the GPS driver suddenly not being available but I will investigate this a bit more to see if it is a problem or is related to the issue we see of some intermittent very poor fixes.

Thanks again for the feedback.

Hi Lester,

Have you been able to resolve the issue?  I am working on a similar issue.

Nothing further done on my side but seems to be working ok.  I think the some initial bad fixes we were seeing were due to poor device GPS.  Have not seen any issues using WatchPosition but don't try just calling GetLocation - That does give poor results.

Actually now that I re-read this thread, I have seen issues that could be related to GPS running in the background.  I have seen parts of my code getting called with values that just shouldn't be at that point in time.  Its very intermittent but does happen.  I will re-look into this issue with new insight and let you know.