Update on my webOS/pre app and some Long/Lat to X/Y code

Here are some screenshots of my webOS golf application I’m developing for the Palm pre.  It draws all the information dynamically from gps recorded information and allows one to zoom in and out and drag the course around.

webos app development screenshotwebos app development screenshotwebos app development screenshot

Eventually, it will do all the scoring an location tracking with distance calculations.  Unfortunately I don’t yet have access to the Mojo SDK yet.  Hopefully it will come soon as the Palm pre is to be released on June 5th/6th.  I know I’ll be up early that morning or there early the night before trying to pick up a pre.

Well… I had previously mentioned that I got the longitude / latitude conversions to/from pixel x/y working.  Here is a snippet of the two functions that will hopefully be helpful to some of you out there.

Lat/Long to X/Y

var gpsLatLongPoint = {lat:degreeslat, long:degreeslong};

var gpsBounds = [
{lat:degreeslat northern, long:degreeslong western},
{lat:degreeslat southern, long:degreeslong eastern}

var mapWidthPixels = 800;
var mapHeightPixels = 600;

var X = ((gpsLatLongPoint.long – gpsBounds[0].long) / (gpsBounds[1].long – gpsBounds[0].long) * mapWidthPixels);
var Y = (mapHeightPixels – (gpsLatLongPoint.lat – gpsBounds[0].lat) / (gpsBounds[1].lat – gpsBounds[0].lat) * mapHeightPixels);

var pixelXYPoint = {x:X, y:Y};

X/Y to Lat/Long

//This code is a hodge of various different pieces I found on the net.

var pixelXYPoint = {x:X, y:Y};

var latitudeSpan = gpsBounds[1].lat – gpsBounds[0].lat;
var longitudeSpan = gpsBounds[1].long – gpsBounds[0].long;
var microDegreePerPixelLatitude = latitudeSpan / mapHeightPixels;
var microDegreePerPixelLongitude = longitudeSpan / mapWidthPixels;
var topLeftScreenX = 0; // could be a map offset value
var topLeftScreenY = 0; // could be a map offset value
var changeLatitude = microDegreePerPixelLatitude * (topLeftScreenY – pixelXYPoint .y);
var changeLongitude = microDegreePerPixelLongitude * (topLeftScreenX – pixelXYPoint .x);
var newLat = gpsBounds[1].lat + changeLatitude ;
var newLong = gpsBounds[0].long – changeLongitude ;

var gpsLatLongPoint = {lat:newLat , long:newLong };

Well… I hope some of that code helps some others out there, as this stuff took me forever to get working.  I’m starting to understand some of it though.  🙂  Well… Till next time.

Leave a Reply

Powered by: Wordpress