2017-05-10 1 views
0

저는 Nativescript (Corona/Lua 개발자로 사용됨)를 처음 사용하고 끊임없이 사용자 위치를 얻고 속도 및 속도로 대시 보드를 업데이트하는 기능 (Lua의 RuntimeEventListener와 유사)을 만들어야합니다. 예를 들어, 고도.Nativescript 위치 런타임 업데이트

현재 코드는 버튼을 눌렀을 때만 정보를 얻습니다 (빌드하려고하는 앱의 종류에는 맞지 않습니다). 질문은 그러한 리스너/함수를 생성하고 호출하는 방법입니까? 사실, 청취자

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; 

답변

0

watchlocation 방법이며이 변경 될 때 사용자의 위치를 ​​업데이트합니다 (이 arguments에 따라 : 나는 자바 스크립트로 코딩하고 그 아래에 있어요

내 현재 코드입니다). 그러나 관찰 가능 속성을 사용하여 정보를 업데이트하고 필요한 경우 언제 어디에서나 다시 사용할 수 있어야합니다. 또한 안드로이드에서 위치가 때로는 약간의 거리 (내 경우 약 100 단계가 점 뒤에 네 번째 사인에서 차이를 줬음) 후에 트리거된다는 것을 명심하십시오.

MVVM pattern에 익숙한 경우 NativeScript 응용 프로그램에서 정기적으로 사용됩니다. Here you can find the article for Data Binding in NativeScript.

그래서 기본적으로 그냥 시계 기능을 실행 (예를 들어 사용하여 페이지에 대한 이벤트를로드) 다음 (예를 들어, 관찰 가능한 속성 위도를 생성하고 업데이트 된 정보를 사용하여 언제 어디서 필요)

관찰 모델의 변경을 감시

예 :

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")); 
})