javascript - Nativescript 位置运行时更新

标签 javascript lua location nativescript

我是 Nativescript 的新手(曾经是​​ Corona/Lua 开发人员),我需要创建一个函数(类似于 Lua 中的 RuntimeEventListener),例如不断获取用户位置并更新仪表板的速度和高度.

我当前的代码仅在按下按钮时才会获取此信息(这对于我尝试构建的应用程序类型没有意义)。问题是,如何创建和调用这样的监听器/函数?

我正在使用 Javascript 进行编码,下面是我当前的代码:

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;

最佳答案

watchlocation 方法实际上是一个监听器,并且会在位置更改时更新您的位置(基于此 arguments )。但是,您将需要使用一些可观察属性来更新信息并在需要的地方和时间重用它。另外,请记住,在 Android 中,位置有时会在一定距离后触发(在我的例子中,大约 100 步给出了点后第四个符号的差异)。

如果您熟悉MVVM pattern这是 NativeScript 应用程序中经常使用的一种。 Here you can find the article for Data Binding in NativeScript .

所以基本上只需执行您的监视功能(例如,为您的Page使用loaded事件),然后监视Observable模型中的变化(例如,创建Observable属性纬度并使用随时随地更新信息)

例如

vm = new Observable();
vm.set("altitude", someDefaultValue);
vm.set("longitude", someDefaultValue);

geolocation.watchLocation(function(loc) { 
   vm.set("altitude", loc.altitude);
   vm.set("longitude", loc.longitude);

   console.log(vm.get("altitude")); // Observable model updated
   console.log(vm.get("longitude"));
})

关于javascript - Nativescript 位置运行时更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43898258/

相关文章:

javascript - 嵌套排序动态项目不折叠

javascript - p5.j​​s - 随机()、高度和宽度未定义?

java - xPath 元素位置 - selenium

javascript - 获取 OSRM 路由的 JSON

javascript - 替换为jquery函数

c++ - 带有 lua lib 的 xcode c++ 项目出现链接错误

gcc - Asm 代码说明

c++ - SWIG 如何使用 Lua 表从 Lua 中的 c 结构访问字节数组以进行操作

mysql - lat long 范围内的 SQL lat longs

pdf - 在 nginx 中按位置提供 pdf 文件