# Calculate Distance Traveled?

So far I have my iphone app getting the users current location and displaying it on the map.

``````Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST;

//
//  SET DISTANCE FILTER.  THIS DICTATES HOW OFTEN AN EVENT FIRES BASED ON THE DISTANCE THE DEVICE MOVES
//  THIS VALUE IS IN METERS
//
Titanium.Geolocation.distanceFilter = 10;

// SET GEO Reason
Ti.Geolocation.purpose = "Recieve User Location";

var win = Titanium.UI.currentWindow;

//
// GET CURRENT POSITION - THIS FIRES ONCE
//
Titanium.Geolocation.getCurrentPosition(function(e)
{
if (e.error)
{
return;
}

var longitude = e.coords.longitude;
var latitude = e.coords.latitude;
var altitude = e.coords.altitude;
var accuracy = e.coords.accuracy;
var speed = e.coords.speed;
var timestamp = e.coords.timestamp;
var altitudeAccuracy = e.coords.altitudeAccuracy;

//
//CREATE MAP VIEW
//
var mapview = Titanium.Map.createView({
mapType: Titanium.Map.STANDARD_TYPE,
region: {latitude: latitude, longitude: longitude, latitudeDelta:0.01, longitudeDelta:0.01},
animate:true,
regionFit:true,
userLocation:true,

});

});
``````

My end goal is to have a start and stop button. User presses start walks so far, hits stop, and I output the distance traveled, speed etc. I can't seem to find much about doing something like this and the documentation on maps is a bit limited to finding positions, and dropping pins. An ideas?

• Like Dawson said:

``````var Dist = 3963.0 * Math.acos(
Math.sin(lat1 / 57.2958) * Math.sin(lat2 / 57.2958)
+ Math.cos(lat1 / 57.2958) * Math.cos(lat2 / 57.2958)
* Math.cos(lon2 / 57.2958 - lon1 / 57.2958)
);
``````

where: 3963.0 Earth radius AND 57.2958 := 180 / 3.1415… (Pi)

• What is the unit for this distance ? Km ?
Thanks

• yes, km :)

• I don't think the Map is going to provide that information for you……what you should probably look at is taking a starting & ending location and then using the haversine distance formula to calculate the distance travelled between the two geolocations. You should be able to find several javascript implementations of haversine distance already done.

• Thanks for the heads up that is what ended up doing. If anyone wants it this is what I am using.

var Dist = 3963.0 Math.acos(Math.sin(lat1/57.2958)Math.sin(lat2/57.2958) + Math.cos(lat1/57.2958)Math.cos(lat2/57.2958) Math.cos(lon2/57.2958 -lon1/57.2958));

• Thanks for the heads up that is what ended up doing. If anyone wants it this is what I am using.

var Dist = 3963.0 Math.acos(Math.sin(lat1/57.2958)Math.sin(lat2/57.2958) + Math.cos(lat1/57.2958)Math.cos(lat2/57.2958) Math.cos(lon2/57.2958 -lon1/57.2958));

• Hello Grant.
what is that value: 57.2958?
and why does your formula starts with 3963.0 SPACE Math.acos..
Shouldn't be any operator between 3963.0 and Math.acos?

Appreciate your help cause i will need to use that formula to :)

Best regards,
B

• @Bruno His code wasn't wrapped in tildes, so markdown formatted his multiplication symbols as italics. The following should be the code he actually meant to send:

``````var Dist = 3963.0 * Math.acos(
Math.sin(lat1 / 57.2958) * Math.sin(lat2 / 57.2958)
+ Math.cos(lat1 / 57.2958) * Math.cos(lat2 / 57.2958)
* Math.cos(lon2 / 57.2958 - lon1 / 57.2958)
);
``````

The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.