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
Dual Super User
Dual 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.

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
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

365 EduCon 768x460.png

Microsoft 365 EduCon

Join us for two optional days of workshops and a 3-day conference, you can choose from over 130 sessions in multiple tracks and 25 workshops.

Users online (2,404)