ppyordanov / Dynamic-Noise-and-Pollution-Map

A dynamically generated University of Glasgow noise and pollution campus map via the innovative Smart Citizen Kit developed in Barcelona, Spain by FabLab.
0 stars 0 forks source link

Client Position Check Window #62

Closed ppyordanov closed 9 years ago

ppyordanov commented 9 years ago

There is a bug when checking the current position, causing the client to make requests to the SCK central server more often than needed (every 3-5 seconds) considering that the updates are sent every 30 seconds by the Smart Citizen Device. This needs to be debugged/ refactored.

ppyordanov commented 9 years ago

The code refactoring has been completed on this stage. The operations carried out in relation to geolocation have been extracted in a separate function for better code readability:

function localize(){

    var date = new Date();
    var time_ms = date.getTime();

    location_id = navigator.geolocation.getCurrentPosition(
        //SUCCESS
        function(current_location){
            route_data.push(current_location);
            getSCKData(current_location, time_ms);

        },
        //FAIL
        function(error_message){
            console.log(error_message);
        },
        { enableHighAccuracy:true,timeout:10000}
    );

}

The previous method required the use of navigator.geolocation.getWatchPosition(success, fail);. This creates a continuous stream of GPS data retrieval that does not seem to be affected by timeout or setInterval() functions which is what the system needs in order to function properly when a specific temporal window value has been input by the user in the relevant field. This is what made it necessary to change the approach: localize() is currently making use of navigator.geolocation.getCurrentPosition(success, fail); which retrieves current device geolocation on request and can be configured with a timeout:

setInterval(localize, TIME_WINDOW);

The isNaN() function verifies the field input. If invalid data has been provided, the default time window is used (30 000 ms):

    //checking time window value
    //window at least a second
    var winVal = $("#window").val();
    if(winVal!='' && isNaN(winVal)==false && winVal >= 1000){
        WINDOW = parseInt(winVal);
    }

All of the events described above are fired on pressing the button to start route tracking. Logging and prompts are extensively used to let the user know what is happening at any point of time during the tracking process.