- This topic has 11 replies, 3 voices, and was last updated 11 years, 4 months ago by SKnoxmn.
-
AuthorPosts
-
SKnoxmnMemberI’d really like to find some guidance on how to get the device orientation. Still running into the “Internet” where everyone, or a lot of them, seem to think orientation is all about portrait and landscape.
Anyhow, I’m doing this:
_________________________________
function getAngle()
{
if (window.DeviceOrientationEvent) {
console.log(“DeviceOrientation is supported”);
window.addEventListener(‘deviceorientation’, function(eventData) {
var Cant = eventData.gamma;
var Angle = eventData.beta;
var Dir = eventData.webkitCompassHeading;
deviceOrientationHandler(Cant, Angle, Dir);
}, false);
}
else
{
alert(“Not supported on your device or browser. Sorry.”);
}function deviceOrientationHandler(Cant, Angle, Dir){
$(‘#m1-Learning-textField4’).val(Angle);
$(‘#m1-Learning-textField5’).val(Dir);
}}
________________________________________And this is working to get the data, and putting in the fields where I want it. I’d like this to run for about 3 seconds and “Stop”. Leaving the data in the data in the fields.
I’d like to be able to get the data in the fields and be able to use that data in some math functions.
First part, in my mind, is figuring out how to get this thing to stop…
SKnox
BrandonMemberUse the timer function:
var myVar=setInterval(function(){function_to_run()},3000); // Run function every three seconds
Stop timer:
window.clearInterval(myVar);// stop the timer from running
SKnoxmnMemberI’d like it to run for the 3 seconds, not run every three seconds. 🙂 and then stop running.
SKnox
Unknown AuthorParticipantRemember that javascript is a multithreaded language. Therefore, to truly “halt”, you need a recursive function (go until a variable value changes, etc.). In one of my apps, I use recursion to wait for results to come back from the Google Places API, before displaying a results page.
-1TC
PS – If you just want to run for three seconds, then count the number of seconds and stop your loop when you hit 180 seconds. Easy.
SKnoxmnMember1TC
thanks, yeah I kind of figured it was going to be a timer of some kind that run for the duration then stop.
The problem I’m having is in finding the information on “how to” make it stop. maybe I’ll find a way to change the “false” to true. That might cause it to stop.
SKnox
BrandonMemberYou could make two timer functions. One that starts it running, and another that will stop it in three seconds.
SKnoxmnMemberIm still stuck. it looks like if I try to put in a timer then the function displays nothing.
I can’t find anything (yet) that will show me how to get a “snapshot” of lat lon alt direction and angle. its all about continuous updates and rotating images or putting pointers on a map.
Digested version is: I’d like to click a button and capture the device orientation at time (T)
SKnox
BrandonMemberTry something like this:
// Wait for device API libraries to load
//
document.addEventListener(“deviceready”, onDeviceReady, false);// device APIs are available
//
function myTimerFunction() {
navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
}// onSuccess: Get a snapshot of the current acceleration
//
function onSuccess(acceleration) {
alert(‘Acceleration X: ‘ + acceleration.x + ‘\n’ +
‘Acceleration Y: ‘ + acceleration.y + ‘\n’ +
‘Acceleration Z: ‘ + acceleration.z + ‘\n’ +
‘Timestamp: ‘ + acceleration.timestamp + ‘\n’);
}// onError: Failed to get the acceleration
//
function onError() {
alert(‘onError!’);
}and use the myTimerFunction() in the button
SKnoxmnMemberNope, doesn’t return anything. I’ll go check around on the navigator.accelerometer. and see what I come up with.
SKnox
SKnoxmnMemberNew direction;
Since I’m wanting to “capture” the angle to be used in some math. How about I just round the angle to the nearest integer. then my onClick would grab whatever is in that field at the time that the button is clicked and just use that.
I was concerned about how “shakey” the angle was being 10 decimal points deep and changing constantly. So I figure set some level of accuracy and just go with it. Since I’m pretty sure a full degree isn’t going to be a huge issue for my needs.
So now its on to learning some javascript math against a variable.
SKnox
BrandonMemberRound different numbers to the nearest integer:
var a=Math.round(2.60);
var b=Math.round(2.50);
var c=Math.round(2.49);
var d=Math.round(-2.60);
var e=Math.round(-2.50);
var f=Math.round(-2.49);
The result of a,b,c,d,e, and f will be:3
3
2
-3
-2
-2
SKnoxmnMemberYeah I got that part now. I was wondering for all of about 5 seconds about how to enter it into the JS.
So I took a shot and turns out it was right. So it would now appear that I can get the math. working for the variables.
I am looking for the “how to” rather than post the results to a textField Id rather have it just post (or display) as text next to the text label on the screen.
I was checking out the compass script that Wayne posted but I’ll need to get more coffee in me before it makes sense.
one more puzzle piece solved,
SKnox -
AuthorPosts