Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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?

— asked October 12th 2010 by Grant Wolz
0 Comments

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)

    — answered December 14th 2011 by dit k
    permalink
    2 Comments
    • What is the unit for this distance ? Km ?
      Thanks

      — commented December 26th 2011 by Armindo Da Silva
    • yes, km :)

      — commented December 26th 2011 by dit k
  • 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.

    — answered October 12th 2010 by Mike Robinson
    permalink
    0 Comments
  • 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));

    — answered October 13th 2010 by Grant Wolz
    permalink
    0 Comments
  • 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));

    — answered October 13th 2010 by Grant Wolz
    permalink
    2 Comments
    • Hello Grant.
      Can you help me understand your formula please…
      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

      — commented July 2nd 2011 by Bruno Braga
    • @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)
      );
      

      — commented July 9th 2011 by Dawson Toth
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.