Implement basic search
This commit is contained in:
parent
255e81ce9f
commit
2e790adc90
@ -17,7 +17,11 @@
|
|||||||
<script src='leafletjs/Leaflet.Editable.js'></script>
|
<script src='leafletjs/Leaflet.Editable.js'></script>
|
||||||
</head>
|
</head>
|
||||||
<body style="position: absolute; padding: 0px; margin: 0px; width:100%; height:100%;">
|
<body style="position: absolute; padding: 0px; margin: 0px; width:100%; height:100%;">
|
||||||
<div id="mapid" style="width: 100%; height: 100%;"></div>
|
<table id="windowtable" style="border-spacing:0;border:0;height:100%;width:100%;"><tr id="windowtr">
|
||||||
|
<td style="padding:0;margin:0">
|
||||||
|
<div id="mapid" style="padding:0;margin:0;width: 100%; height: 100%;"></div>
|
||||||
|
</td>
|
||||||
|
</tr></table>
|
||||||
<style>
|
<style>
|
||||||
.leaflet-tooltip.city-names {
|
.leaflet-tooltip.city-names {
|
||||||
background-color: transparent;
|
background-color: transparent;
|
||||||
|
@ -15,7 +15,7 @@ var streetLabelsRenderer = new L.StreetLabels({
|
|||||||
strokeStyle: "white",
|
strokeStyle: "white",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Projection fix from: https://gis.stackexchange.com/questions/200865/leaflet-crs-simple-custom-scale
|
// Projection fix from: https://gis.stackexchange.com/questions/200865/leaflet-crs-simple-custom-scale
|
||||||
var factorx = 1 / 256 * 4;
|
var factorx = 1 / 256 * 4;
|
||||||
var factory = factorx;
|
var factory = factorx;
|
||||||
@ -200,6 +200,65 @@ function prompt_location() {
|
|||||||
document.location.hash = "#" + str;
|
document.location.hash = "#" + str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var search_element;
|
||||||
|
function toggle_search() {
|
||||||
|
if (el = document.getElementById('searchbar')) {
|
||||||
|
el.parentNode.removeChild(el);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!search_element) {
|
||||||
|
search_element = document.createElement("div");
|
||||||
|
search_element.style.overflow = "scroll";
|
||||||
|
search_element.style.padding = "6px";
|
||||||
|
search_element.style.height = "100%";
|
||||||
|
search_element.innerHTML = '<input style="width:100%;" id="search_query" name="lifo_map_search" type="search" placeholder="Start typing to search..." onkeypress="search(event)"><div id="search_results"></div>' ;
|
||||||
|
}
|
||||||
|
|
||||||
|
td = document.getElementById('windowtr').insertCell(0);
|
||||||
|
td.style.width = "400px";
|
||||||
|
td.style.borderRight = "1px solid black";
|
||||||
|
td.style.verticalAlign = "top";
|
||||||
|
td.id = "searchbar";
|
||||||
|
td.appendChild(search_element);
|
||||||
|
|
||||||
|
document.getElementById('search_query').focus();
|
||||||
|
}
|
||||||
|
|
||||||
|
function htmlEntities(str) {
|
||||||
|
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
||||||
|
}
|
||||||
|
var regex;
|
||||||
|
function search(e) {
|
||||||
|
var query = htmlEntities(document.getElementById("search_query").value);
|
||||||
|
if (query.length > 0 || e.key == "Enter") {
|
||||||
|
results = document.createElement("ul");
|
||||||
|
for (var i = 0; i < layers._layers.length; i++) {
|
||||||
|
if (!layers._layers[i].layer._layers)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
for (key in layers._layers[i].layer._layers) {
|
||||||
|
item = layers._layers[i].layer._layers[key];
|
||||||
|
switch (item.feature.geometry.type) {
|
||||||
|
case "Point":
|
||||||
|
regex = new RegExp(query, 'i');
|
||||||
|
if (item.feature.properties.name.match(regex)) {
|
||||||
|
el = document.createElement("li");
|
||||||
|
el.innerHTML = "[" + layers._layers[i].name + "] " + '<a href="javascript:layers._layers[' + i + '].layer._layers[' + item._leaflet_id + '].fire(\'click\') ">' + item.feature.properties.name + "</a>";
|
||||||
|
results.appendChild(el);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
document.getElementById('search_results').innerHTML = "Search results for " + query;
|
||||||
|
document.getElementById('search_results').appendChild(results);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
L.MyControl = L.Control.extend({
|
L.MyControl = L.Control.extend({
|
||||||
options: {
|
options: {
|
||||||
position: 'topleft',
|
position: 'topleft',
|
||||||
@ -222,6 +281,14 @@ L.MyControl = L.Control.extend({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
L.SearchControl = L.MyControl.extend({
|
||||||
|
options: {
|
||||||
|
position: 'topleft',
|
||||||
|
callback: toggle_search,
|
||||||
|
title: 'Search',
|
||||||
|
html: '🔍'
|
||||||
|
}
|
||||||
|
});
|
||||||
L.GotoControl = L.MyControl.extend({
|
L.GotoControl = L.MyControl.extend({
|
||||||
options: {
|
options: {
|
||||||
position: 'topleft',
|
position: 'topleft',
|
||||||
@ -230,6 +297,7 @@ L.GotoControl = L.MyControl.extend({
|
|||||||
html: '⌖'
|
html: '⌖'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
mymap.addControl(new L.SearchControl());
|
||||||
mymap.addControl(new L.GotoControl());
|
mymap.addControl(new L.GotoControl());
|
||||||
|
|
||||||
var popup = L.popup();
|
var popup = L.popup();
|
||||||
@ -277,5 +345,6 @@ function onHashChange(e) {
|
|||||||
var latlng = L.latLng(parseFloat(coordstr[0]), parseFloat(coordstr[1]));
|
var latlng = L.latLng(parseFloat(coordstr[0]), parseFloat(coordstr[1]));
|
||||||
mymap.setView(latlng, parseInt(coordstr[2]));
|
mymap.setView(latlng, parseInt(coordstr[2]));
|
||||||
}
|
}
|
||||||
|
|
||||||
window.addEventListener("hashchange", onHashChange, false);
|
window.addEventListener("hashchange", onHashChange, false);
|
||||||
onHashChange(null);
|
onHashChange(null);
|
||||||
|
Loading…
Reference in New Issue
Block a user