English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have an algorithm which produces an X/Y coordinate for a map based on Latitude and Longitude.
I would like to create an algorithm which does the exact opposite - takes an X and Y coordinate and returns the appropriate Lat/Long.

In my code, earthRadius, earthCircum and earthHalfRadius are constants which can be taken as given.

My program also has a zoom level, which has been taken into account in the code you see below:

Parameters: latitude, longitude, zoomLevel

double arc = earthCircum / ((1 << (zoomLevel)) * 256);
double sinLat = Math.sin(latitude);
double metersY = earthRadius / 2 * Math.log((1 + sinLat) / (1 - sinLat));
double y = (earthHalfCirc - metersY) / arc;

double metersX = earthRadius * longitude;
double x = (earthHalfCirc + metersX) / arc;

return: x, y

In case you are not familiar with code, the term (1 << (zoomLevel)) can be read as (2^zoomLevel).

Any ideas?

2007-01-18 06:39:45 · 3 answers · asked by Liam D 1 in Science & Mathematics Mathematics

3 answers

Seems pretty simple:
double longitude = (x * arc - earthHalfCirc)/earthRadius;
double latitude = Math.arcsin(Math.sqrt(1 - Math.exp((y * arc - earthHalfCirc) * 2/earthRadius)));

Not sure if all those math functions exist, but you can probably find substitutes...

2007-01-18 06:52:26 · answer #1 · answered by astazangasta 5 · 0 0

I think these are correct, you may want to check them

latitude = arcsin ( (exp(D) -1) / (1 + exp(D)) )

where D = earthRadius / 2 * [ (y*earthCircum/(2^zoomLevel) ) - earthHalfCirc ]

longitude = [ (earthCircum/(2^zoomLevel)) * x - earthHalfCirc ] / earthRadius

2007-01-18 07:13:54 · answer #2 · answered by Mike 5 · 0 0

Turn it upside down. (itmay be more confusing but I think it looks prettier that way !)

2007-01-18 06:55:56 · answer #3 · answered by derstrudelmonkey 4 · 0 0

fedest.com, questions and answers