GPS and compass data would be sweet. #250

Closed
jessemillar opened this Issue Nov 13, 2013 · 4 comments

Projects

None yet

3 participants

@jessemillar

I've got a couple of game ideas in mind that mandate GPS and compass data. I tried throwing some prototypes together in Phonegap but it just wasn't cutting it. Whenever you find the time to add some calls to the geolocation API to Ejecta I would be eternally grateful and probably draw you a picture of a puppy.

@phoboslab phoboslab closed this in d661558 Nov 20, 2013
@phoboslab
Owner
var success = function(position) {
    console.log('position', JSON.stringify(position.coords), position.timestamp);
};
var error = function(error) {
    if( error.code === navigator.geolocation.PERMISSION_DENIED ) {
        // Remember that we don't have permissions and don't try again
    }
    console.log('error', error.code, error.message);
}

// One shot location - aims to return fast and may be imprecise.
// Set enableHighAccuracy to true for better results
navigator.geolocation.getCurrentPosition(success, error, {enableHighAccuracy:false});




// Draw line of continuos movement on canvas
var ctx = canvas.getContext('2d');
ctx.strokeStyle = '#f0f';
var first = true;
var coords = [];

var cx = canvas.width/2,
    cy = canvas.height/2;


var draw = function(position) {
    // clear
    ctx.fillStyle = '#000000';
    ctx.fillRect(0,0,canvas.width,canvas.height);

    // show coords
    ctx.fillStyle = '#ffffff';
    ctx.fillText( position.coords.latitude, 8, 16 );
    ctx.fillText( position.coords.longitude, 8, 32 );


    // collect coords but limit to 500
    coords.push( position.coords );
    if( coords.length > 500 ) {
        coords.shift();
    }

    // draw path
    ctx.beginPath();

    var first = coords[0]; // take first coords as center
    for( var i = 0; i < coords.length; i++ ) {
        var x = first.longitude - coords[i].longitude,
            y = first.latitude - coords[i].latitude;
        ctx.lineTo(cx + x * 10000, cy + y * 10000);
    }

    ctx.stroke();
};

// Continuously watch location; will always enable high accuracy
var watchId = navigator.geolocation.watchPosition(draw, error);

// Stop watching for position changes
// navigator.geolocation.clearWatch( watchId );
@jessemillar

You, sir, are a hero.

@Incubatio

puppy picture pls.

@jessemillar

screen shot 2013-12-09 at 9 42 17 am

(Nabbed a puppy drawing from one of my other projects.)

Any chance of adding the ability to monitor the current compass heading?

UPDATE
Created an issue for the compass data here: #258

@phoboslab phoboslab referenced this issue Dec 21, 2013
Closed

Compass Data? #258

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment