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)
{
alert('Cannot get your current location');
return;
}
var longitude = e.coords.longitude;
var latitude = e.coords.latitude;
var altitude = e.coords.altitude;
var heading = e.coords.heading;
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,
});
win.add(mapview);
});
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?
4 Answers
-
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)
-
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));