printmaps/index.html

236 lines
7.1 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Printable High-Resolution Maps!</title>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.css" />
<!--[if lte IE 8]>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.ie.css" />
<![endif]-->
<style type="text/css">
#navigation {
position: absolute;
top: 0;
right: 0;
z-index: 9999;
background-color: white;
font-family: Arial, Helvetica, sans-serif;
padding: 1em;
width: 15em;
height: 612px;
overflow-y: auto;
opacity: 0.8;
}
#printbutton {
font-size: 1.5em;
font-weight: bold;
}
#map {
padding: 1em;
border: 1px solid black;
height: 612px; /* 8.5 inch at 72 dpi */
width: 792px; /* 11 inch at 72 dpi */
}
#results {
margin: 0 0 0 1em;
padding: 0;
}
#results li span {
cursor: pointer;
color: blue;
text-decoration: underline;
}
.active_result {
background-color: #eee;
}
</style>
<script src="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.js"></script>
</head>
<body>
<div id="navigation">
<form method="get" action="#" id="search_form">
<input type="text" id="q" name="q" value="" /><input type="submit" value="Search" />
</form>
<input type="button" id="printbutton" value="Print" onclick="printMap()" />
<h2>Printable High-Resolution Maps!</h2>
<ol>
<li>Search/center/zoom this map until it covers the desired area.</li>
<li>Click the Print button above.</li>
<li>A new window will open with a high-resolution map!</li>
</ol>
<ul id="results"></ul>
</div>
<div id="map"></div>
<script type="text/javascript">
var query = "";
var map = L.map('map').setView([51.505, -0.09], 2);
L.tileLayer('http://{s}.tile.cloudmade.com/4be2d4a8b7ae4a5e8ebc0558bb5d7db4/997/256/{z}/{x}/{y}.png', {
attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>; Imagery &copy; <a href="http://cloudmade.com">CloudMade</a>',
maxZoom: 16
}).addTo(map);
// Get url params
var prmstr = window.location.search.substr(1);
var prmarr = prmstr.split ("&");
var params = {};
for ( var i = 0; i < prmarr.length; i++) {
var tmparr = prmarr[i].split("=");
params[tmparr[0]] = tmparr[1];
}
// decode query param
if(params["q"] && params["q"].length > 0){
handleQuery(decodeURIComponent(params["q"]).replace(/\+/g, ' '));
}
else {
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(function(pos){
// Move the map over
map.setView([pos.coords.latitude,pos.coords.longitude],12);
// AJAX request for checking where we are
reverseLookup(pos.coords.latitude,pos.coords.longitude,18,function(xmlhttp) {
// Decode response as JSON
response = JSON.parse(xmlhttp.responseText);
if(response.address){
console.log(response.address);
var loc = "";
// Set search textbox to where we think we are
if(response.address.suburb){
loc += response.address.suburb+", ";
}
if(response.address.city){
loc += response.address.city+", ";
}
if(response.address.state){
loc += response.address.state+", ";
}
if(response.address.country){
loc += response.address.country+", ";
}
document.getElementById("q").value = loc;
}
else{
console.log("No reverse-lookup json");
}
});
},function(error){
console.log(error);
updateMap("Tempe, Arizona",[[33.6569,-111.6801],[33.3133,-112.3276]])
window.query = "Tempe, Arizona";
});
}
else {
updateMap("Tempe, Arizona",[[33.6569,-111.6801],[33.3133,-112.3276]])
window.query = "Tempe, Arizona";
}
}
function handleQuery(query){
window.query = query;
// AJAX request for geolocation of query
forwardLookup(query,function(xmlhttp) {
// Decode response as JSON
response = JSON.parse(xmlhttp.responseText);
if(response.length > 0){
// Display all results in sidebar
response.forEach(function(result,index){
appendToResults(index,result.display_name);
});
// Fit map to bounds of first result
showItem(0);
}
else {
appendToResults("No Results");
}
});
}
function forwardLookup(query,callback){
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","http://nominatim.openstreetmap.org/search/"+encodeURIComponent(query)+"?format=json",true);
xmlhttp.send();
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
callback(xmlhttp);
}
};
}
function reverseLookup(lat,lon,zoom,callback){
xmlhttp=new XMLHttpRequest();
request = "http://nominatim.openstreetmap.org/reverse?format=json&lat="+encodeURIComponent(lat)+"&lon="+encodeURIComponent(lon)+"&zoom="+encodeURIComponent(zoom);
xmlhttp.open("GET",request,true);
xmlhttp.send();
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
callback(xmlhttp);
}
};
}
function appendToResults(index,name){
var item = document.createElement("li");
item.innerHTML = "<span onclick=\"showItem('"+index+"');\">"+name+'</span>';
item.id = "result_"+index;
document.getElementById("results").appendChild(item);
}
function showItem(index){
// Change this item to be the active one
var results = document.getElementsByClassName("active_result")
for(var i=0; i < results.length; i++){
console.log(i);
results[i].className = "";
}
document.getElementById("result_"+index).className = "active_result";
// Update the map
updateMap(query,[[response[index].boundingbox[0], response[index].boundingbox[2]],
[response[index].boundingbox[1], response[index].boundingbox[3]]]);
}
function updateMap(query,bounds){
// Set search textbox to query
document.getElementById("q").value = query;
// Fit map to the GPS boundaries
map.fitBounds(bounds);
}
function printMap(){
var bounds = map.getBounds();
newwindow=window.open("print.html?q="+query
+"&b0="+bounds._northEast.lat
+"&b1="+bounds._northEast.lng
+"&b2="+bounds._southWest.lat
+"&b3="+bounds._southWest.lng
,'name');
if (window.focus) {newwindow.focus()}
return false;
}
</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-42790423-1', 'zyphlar.github.io');
ga('send', 'pageview');
</script>
</body>
</html>