cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anonymous
Not applicable

track distance using long and lat

Hello everyone,

I am working on a app where someone can press a button that starts a GPS timer. it give long and lat every 5 seconds. Now i want to add a field that displays the distance traveled. The timer looks like this.

 

onselect = Patch('[dbo].[GPS]', Defaults('[dbo].[GPS]'), {trajectID: varTrajectId.ID, time: Text(Now(), "[$-nl-NL]hh:mm:ss"), lat: Location.Latitude, long: Location.Longitude})

 

Can anyone help me? Thanks in advance. 

1 ACCEPTED SOLUTION

Accepted Solutions

Hi @Anonymous ,

Firstly, put this at Screen OnVisible

UpdateContext(
   {
      lastLatitude:0,
      lastLongitude:0
   }
)

Add a numeric column to your list called distance
Then put this on your Timer to replace the posted code

If(
   lastLatitude=0,
   UpdateContext({lastLatitude,Location.Latitude}
);
If(
   lastLongitude=0,
   UpdateContext({lastLongitude,Location.Longitude}
);
With(
   {
      wDist:
      12742 * 
      Asin(
         Sqrt(
            0.5 - 
            Cos(
               (lastLatitude - Location.Latitude) * 0.0174532925199432
            )/2 + 
            Cos(
               lastLatitude * 0.0174532925199432
            ) * 
            Cos(
               Location.Latitude * 0.0174532925199432
            ) * 
            (
               1 - 
               Cos(
                 (lastLongitude - Location.Longitude) * 0.0174532925199432
               )
            ) / 2
         )
      ) * 1000
   },
   Patch(
      '[dbo].[GPS]', 
      Defaults('[dbo].[GPS]'), 
      {
         trajectID: varTrajectId.ID, 
         time: Text(Now(), "[$-nl-NL]hh:mm:ss"), 
         lat: Location.Latitude, 
         long: Location.Longitude
         distance:wDist
      }
   )
);
UpdateContext(
   {
      lastLatitude:Location.Latitude,
      lastLongitude:Location.Longitude
   }
)

Note that the screen and app needs to stay open for all of this to work.

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

View solution in original post

4 REPLIES 4
gabibalaban
Super User
Super User

Hi @Anonymous ,

This is more like a geolocation problem :).

Here is a formula to calculate distance between 2 points based on their GPS location:

distance = 

12742*Asin(Sqrt(0.5-Cos((lastLatitude-currentLatitude)*0.0174532925199432)/2+Cos(lastLatitude*0.0174532925199432)*Cos(lastLatitude*0.0174532925199432)*(1-Cos((lastLongitude-currentLongitude)*0.0174532925199432))/2))*1000

 

Hope it helps !

Anonymous
Not applicable

Thanks. But i have no idea where in the app I should put this.

Hi @Anonymous ,

Firstly, put this at Screen OnVisible

UpdateContext(
   {
      lastLatitude:0,
      lastLongitude:0
   }
)

Add a numeric column to your list called distance
Then put this on your Timer to replace the posted code

If(
   lastLatitude=0,
   UpdateContext({lastLatitude,Location.Latitude}
);
If(
   lastLongitude=0,
   UpdateContext({lastLongitude,Location.Longitude}
);
With(
   {
      wDist:
      12742 * 
      Asin(
         Sqrt(
            0.5 - 
            Cos(
               (lastLatitude - Location.Latitude) * 0.0174532925199432
            )/2 + 
            Cos(
               lastLatitude * 0.0174532925199432
            ) * 
            Cos(
               Location.Latitude * 0.0174532925199432
            ) * 
            (
               1 - 
               Cos(
                 (lastLongitude - Location.Longitude) * 0.0174532925199432
               )
            ) / 2
         )
      ) * 1000
   },
   Patch(
      '[dbo].[GPS]', 
      Defaults('[dbo].[GPS]'), 
      {
         trajectID: varTrajectId.ID, 
         time: Text(Now(), "[$-nl-NL]hh:mm:ss"), 
         lat: Location.Latitude, 
         long: Location.Longitude
         distance:wDist
      }
   )
);
UpdateContext(
   {
      lastLatitude:Location.Latitude,
      lastLongitude:Location.Longitude
   }
)

Note that the screen and app needs to stay open for all of this to work.

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

View solution in original post

Hi @Anonymous ,

Just checking if you got the result you were looking for on this thread. Happy to help further if not.

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Welcome Super Users.jpg

Super User Season 2

Congratulations, the new Super User Season 2 for 2021 has started!

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

Users online (1,368)