Solved

Converting Longitude and Latitude to Google Image Tiles Javascript

Posted on 2013-06-19
7
356 Views
Last Modified: 2013-06-20
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
Comment
Question by:ALASKASTREETMASTER
  • 4
  • 3
7 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39261035
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
 

Author Comment

by:ALASKASTREETMASTER
ID: 39261052
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
 
LVL 82

Expert Comment

by:leakim971
ID: 39261064
using (from the site) :
var lon = -122; var lat = 37; var zl =3;

I get : [2639, 6377]

It is a wrong result?
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:ALASKASTREETMASTER
ID: 39261080
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
 
LVL 82

Expert Comment

by:leakim971
ID: 39261101
Check here : http://jsfiddle.net/5R6V8/
And check attachment
NOTICE.pdf
0
 

Author Comment

by:ALASKASTREETMASTER
ID: 39261128
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
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39261269
Check this : http://jsfiddle.net/GPS66/4/

I used zoomLevel = 17-zoomLevel
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question