So I've been following samples from the kitchen sink, and adapting the existing code to my needs. Problem is, I think I might have made some mistakes along the way.
I ran my app while I was driving and got somewhere along the lines of 50x the distance I actually traveled.
I am using the following formula to get the distance between to lat/long points:
var getDistance = function (lat1, lat2, lon1, lon2){ var R = 6371; // km var dLat = toRad(lat2-lat1); var dLon = toRad(lon2-lon1); var lat1 = toRad(lat1); var lat2 = toRad(lat2); var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); var d = R * c; return d * 0.621371; // converting to miles };I've assumed the return value for that function is in km so I do a conversion towards the end.
The way I call it is as follows:
var updates = 0; var locationChangedHandler = function(e){ if (!e.success || e.error){ // error handling } if (e.coords != null){ 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; addNewCoordinate(latitude,longitude); updates ++; $.status.text = "UPDATED " + updates; $.lblSpeed.text = speed + " mi/h"; } }; var addNewCoordinate = function(lat, lon){ var lastDistance = distance; distance += getDistance(lastLat, lat, lastLong, lon); model.distance = Math.round(distance*100)/100; // max 500 $.lblDistance.text = model.distance + " mi"; $.lblDistance.text = model.distance; if (distance > lastDistance){ var m = Alloy.createModel('Coordinate', {lat: lat, lon: lon, exerciseInstanceID: model.id}); m.save(); } $.lastupdated.text = (new Date()).toString(); $.lat.text = lat; $.lon.text = lon; }; this.startGPSTracking = function(){ Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST; Titanium.Geolocation.preferredProvider = Titanium.Geolocation.PROVIDER_GPS; Titanium.Geolocation.purpose = "C7 Tracking"; Titanium.Geolocation.distanceFilter = 2; // TODO: make this adaptive $.status.text = "GPS tracking STARTED"; // we need to save the model to have an id if ((model.id == null)||(model.id === "")||(model.id === 0)){ exerciseID = 0; setEditor.saveCurrentExercise(updateExerciseID); } else{ exerciseID = model.id; } setDistance(model); Titanium.Geolocation.getCurrentPosition(function(e){ $.status.text = "First Position Retrieved"; if (!e.success || e.error) { // error handling } var longitude = e.coords.longitude; var latitude = e.coords.latitude; var altitude = e.coords.altitude; lastLat = latitude; lastLong = longitude; addNewCoordinate(latitude, longitude); }); $.status.text = "STARTED"; Titanium.Geolocation.addEventListener('location',locationChangedHandler); }; this.stopGPSTracking = function(){ Titanium.Geolocation.removeEventListener('location', locationChangedHandler); $.status.text = "STOPPED"; };The problem is the distance I am getting is about 50x the actual distance. It's probably noteworthy I tested this while driving my car.
I did notice that the speed returned by e.coords.speed returns 28 when I'm traveling at 60 mph. I also noticed that when the car is in full stop, the distance continues incrementing, so there's probably something wrong with how I'm handling the event as well.