How to calculate distance over multiple points using location coordinates?

638 views
Skip to first unread message

dragon...@gmail.com

unread,
Jul 11, 2014, 5:31:35 AM7/11/14
to mitappinv...@googlegroups.com
I want to work out total distance while i travel.

I can calculate distance between two gps co-ordinates but am not sure how to calculate if traveling between multiple points and get the app keep updating the total distance while i'm moving.

I had thought if i could calculate two points then i could just keep adding the difference between a new point and a previous point but cant get my head around how to actually code something like that.

Does anyone know what i am trying to explain and have any ideas on how its done?


Hillechien

unread,
Jul 11, 2014, 8:02:51 AM7/11/14
to mitappinv...@googlegroups.com
To start with, you will want to define a global variable, totalDistance or something similar, initialized as 0.
Then, you will want two global variables, with names like lastLat and lastLong, initialized with the current lat-long values when you start the app. 
Now, there is a block which says when LocationSensor1. locationChanged do (...)
This should be called whenever you travel a certain distance (In my version of AndroidWheresMyCar it does not seem to work properly, but that is another issue). 
In this do area you can retrieve the current lat/long, calculate your distance with the last lat/long in your global variables, add it to the total distance global variable and replace the values in your global last lat/long variables. 
The next time the location changes you will execute the when ..locationChanged do ... again and you can repeat the trick.
I hope this makes sense to you.

SteveJG

unread,
Jul 11, 2014, 8:37:02 AM7/11/14
to mitappinv...@googlegroups.com
@ Hillechien .... very good answer.   Where is My Car  not working properly?    Are you having issues with replacing the LocationSensor period with a comma   or vice versa or other issues.   Ask a question and you will get suggestions for making it work for you.

@ dragon  ... Hillechien explains basically how to do this.   Yes, we know exactly what you want to do.

More explanation:
   What you want to do is a little complicated because it can be done in many different ways, especially the calculation of distance
  1    You can calculate the distance between two point using simple trig or the Haversine formula and store the distance  information on a TinyDB component and/or a list.   You save the distance information because you re going to have to keep adding to the previous distance to get the total distance.  There are several problems here.   Calculating the distances mathematically this way gives you a straight line distance.  If your two distance point were arrived by traversing a curvy road .. you underestimate the distance.  The are more issues, that is the simplist to explain.  Can it work and can you have fun with it?   Yes, of course.  You might look at the Where is My car tutorial  and the other location tutorials here:   http://explore.appinventor.mit.edu/ai2/tutorials?field_tutorial_type_tid%5B%5D=91&field_tutorial_type_tid%5B%5D=67  .Where is my car also explains how to store data temporarily but not necessarily in the way you will need to do it.

2)   You can calculate the distance actually driven using the API provided by Google and which is also used when you reguest distance betwen your location and Columbia, Mo   or whatever city is close to your location using the algorithms hidden in Google Maps.    This method is accurate, requires a lot of coding and is certainly doable with AI2.

dragon...@gmail.com

unread,
Jul 12, 2014, 7:25:56 AM7/12/14
to mitappinv...@googlegroups.com
Thanks, i have tried this but when trying to calculate total distance every time the location changes, it will only keep calculating the previous difference in distance plus the new difference distance and wont add on to all the other distances before those two as they are lost every time the distance updates.

I can get only seem to get the correct result when calculating difference between two locations only, the last location the new location which updates every location change.

I cant get it to calculate total overall distance over multiple location changes. 

Using a variable for the total just seems to get overridden when a "new" previous value gets calculated with the location change. 

When moving, i expect the location to update around every second so there is going to be a lot of distances that need to be calculated.

SteveJG

unread,
Jul 12, 2014, 7:54:42 AM7/12/14
to
Did you not know you have to save each calculation result somewhere?   ...and then sume the saved calculations.   If you do not need persistent data, you can save values in a list, then use the list to update a calculation.  What happens when you close the app in this instance?   All the saved data disappears!        If you want to save the data for future sessions, you either have to use a TinyDB to store  or use the File control to save the data as a csv file (and later use File to retrieve the data).

When you save a new distance to the Distance global, the Distance global is saved with the new value and wipes out the old.

If you do not need to save intermediate calculated distances, something like this might work:  
Try   Total Distance = TotalDistance + New Distance    ....If you do that, based on what you said you have done so far, you MIGHT get your desired result.    Please let us know.

How best to help you?     You have not learned some key AI2 programming concepsts based on your coding results and additional questions.

Please do this:   Load the Where Is My Car tutorial.    It demonstrates using the GPS, saving coordinates (you can save your distance calculations)   http://appinventor.mit.edu/explore/ai2/android-wheres-my-car.html     Also, read the section on Lists on this online book (it is free)  http://www.appinventor.org/book2

AND, you said you tried some things... no one here can help you figure out what might be wrong if YOU do not post block images..

If you do not do any of the tutorials, you will not learn anything about programming.    A lot of tutorials that teach you how to do things are here:   http://appinventor.mit.edu/explore/ai2/tutorials.html

Good luck... do some reading, tutorials, try some blocks and please do return when you have a specific question and can post some blocks.

Regards,
Steve

Hillechien

unread,
Jul 12, 2014, 6:09:50 PM7/12/14
to mitappinv...@googlegroups.com
@ steveJG
No I do not have problems with . and , (I was the one who raised the issue in the first place), but something is not working properly in the LocationSensor.
I will ask a new question to discuss this further.
Reply all
Reply to author
Forward
0 new messages