# How to convert Lat/Long to X/Y

Discussion in 'Questions' started by latcc, Feb 15, 2012.

1. ### latccBanned

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...

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

Any ideas?

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

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.

4. ### latccBanned

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: Feb 24, 2012