[Webinar] Streamline your web hosting managementRegister Today

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 377

# Converting Longitude and Latitude to Google Image Tiles Javascript

I am trying to get this code to work in the Acrobat JavaScript api. I keep getting 0,0 as the result. Any help would be appreciated.

lat = 65.06518345;
lon = -146.09069824;
zoomLevel = 17;

function getXYfromLatLon(lat, lon, zoomLevel) {
var lon = 180.0 + lon;
var x = Math.floor( (lon / 360.0) * 131072 );
/* 2^17 = 131072 */
x >>= zoomLevel;
var lat = lat / 180.0 * 3.1415926;
var y = Math.PI - 0.5 * Math.log((1+Math.sin(lat))/(1-Math.sin(lat)));
y = Math.floor( (y / 2 / Math.PI) * 131072 );
y >>= zoomLevel;
return new Array(x,y);
}

getXYfromLatLon(lat, lon, zoomLevel)
0
• 4
• 3
1 Solution

PluritechnicianCommented:
Do it work with : zoomLevel = 1 ?

Try this :
``````function getXYfromLatLon(lat, lon, zoomLevel) {
var lon = 180.0 + lon;
var x = Math.floor( (lon / 360.0) * 131072 );
var zoomLevel = Math.pow(2, zoomLevel);
x = x / zoomLevel;
var lat = lat / 180.0 * 3.1415926;
var y = Math.PI - 0.5 * Math.log((1+Math.sin(lat))/(1-Math.sin(lat)));
y = Math.floor( (y / 2 / Math.PI) * 131072 );
y = y / zoomLevel;
return new Array(x,y);
}
``````
0

Author Commented:
I tried it like you have it also, with many zoom levels  It gives weird answers for each zoom level.

7 = 96.453125,266.046875

1 = 6173,17027 ;  the tiles #'s are way too big for 1.

17 = 0.0941925048828125,0.2598114013671875.

I am trying to convert the code from this snippet at this site:

0

PluritechnicianCommented:
using (from the site) :
var lon = -122; var lat = 37; var zl =3;

I get : [2639, 6377]

It is a wrong result?
0

Author Commented:
How did you run the routine? Web Page?
I have only tried it in Acrobat. Which is where I need it to work.
Thanks.
0

PluritechnicianCommented:
Check here : http://jsfiddle.net/5R6V8/
And check attachment
NOTICE.pdf
0

Author Commented:

using the above web page, the tiles should be 1,3 for coordinates 37,-122 at zl 3.

zl 9 should be 89,205 but comes up 41,99 in fiddle.

Not sure what is off.
0

PluritechnicianCommented:
Check this : http://jsfiddle.net/GPS66/4/

I used zoomLevel = 17-zoomLevel
0

## Featured Post

• 4
• 3
Tackle projects and never again get stuck behind a technical roadblock.