# Miles

86 views

### Jose Miguel

Jan 3, 2020, 7:56:05 PM1/3/20
to MIT App Inventor Forum
Hi....

I need to know if there is any example, to indicate how many kilometers (miles) a vehicle has traveled
since we digitized a start button, until we turned off the button ...

Thank you

### SteveJG

Jan 3, 2020, 9:08:47 PM1/3/20
No example in a tutorial Jose.  There are several ways that could be done.

You need a global variable to store total distance traveled as a running total.. Initially set it to 0.
When your LocationChanged event fires, record the new coordinates and calculate the distance between the start and the new.  You can calculate the distance either using Blocks and a Great Circle Algorithm or using other techniques.  Simplest is to calculate and store the distance in meters.  At the next LocationChanged event, add that distance to the totalDistance (or meters or km or miles). and so on.  When you finish, add the final distance to the List of distances.  Only then total the distance (in meters or miles or km.); convert to miles to avoid intermediate calculations and you are done.   Alternatively, store all the latitude/longitude coordinate pairs in a List. When you are 'done', calculate the distances between coordinates, one at a time and place the values in a List of distances.  Next, total all the distances in the List.  What works better?   I don't know.   What would be best is determined by how you would use the app to measure distance, how frequently you sample etc.

It is also possible to calculate distance between two points  using a block in the Map Marker component ( call Marker.DistanceToPoint ...latitude  longitude; it provides a distance in meters ) .

What have you coded so far Jose?

There are other ways of doing an app to make a running total of distance traveled.  Coding r something like described is relatively simple.    Why not try it?    How to use the LocationSensor is described in Using the Location Sensor in detail.

Here is a tutorial that shows some of what you might want to eventually do  App Inventor GPS Trak Basics  .   It posts a Marker on a map either using a timer or on demand.or using the LocationChanged block ( if I remember correctly)..adapt it to making the distance calculation?

Here are descriptions showing how to calculate distances between two sets of coordinates using either the Map component or using a Great Circle algorithm in Blocks   https://groups.google.com/forum/#!searchin/mitappinventortest/calculate\$20distance%7Csort:date/mitappinventortest/L1aqXGe0gaQ/lqf85EkYAgAJ   either method gives nearly identical results.

Regards,
Steve

### Taifun

Jan 4, 2020, 10:13:04 AM1/4/20
to MIT App Inventor Forum