Last active
December 21, 2015 07:38
-
-
Save blech/6272393 to your computer and use it in GitHub Desktop.
Find My Tiles: lat/long to zxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Find My Tiles</title> | |
<link rel="stylesheet" href="http://husk.org/collected.css"> | |
</head> | |
<body> | |
<h1>Find My Tiles</h1> | |
<p id="latitude">Finding your lat/long…</p> | |
<ul id="zooms"> | |
</ul> | |
<script> | |
function sec(aValue) { | |
return 1/Math.cos(aValue); | |
} | |
Math.constructor.prototype.sec = sec; | |
function getLocation() { | |
if (location.hash) { | |
coords = location.hash.split("/") | |
lat = parseFloat(coords[0].substr(1)) | |
lng = parseFloat(coords[1]) | |
position = {'coords': {'latitude': lat, 'longitude': lng}} | |
showTiles(position) | |
} | |
else if (navigator.geolocation) { | |
navigator.geolocation.getCurrentPosition(showTiles); | |
} else { | |
alert("Your browser doesn't do geolocation, sorry."); | |
} | |
} | |
function showTiles(position) { | |
var zooms = document.getElementById('zooms'), | |
latitude = document.getElementById('latitude'); | |
longitude = document.getElementById('longitude'); | |
lat_rad = position.coords.latitude * Math.PI/180 | |
// http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#lon.2Flat_to_tile_numbers_2 | |
for (var z=0; z<=21; z++) { | |
n = Math.pow(2, z) | |
x = Math.floor(n * ((position.coords.longitude + 180) / 360)) | |
y = Math.floor(n * (1-(Math.log(Math.tan(lat_rad) + Math.sec(lat_rad)) / Math.PI))/2) | |
var el = document.createElement('li') | |
txt = document.createTextNode("ZXY: "+z+"/"+x+"/"+y) | |
el.appendChild(txt) | |
zooms.appendChild(el) | |
} | |
var txt = document.createTextNode('Your lat/long is ' + position.coords.latitude.toFixed(1) + '°, '+position.coords.longitude.toFixed(1)+ '°'); | |
latitude.replaceChild(txt, latitude.firstChild); | |
} | |
getLocation() | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment