|
5 | 5 | <meta name="author" content="Klokan Technologies GmbH" />
|
6 | 6 | <script src="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.17.1/ol.js"></script>
|
7 | 7 | <link href="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.17.1/ol.css" rel="stylesheet" />
|
| 8 | + <script src="https://klokantech.github.io/javascript/kt.js"></script> |
| 9 | + <link href="https://klokantech.github.io/styles/base.css" rel="stylesheet" /> |
8 | 10 | <style>
|
9 | 11 | body {font-family:Arial;}
|
10 |
| - #map {position:absolute;top:100px;left:400px;right:0;bottom:0;} |
11 |
| - #results {position:absolute;top:100px;left:0;width:400px;bottom:400px;overflow:auto;} |
12 |
| - #results div {margin:3px;padding:3px;background:#eee;border:1px solid #ccc;} |
13 |
| - #results div:hover {background:#ddd;} |
14 |
| - #details {position:absolute;left:0;bottom:0;width:400px;height:400px;overflow:auto;font-size:.8em;margin:0;} |
| 12 | + #map {position:absolute;top:0;left:0;right:0;bottom:0;} |
| 13 | + #details {position:absolute;left:0;bottom:0;font-size:.7em;background:rgba(255,255,255,.6);} |
| 14 | + input {display:inline-block;width:400px;position:absolute;top:10px;left:50%;margin-left:-200px;} |
15 | 15 | </style>
|
16 | 16 | <script>
|
17 |
| - var input, resultsEl, detailsEl; |
| 17 | + var detailsEl; |
18 | 18 | var map;
|
19 | 19 | var feature = new ol.Feature({
|
20 | 20 | geometry: new ol.geom.Polygon([])
|
21 | 21 | });
|
22 | 22 | var init = function() {
|
23 |
| - input = document.getElementById('search'); |
24 |
| - resultsEl = document.getElementById('results'); |
| 23 | + var autocomplete = new kt.OsmNamesAutocomplete('search', '/'); |
| 24 | + |
| 25 | + autocomplete.registerCallback(function(item) { |
| 26 | + var isDegenerate = (item.boundingbox[2] - item.boundingbox[0]) * (item.boundingbox[3] - item.boundingbox[1]) <= 0; |
| 27 | + var extent = ol.extent.applyTransform(item.boundingbox, ol.proj.getTransform('EPSG:4326', 'EPSG:3857')); |
| 28 | + |
| 29 | + map.getView().fit(extent, map.getSize(), { |
| 30 | + padding: [250, 50, 50, 50] |
| 31 | + }); |
| 32 | + if (isDegenerate) { |
| 33 | + map.getView().setZoom(20); |
| 34 | + } |
| 35 | + var coords = [[extent[0], extent[1]], [extent[0], extent[3]], [extent[2], extent[3]], [extent[2], extent[1]], [extent[0], extent[1]]]; |
| 36 | + feature.getGeometry().setCoordinates([coords], 'XY'); |
| 37 | + detailsEl.innerHTML = JSON.stringify(item, undefined, 2); |
| 38 | + }, true); |
| 39 | + |
25 | 40 | detailsEl = document.getElementById('details');
|
26 |
| - input.oninput = update; |
27 | 41 |
|
28 | 42 | var source = new ol.source.Vector({
|
29 | 43 | features: [feature]
|
|
48 | 62 | zoom: 2
|
49 | 63 | })
|
50 | 64 | });
|
51 |
| - if (input.value.length > 0) { |
52 |
| - if (typeof input.selectionStart == "number") { |
53 |
| - input.selectionStart = input.selectionEnd = input.value.length; |
54 |
| - } else { |
55 |
| - input.focus(); |
56 |
| - input.setSelectionRange(input.value.length, input.value.length); |
57 |
| - } |
58 |
| - update(); |
59 |
| - } |
60 |
| - }; |
61 |
| - |
62 |
| - var results = []; |
63 |
| - var query = ''; |
64 |
| - var queryTimeoutId = null; |
65 |
| - var activateTimeoutId = null; |
66 |
| - var update = function() { |
67 |
| - query = input.value; |
68 |
| - if (queryTimeoutId) { |
69 |
| - clearTimeout(queryTimeoutId); |
70 |
| - } |
71 |
| - if (query.length < 1) { |
72 |
| - return; |
73 |
| - } |
74 |
| - queryTimeoutId = setTimeout(function() { |
75 |
| - queryTimeoutId = null; |
76 |
| - results = []; |
77 |
| - |
78 |
| - var usedQuery = query; |
79 |
| - |
80 |
| - resultsEl.innerHTML = 'Loading...'; |
81 |
| - detailsEl.innerHTML = ''; |
82 |
| - feature.getGeometry().setCoordinates([], 'XY'); |
83 |
| - |
84 |
| - var xhr = new XMLHttpRequest(); |
85 |
| - xhr.responseType = 'json'; |
86 |
| - xhr.onreadystatechange = function(e) { |
87 |
| - if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) { |
88 |
| - if (usedQuery !== query && console && console.log) { |
89 |
| - console.log('Ignoreing late result for:', usedQuery); |
90 |
| - return; |
91 |
| - } |
92 |
| - results = e.target.response; |
93 |
| - var html = ''; |
94 |
| - |
95 |
| - if (results.count == 0) { |
96 |
| - html = 'No results found'; |
97 |
| - } else { |
98 |
| - results.results.forEach(function(item, i) { |
99 |
| - var isDegenerate = (item.boundingbox[2] - item.boundingbox[0]) * (item.boundingbox[3] - item.boundingbox[1]) <= 0; |
100 |
| - var type = item.type; |
101 |
| - if (item.class == 'street') { |
102 |
| - type = item.class; |
103 |
| - } |
104 |
| - html += '<div onmouseover="activate(' + i + ')">' + item.display_name + ' <span class="type">[' + type + (isDegenerate ? '] [<b>empty</b>' : '') + ']</span></div>'; |
105 |
| - }); |
106 |
| - } |
107 |
| - if (activateTimeoutId) { |
108 |
| - clearInterval(activateTimeoutId); |
109 |
| - activateTimeoutId = null; |
110 |
| - } |
111 |
| - activateTimeoutId = setTimeout(function(){activate(results.count ? 0 : null);}, 50); |
112 |
| - |
113 |
| - resultsEl.innerHTML = html; |
114 |
| - } |
115 |
| - }; |
116 |
| - xhr.open('GET', '/?q=' + encodeURIComponent(usedQuery) + '&format=json&autocomplete=1', true); |
117 |
| - xhr.send(); |
118 |
| - }, 500); |
119 |
| - }; |
120 |
| - |
121 |
| - var activate = function(i) { |
122 |
| - var item = results.results[i]; |
123 |
| - if (console && console.log) { |
124 |
| - console.log('Bounding box: ', item.boundingbox); |
125 |
| - } |
126 |
| - |
127 |
| - var isDegenerate = (item.boundingbox[2] - item.boundingbox[0]) * (item.boundingbox[3] - item.boundingbox[1]) <= 0; |
128 |
| - var extent = ol.extent.applyTransform(item.boundingbox, ol.proj.getTransform('EPSG:4326', 'EPSG:3857')); |
129 |
| - map.getView().fit(extent, map.getSize()); |
130 |
| - if (isDegenerate) { |
131 |
| - map.getView().setZoom(20); |
132 |
| - } |
133 |
| - var coords = [[extent[0], extent[1]], [extent[0], extent[3]], [extent[2], extent[3]], [extent[2], extent[1]], [extent[0], extent[1]]]; |
134 |
| - feature.getGeometry().setCoordinates([coords], 'XY'); |
135 |
| - detailsEl.innerHTML = JSON.stringify(item, undefined, 2); |
136 | 65 | };
|
137 | 66 | </script>
|
138 | 67 | </head>
|
139 | 68 | <body onload="init();">
|
140 |
| - <h1>OSM Names full text search</h1> |
141 |
| - <form action="/" method="get"> |
142 |
| - <input autocomplete="off" id="search" type="text" name="q" autofocus="autofocus" value="{{query}}" /> |
143 |
| - <input type="hidden" name="format" value="html" /> |
144 |
| - <input class="btn" type="submit" onclick="update();return false;" value="Search" /> |
145 |
| - <input class="btn" type="submit" value="Show debug results" /> |
146 |
| - </form> |
147 |
| - |
148 |
| - <div id="results"> |
149 |
| - Type in the search box above to see results here. |
150 |
| - </div> |
151 |
| - <pre id="details"></pre> |
152 | 69 | <div id="map"></div>
|
| 70 | + <pre id="details"></pre> |
| 71 | + <form><input autocomplete="off" id="search" type="text" autofocus="autofocus" placeholder="Type here to search.." /></form> |
153 | 72 | </body>
|
154 | 73 | </html>
|
0 commit comments