NativeScript / nativescript-geolocation

Geolocation plugin to use for getting current location, monitor movement, etc
Apache License 2.0
139 stars 76 forks source link

How to get constant updates on geolocation? #57

Closed d3mac123 closed 7 years ago

d3mac123 commented 7 years ago

First of all, sorry if this is not the proper forum for my question.

I'm new to Nativescript (used to be a Corona/Lua developer) and I need to create a function (similar to a RuntimeEventListener in Lua) that constantly gets user location and updates a dashboard with speed and altitude, for example.

My current code gets this info only when a button is pressed (which does not make sense for the kind of app I am trying to build). Question is, how to create and invoke such listener/function?

I am coding in Javascript and below it is my current code:

var Observable = require("data/observable").Observable;
var frames = require("ui/frame");

var orientation = require('nativescript-orientation');
  orientation.enableRotation(); // The screen will  rotate 
  console.log(orientation.getOrientation());  // Returns the enum DeviceOrientation value

var dialogs = require("ui/dialogs");  

// Get geo coordinates
var geolocation = require("nativescript-geolocation");
if (!geolocation.isEnabled()) {
        geolocation.enableLocationRequest();
}
/*
var watchID
watchId = geolocation.watchLocation(
    function (loc) {
        if (loc) {
            console.log("(watchid) Received location: " + loc);
        }
    }, 
    function(e){
        console.log("(watchid) Error: " + e.message);
    }, 
    {desiredAccuracy: 3, updateDistance: 10, minimumUpdateTime : 1000 * 20}); // should update every 20 sec according to google documentation this is not so sure.
*/

    //variables for the dashboard and the Origin 
    var originLoc //holds the lat,long of the starting point
    var originHeading = "NNW"
    var originTime = "0"
    var originDistance = "0"

    var mySpeed = "0"
    var myDuration = "00:00"
    var myDistance = "0"
    var myAltitude = "0";
    var myDirection;

    var butAction = "START" //button action when it starts

var fbMeasurement = "imperial";

//Sets the right heading of the compass (if landscape, subtracts 90 degrees)
function headingCompass(args) {
    var compassHead = "";

    if (args>12 && args<=34) {
        compassHead = "NNE";
    } else if (args>34 && args<=57) {
        compassHead = "NE";
    } else if (args>57 && args<=80) {
        compassHead = "ENE";
    } else if (args>80 && args<=102) {
        compassHead = "E";
    } else if (args>102 && args<=124) {
        compassHead = "ESE";
    } else if (args>124 && args<=147) {
        compassHead = "SE";
    } else if (args>147 && args<=170) {
        compassHead = "SSE";
    } else if (args>170 && args<=192) {
        compassHead = "S";
    } else if (args>192 && args<=215) {
        compassHead = "SSW";
    } else if (args>215 && args<=237) {
        compassHead = "SW";
    } else if (args>237 && args<=260) {
        compassHead = "WSW";
    } else if (args>260 && args<=282) {
        compassHead = "W";
    } else if (args>282 && args<=305) {
        compassHead = "WNW";
    } else if (args>305 && args<=327) {
        compassHead = "NW";
    } else if (args>327 && args<=350) {
        compassHead = "NNW";
    } else {
        compassHead = "N";
    }  
    return compassHead;
}

//Gets current location when app starts
var geolocation = require("nativescript-geolocation");
if (!geolocation.isEnabled()) {
        geolocation.enableLocationRequest();
}
var location = geolocation.getCurrentLocation({desiredAccuracy: 3, updateDistance: 10, maximumAge: 20000, timeout: 20000}).
then(function(loc) {
    if (loc) {
        console.log("Current location is: " + loc);
        originLoc = loc;
        if (fbMeasurement === "imperial") {
            myAltitude = parseInt(loc.altitude * 3.28084);
            mySpeed = (loc.speed * 2.23694).toFixed(1);
        } else {
            mySpeed = loc.speed.toFixed(1);
            myAltitude = parseInt(loc.altitude);
        }
        myDirection = headingCompass(loc.direction)
    }
}, function(e){
    console.log("Error: " + e.message);
});

function createViewModel() {
    var viewModel = new Observable();

    viewModel.originHeading = originHeading;
    viewModel.originTime = originTime;
    viewModel.originDistance = originDistance;

    viewModel.mySpeed = mySpeed;
    viewModel.myDuration = myDuration;
    viewModel.myDistance = myDistance;
    viewModel.myAltitude = myAltitude;

    viewModel.butAction = butAction;

    //STARTs
    var watchid;
    viewModel.onTapStart = function(args) {
        if (butAction==="START") {

            //change button color to RED
            var btn = args.object;
            btn.backgroundColor = "#FF0000";
            //change button text to "STOP"
            this.set("butAction","STOP");
            butAction = "STOP";

            watchId = geolocation.watchLocation(
            function (loc) {
                if (loc) {
                    console.log("Received location: " + loc);

                    if (fbMeasurement === "imperial") {
                        myAltitude = parseInt(loc.altitude * 3.28084);
                        mySpeed = (loc.speed * 2.23694).toFixed(1);
                    } else {
                        mySpeed = loc.speed.toFixed(1);
                        myAltitude = parseInt(loc.altitude);
                    }
                    myDirection = headingCompass(loc.direction);

                }
            }, 
            function(e){
                console.log("Error: " + e.message);
            }, 
            {desiredAccuracy: 3, updateDistance: 10, minimumUpdateTime : 1000 * 1}); // should update every 20 sec according to google documentation this is not so sure.

        } else {
            //change button color to GREEN
            var btn = args.object;
            btn.backgroundColor = "#00FF00";
            //change button text to "START"
            this.set("butAction","START")
            butAction = "START";

            if (watchId) {
                geolocation.clearWatch(watchId);
            }
        }

        this.set("myAltitude",myAltitude);
        this.set("mySpeed",mySpeed);
        this.set("myDistance",myDirection);

    }

    return viewModel;
}

exports.createViewModel = createViewModel;
siulAzure commented 5 years ago

Have you been able to find a solution to this?