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 II
Super User II

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
PA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Power Query PA Forum 768x460.png

Check it out!

Did you know that you can visit the Power Query Forum in Power BI and now Power Apps

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.

R2 (Green) 768 x 460px.png

Microsoft Dynamics 365 & Power Platform User Professionals

DynamicsCon is a FREE, 4 half-day virtual learning experience for 11,000+ Microsoft Business Application users and professionals.

Users online (2,003)