How to convert Lat/Long to X/Y

latcc

Banned
I wish to convert gps Lat/Long coords to X/Y coords. Precision is not paramount. I would be happy if it were within a quarter of a km accuracy.

I found this code online...

// convert lat/long to radians
latitude = Math.PI * latitude / 180;
longitude = Math.PI * longitude / 180;

// adjust position by radians
latitude -= 1.570795765134; // subtract 90 degrees (in radians)

// and switch z and y
xPos = (app.radius) * Math.sin(latitude) * Math.cos(longitude);
zPos = (app.radius) * Math.sin(latitude) * Math.sin(longitude);
yPos = (app.radius) * Math.cos(latitude);

The Math.PI would be our cPI and app.radius would be the enlargement of the map area I suspect.

Any ideas?
 

DouglasNYoung

Active Member
Licensed User
Longtime User
latcc,
If by x/y coordinates you mean Eastings and Northings as used in countries national grids then it all depends on where in the world you are. As the world is spherical and maps are flat the conversion is very local predominately depending on how far north of the equator you are.

The GPStoOSGB library performs the transformation for Great Britain (Basic4android - GPStoOSGB), but for other countries you'd have to research their local transformations.

Douglas
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
If you want actual X and Y pixel coordinates from latitude and longitude then you ought to say what range the pixel coordinates must be within...

For example if you want to draw a point (latitude, longitude) on a canvas and that canvas has a size of 500 pixels square then converting the latitude and longitude must take account of the canvas size.

Here's a couple of PHP functions that will convert latitude and longitude to floats which range from zero to one:

PHP:
function lonToX($lon){
   return ($lon/360)+0.5;
}

function latToY($lat){
   return (abs((asinh(tan(deg2rad($lat)))/M_PI/2)-0.5));
}

Multiplying the results of each float value by 500 would then give the required (X, Y) pixel position on the canvas.

Something to watch for though is the projection used by your (latitude, longitude) coordinates.
The PHP functions will convert WGS 84 coordinates.
WGS 84 is the projection used in Google Maps and many other coordinates systems BUT it is not the only project used.

Martin.
 
Upvote 0

latcc

Banned
If you want actual X and Y pixel coordinates from latitude and longitude then you ought to say what range the pixel coordinates must be within...

For example if you want to draw a point (latitude, longitude) on a canvas and that canvas has a size of 500 pixels square then converting the latitude and longitude must take account of the canvas size.

Here's a couple of PHP functions that will convert latitude and longitude to floats which range from zero to one:

PHP:
function lonToX($lon){
   return ($lon/360)+0.5;
}

function latToY($lat){
   return (abs((asinh(tan(deg2rad($lat)))/M_PI/2)-0.5));
}

Multiplying the results of each float value by 500 would then give the required (X, Y) pixel position on the canvas.

Something to watch for though is the projection used by your (latitude, longitude) coordinates.
The PHP functions will convert WGS 84 coordinates.
WGS 84 is the projection used in Google Maps and many other coordinates systems BUT it is not the only project used.

Martin.

I don't write in PHP. I was after Basic for Android-like code and not some other language. I have seen this code on the internet but it is useless with B4A. Does anyone have code that a B4A programmer can make sense of?
 
Last edited:
Upvote 0
Top