Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Journey Recording using GPS Tracking

Hi Joe,

There are 2 modes of Geolocation: monitoring accurate data from the satellite (function watchPosition) and less accurate data (IP location or wifi) (function getCurrentPosition).

By default, getCurrentPosition () tries to answer as fast as possible with a low accuracy result. It is usefull if you need a quick answer regardless of the accuracy. Devices with a GPS, for example, can take a minute or more to get a GPS fix, so less accurate data (IP location or WiFi) may be returned to getCurrentPosition ().

watchPosition () produces more accurate data, but you can not control when it's received. (Eg data may be received after the turn and part of the journey will be cut off).

Besides using watchPosition function successCallback invokes only if new position differs significantly from the previous position - because of this if you drive slowly and when standing still - successCallback function is invoked rarely (once per minute or less), and update is rare.

That's why accurate (up to one meter) measuring the distance and speed is impossible. Distance will be obtained only approximately (+ - a few percent).

Here's a little guide how to try this. Distance is obtained by means of function watchPosition. When satellite signals are installed (this can take a few minutes) - the motion data will be updated every 3-4 seconds. Distance will be measured quite accurately. After pressing the init Geolocation service runs for communications with the satellite. After clicking on the start button gathering data starts. When you press the pause data collection is suspended (for example, during a stop).

1) Add Geolocation service to your app

2) On page where you want to record speed add Geolocation service named geolocation1 with the following Request parameters:
maximumAge = 3000
timeout = 5000
enableHighAccuracy = true
watchPosition = checked

3) Create new JS asset with the following code:
codevar running = false, paused = true, oldLat = 0, oldLng = 0, oldTime = 0, totalTime = 0, totalDist = 0, invokeTimes = 0, map, marker;
function findDistance(t1, n1, t2, n2) {
var lat1, lon1, lat2, lon2, dlat, dlon, a, c, dk, mi, km, Rk = 6371 ;

Code: Select all

     // convert coordinates to radians 
     lat1 = deg2rad(t1); 
     lon1 = deg2rad(n1); 
     lat2 = deg2rad(t2); 
     lon2 = deg2rad(n2); 

     // find the differences between the coordinates 
     dlat = lat2 - lat1; 
     dlon = lon2 - lon1; 

     // here's the heavy lifting 
     a  = Math.pow(Math.sin(dlat/2),2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon/2),2); 
     c  = 2 * Math.atan2(Math.sqrt(a),Math.sqrt(1-a)); // great circle distance in radians 
     dk = c * Rk; // great circle distance in km 

     // round the result down to the nearest 1/1000 
     km = round(dk); 
     return km; 

}

// convert degrees to radians
function deg2rad(deg) {
rad = deg * Math.PI/180; // radians = degrees * pi/180
return rad;
}

// round to the nearest 1/1000
function round(x) {
return Math.round( x * 1000) / 1000;
}

//convert Milliseconds to Time string
function mSecToTime(mSec){
var time = Math.floor(mSec/1000);
var hours = Math.floor(time / 3600);
time = time - hours * 3600;
var minutes = Math.floor(time / 60);
var seconds = time - minutes * 60;
return hours + ':' + minutes+ ':' + seconds;
}

function updateMarker (lat, lng){
if (!map) {
map = Tiggzi("map").gmap;
}
var newPoint = new google.maps.LatLng(lat, lng);
if (marker) {
// Marker already created - Move it
marker.setPosition(newPoint);
} else {
// Marker does not exist - Create it
marker = new google.maps.Marker({
position: newPoint,
map: map
});
}
// Center the map on the new position
map.setCenter(newPoint);
}/code
4) Add on page:

  • map named map

  • 5 labels with names: errorLabel, totalDistLabel, totalTimeLabel, avrSpeedLabel, dataLabel

  • 3 buttons named: init, start, pause

    5) on Page show run:
    codeAppery("pause").hide();
    Appery("start").hide();/code
    6) on init-Click run:
    codeAppery("init").hide();
    Appery("start").show();
    if (!running) {
    running = true;
    geolocation1.execute({});
    }/code
    7) on pause-Click run:
    codepaused = true;
    Appery("pause").hide();
    Appery("start").show();
    oldTime = 0;
    oldLat = 0;
    oldLng = 0;/code
    8) on start-Click run:
    codepaused = false;
    Appery("pause").show();
    Appery("start").hide();/code
    9) on geolocation1-Success run:
    codeAppery("errorLabel").hide();
    invokeTimes++;
    Appery("dataLabel")&#46;html('Latitude: ' + data&#46;coords&#46;latitude + '<br>' +
    'Longitude: ' + data&#46;coords&#46;longitude + '<br>' +
    'Altitude: ' + data&#46;coords&#46;altitude + '<br>' +
    'Accuracy: ' + data&#46;coords&#46;accuracy + '<br>' +
    'Timestamp: ' + data&#46;timestamp + '<br>');
    if(!paused) {
    if(oldTime != 0) {
    var dist = findDistance(oldLat, oldLng, data&#46;coords&#46;latitude, data&#46;coords&#46;longitude);
    totalDist += dist;
    totalTime += data&#46;timestamp - oldTime;
    }
    oldTime = data&#46;timestamp;
    oldLat = data&#46;coords&#46;latitude;
    oldLng = data&#46;coords&#46;longitude;
    Appery("totalDistLabel")&#46;text("Distance: " + round(totalDist) + ' km');
    Appery("totalTimeLabel")&#46;text("Time: " + mSecToTime(totalTime));
    if (totalTime>0) {
    Appery("avrSpeedLabel")&#46;text("Average speed: " + round(totalDist&#47;(totalTime&#47;(1000*3600))) + " km&#47;h");
    } else {
    Appery("avrSpeedLabel")&#46;text("Average speed: 0 km&#47;h");
    }
    }
    updateMarker(data&#46;coords&#46;latitude, data&#46;coords&#46;longitude);/code
    10) on geolocation1-Fail run:
    codevar date = new Date();
    date&#46;getHours() + ':' + date&#46;getMinutes() + ':' + date&#46;getSeconds()
    Appery("errorLabel")&#46;text("Some error occured at " + (date&#46;getHours() + ':' + date&#46;getMinutes() + ':' + date&#46;getSeconds()));
    Appery("errorLabel")&#46;show();/code

    Please try adding this on the NEW app, not yours. Because it can conflict with the old one (for example, because of the same names of variables or functions). If it works correctly then try it in your app.

Joe Paisley
Posts: 0
Joined: Thu Mar 14, 2013 8:41 pm

Journey Recording using GPS Tracking

Thanks Katya! I'll look through it and get back to you! I'll also reshare the new "test" app and notify you once its shared and give you the name of the app. Thanks again!

Joe Paisley
Posts: 0
Joined: Thu Mar 14, 2013 8:41 pm

Journey Recording using GPS Tracking

Alright, I think I've added everything correctly, except, I'm somehow confused how to add geolocation1 -success and fail. Can you take a look? Or show me a screenshot of how to do this? It's sort of a silly question to have. Thanks!

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

Journey Recording using GPS Tracking

Hello!

Check please on the Data view tab, Events - geolocation1 -Success- Run JavaScript; geolocation1 - Error- Run JavaScript.

Joe Paisley
Posts: 0
Joined: Thu Mar 14, 2013 8:41 pm

Journey Recording using GPS Tracking

Alright, all done adding those lines of code, what do I need to do now? Are you able to view that app called "Record Ride Test"?

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Journey Recording using GPS Tracking

Hello! You would need to click on init button to connect with GPS satellite, wait some time (about a minute), click start button to start recording, your journey data will be displayed on screen.

Joe Paisley
Posts: 0
Joined: Thu Mar 14, 2013 8:41 pm

Journey Recording using GPS Tracking

So here's the log that running the app in Eclipse produced: Image
It centered the map on the page to my location, but there was no marker. None of the labels did anything.
The buttons only appeared one at a time (which is fine with me as long as they work). They appear in a series, and you can see in the log that I clicked "init" then "start" then "pause."
Thanks!

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Journey Recording using GPS Tracking

Hi Joe,

Please delete
startScreen-Load-Invoke service-Datasource: geolocation1

Joe Paisley
Posts: 0
Joined: Thu Mar 14, 2013 8:41 pm

Journey Recording using GPS Tracking

Wow, when I run eclipse, it kicks out a lot of things in the "Logcat" Too much to post here. Is the "Init" button supposed to start the GPS service on the phone? Or does it just settle for the coarse location with WIFI and the cellular network? Nothing is happening with the labels, but the marker is showing up. How do I customize that marker? How will the data be recorded or populated to view for the user?

Joe Paisley
Posts: 0
Joined: Thu Mar 14, 2013 8:41 pm

Journey Recording using GPS Tracking

Do I need to start creating localStorage variables and mapping labels in data mapping for "geolocation 1"? How would I do that?

Return to “Issues”