How to convert Android GPS coordinates into X,Y coordinates.

Without further math bullshit about all the conversion systems, when you have a bunch of Android GPS coordinates (which are compatible with Google Earth and Google Maps), and you want to draw them on a finite 2D plane, here’s what worked for me.

int x = (int) ((PLANE_WIDTH/360.0) * (180 + lon));
int y = (int) ((PLANE_HEIGHT/180.0) * (90 – lat));

Animating a game-like sky with HTML5 Canvas

Try it | View Source

Again playing a little more with HTML5 and Canvas animation.

This time around the result is a little more pleasing to the eye, based on some ideas I have for a little arcade game I want to make I’ve created a gradient blue sky and clouds that move either to the left or right at different speeds.

If I’m correct, I don’t think there is a way to move something that’s already been painted in the canvas, therefore I made the Clouds a “Cloud” object (I used to think I could paint a vector based graphic and then manipulate it in the canvas, but I think now the HTML Canvas is more of a place where you paint and you can’t really manipulate what’s already been painted). Each cloud is nothing but an array of white circles. The program initializes several clouds randomly and then with an interval call it repaints the gradient sky, and each one of the clouds, then moves them, to repeat the cycle over and over.

I’m not sure if game devs out there are doing this or if there is a way to have the canvas move vector objects in a more efficient way than repainting the whole thing from scratch on every frame. At this point the animation looks very smooth but nothing except clouds moving is happening.

I hope in your comments I’ll find some answers as if this is the way to animate your game, meaning, keeping object representations of everything that’s been painted in memory, and repainting the canvas on every frame of the game cycle.

My goal (which I don’t know if is correct) is to do everything within a single CANVAS object. I’ve seen though how some people still like to play around with the DOM Tree, I’m not sure if adding and manipulating the DOM Tree is the correct way to procede here, I think it’d also be a nightmare to handle things when it comes to focus, or converting coordinates from one canvas to another and have everything make sense.

Playing with basics of HTML5 Canvas

Here’s a snippet of something I did tonight to play a bit with the Canvas and 2d Graphics context objects in javascript.

<title>Playing with Canvas</title>

<body style="padding:0 0; margin: 0 0; background:black">

<canvas id="myCanvas" width="1024" height="1024" ></canvas>

<script type="text/javascript">

//convert to CSS friendly Hex String
function d2h (n) {
var result = n.toString(16);
if (n < 16) {
result = ‘0’+result;
return result;

var preparingBackground = true;
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");

var canvasWidth = parseInt(canvas.getAttribute("width"));
var canvasHeight = parseInt(canvas.getAttribute("height"));

var r=255; var red;
var g=255; var green;
var b=255; var blue;

var rectHeight = canvasHeight/256;
var rectWidth = canvasWidth/256;

var rectX = 0;
var rectY = 0;

var Rect = function(xval,yval,speedXval,speedYval,r,g,b) {
this.x = xval;
this.y = yval;

this.speedX = speedXval;
this.speedY = speedYval;

this.r = r;
this.g = g;
this.b = b;

//so colors dont jump from 255 to 0.
this.rDelta = 1;
this.gDelta = 1;
this.bDelta = 1;

this.width = 1;
this.height = 1;

//so rectangle colors wont change so fast.

this.move = function () {
this.x = this.x+this.speedX;
this.y = this.y+this.speedY;

//if you reach a borders change direction.
if ((this.x+this.width >= canvasWidth && this.speedX > 0) ||
(this.x <= 0 && this.speedX < 0)) {
this.speedX = this.speedX*-1;

if ((this.y+this.height >= canvasHeight && this.speedY > 0) ||
(this.y <= 0 && this.speedY < 0)) {
this.speedY = this.speedY*-1;

//draw and change colors
this.draw = function () {
//play with color gradients when you have a chance…
if (this.colorChangeAcumulator–==0) {
if ((this.r >= 255 && this.rDelta > 0) ||
(this.r <= 0 && this.rDelta < 0)) {

if ((this.g >= 255 && this.gDelta > 0) ||
(this.g <= 0 && this.gDelta < 0)) {

if ((this.b >= 255 && this.bDelta > 0) ||
(this.b <= 0 && this.bDelta < 0)) {

this.r=(this.r+this.rDelta) % 256;
this.g=(this.g+this.gDelta) % 256;
this.b=(this.b+this.bDelta) % 256;


//paint the reactangle

//animation interval ids
var prepareBackgroundIntervalId = 0;
var rectsIntervalId = 0;

var speedX = 7;
var speedY = 7;

var theRects = [new Rect(0,0,speedX,speedY,10,44,100),
new Rect(canvasWidth,canvasHeight,-speedX,-speedY,125,0,0),
new Rect(canvasWidth,0,-speedX,speedY,0,210,80),
new Rect(0,canvasHeight,speedX,-speedY,40,40,0),
new Rect(canvasWidth/2,0,0,speedY,-4,40,40),
new Rect(canvasWidth/2,canvasHeight,4,-speedY,0,100,250),
new Rect(0,canvasHeight/2,speedX,0,210,100,100),
new Rect(canvasWidth,512,-speedX,0,100,10,0)];

function prepareBackground() {

if (preparingBackground) {

red = d2h(r);
green = d2h(g);
blue = d2h(b);

ctx.fillStyle = "#"+red+green+blue;

rectY = rectY + rectHeight;

preparingBackground = !(r==0 && g==0 && b==0);
} else {
rectsIntervalId = setInterval("moveRects()",33);

function moveRects() {
for (i in theRects) {
var rect = theRects[i];

prepareBackgroundIntervalId = setInterval("prepareBackground()",1);