[Webinar] Streamline your web hosting managementRegister Today

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

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
ALASKASTREETMASTER
Asked:
ALASKASTREETMASTER
  • 4
  • 3
1 Solution
 
leakim971PluritechnicianCommented:
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); 
}

Open in new window

0
 
ALASKASTREETMASTERAuthor 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:

http://blogs.infoecho.net/echo/2007/02/05/code-snippet-for-converting-longitude-and-latitude-to-google-image-tiles-uri/
0
 
leakim971PluritechnicianCommented:
using (from the site) :
var lon = -122; var lat = 37; var zl =3;

I get : [2639, 6377]

It is a wrong result?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ALASKASTREETMASTERAuthor 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
 
leakim971PluritechnicianCommented:
Check here : http://jsfiddle.net/5R6V8/
And check attachment
NOTICE.pdf
0
 
ALASKASTREETMASTERAuthor Commented:
http://www.maptiler.org/google-maps-coordinates-tile-bounds-projection/

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
 
leakim971PluritechnicianCommented:
Check this : http://jsfiddle.net/GPS66/4/

I used zoomLevel = 17-zoomLevel
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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