Solved

Converting Longitude and Latitude to Google Image Tiles Javascript

Posted on 2013-06-19
7
359 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

685 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