printmaps/index.html

162 lines
4.6 KiB
HTML
Raw Normal View History

2013-07-27 22:30:25 +00:00
<html>
<head>
<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">
2013-07-28 06:34:38 +00:00
#map {
height: 600px;
width: 80%;
}
#results {
width: 18%;
position: absolute;
top: 0;
right: 0;
}
#results li span {
cursor: pointer;
color: blue;
text-decoration: underline;
}
.active_result {
background-color: #eee;
}
2013-07-27 22:30:25 +00:00
</style>
<script src="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.js"></script>
</head>
<body>
2013-07-28 06:34:38 +00:00
<form method="get" action="#" id="search_form">
2013-07-27 23:40:21 +00:00
<input type="text" id="q" name="q" value="Phoenix" /><input type="submit" />
</form>
<div id="map"></div>
2013-07-28 08:07:10 +00:00
<ul id="results"></ul>
2013-07-27 23:40:21 +00:00
2013-07-27 22:30:25 +00:00
<script type="text/javascript">
2013-07-28 08:07:10 +00:00
var query = "";
var map = L.map('map'); //.setView([51.505, -0.09], 13);
2013-07-27 22:30:25 +00:00
L.tileLayer('http://{s}.tile.cloudmade.com/4be2d4a8b7ae4a5e8ebc0558bb5d7db4/997/256/{z}/{x}/{y}.png', {
2013-07-28 06:34:38 +00:00
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>',
2013-07-27 22:30:25 +00:00
maxZoom: 18
}).addTo(map);
2013-07-27 23:40:21 +00:00
// 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];
}
2013-07-28 06:34:38 +00:00
// decode query param
2013-07-28 08:07:10 +00:00
if(params["q"] && params["q"].length > 0){
handleQuery(decodeURIComponent(params["q"]).replace(/\+/g, ' '));
}
else
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(function(pos){
map.setView([pos.coords.latitude,pos.coords.longitude],10);
// AJAX request for checking where we are
reverseLookup(pos.coords.latitude,pos.coords.longitude,10,function(xmlhttp) {
// Decode response as JSON
response = JSON.parse(xmlhttp.responseText);
2013-07-28 00:22:29 +00:00
2013-07-28 08:07:10 +00:00
if(response.length > 0){
console.log(response);
}
});
},function(error){
console.log(error);
updateMap("Tempe, Arizona",[[33.6569,-111.6801],[33.3133,-112.3276]])
});
}
else {
updateMap("Tempe, Arizona",[[33.6569,-111.6801],[33.3133,-112.3276]])
}
2013-07-28 00:22:29 +00:00
}
2013-07-27 23:40:21 +00:00
2013-07-28 08:07:10 +00:00
function handleQuery(query){
window.query = query;
// AJAX request for geolocation of query
forwardLookup(query,function(xmlhttp) {
// Decode response as JSON
2013-07-27 23:40:21 +00:00
response = JSON.parse(xmlhttp.responseText);
2013-07-28 06:34:38 +00:00
if(response.length > 0){
// Display all results in sidebar
response.forEach(function(result,index){
appendToResults(index,result.display_name);
});
2013-07-27 23:40:21 +00:00
2013-07-28 06:34:38 +00:00
// Fit map to bounds of first result
showItem(0);
}
else {
2013-07-28 08:07:10 +00:00
appendToResults("No Results");
2013-07-28 06:34:38 +00:00
}
2013-07-28 08:07:10 +00:00
});
}
function forwardLookup(query,callback){
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","http://nominatim.openstreetmap.org/search/"+encodeURIComponent(query)+"?format=json",true);
xmlhttp.send();
2013-07-28 08:15:53 +00:00
xmlhttp.onreadystatechange = function(xmlhttp){
2013-07-28 08:07:10 +00:00
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
callback(xmlhttp);
}
};
}
function reverseLookup(lat,long,zoom,callback){
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","http://nominatim.openstreetmap.org/reverse?format=json&lat="+encodeURIComponent(lat)+"&lon="+encodeURIComponent(lat)+"&zoom="+encodeURIComponent(zoom),true);
xmlhttp.send();
2013-07-28 08:15:53 +00:00
xmlhttp.onreadystatechange = function(xmlhttp){
2013-07-28 08:07:10 +00:00
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
callback();
}
};
}
2013-07-28 06:34:38 +00:00
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";
2013-07-28 08:07:10 +00:00
// Update the map
updateMap(query,[[response[index].boundingbox[0], response[index].boundingbox[2]],
[response[index].boundingbox[1], response[index].boundingbox[3]]]);
2013-07-28 06:34:38 +00:00
}
2013-07-28 08:07:10 +00:00
function updateMap(query,bounds){
// Set search textbox to query
document.getElementById("q").value = query;
// Fit map to the GPS boundaries
map.fitBounds(bounds);
};
2013-07-27 22:30:25 +00:00
</script>
2013-07-27 23:40:21 +00:00
2013-07-27 22:30:25 +00:00
</body>
</html>