|
13 | 13 | <map-layer label="CBMT" src="/experiments/refactoring-temporary/cbmtile-cbmt.mapml" checked hidden></map-layer>--> |
14 | 14 | <mapml-viewer projection="OSMTILE" zoom="14" lat="45.406314" lon="-75.6883335" controls controlslist="geolocation search"> |
15 | 15 | <map-caption>Ottawa Restaurants Map</map-caption> |
| 16 | + <!-- the osm.mapml includes search and suggestions links to photon, |
| 17 | + and this page relies on the default search/suggestions handler --> |
16 | 18 | <map-layer label="OpenStreetMap" src="../../refactoring-temporary/osm.mapml" checked hidden></map-layer> |
17 | 19 | <map-layer id="restaurant" label="Restaurants" checked> |
18 | 20 | <map-meta name="cs" content="gcrs" ></map-meta> |
|
31 | 33 | </map-extent> |
32 | 34 | </map-layer> |
33 | 35 | </mapml-viewer> |
| 36 | +<!-- <script> |
| 37 | + const map = document.getElementById('map'); |
| 38 | +
|
| 39 | + // Keep a reference to the results layer so we can replace it on each search |
| 40 | + let resultsLayer = null; |
| 41 | +
|
| 42 | + map.addEventListener('mapsearch', (e) => { |
| 43 | + e.preventDefault(); // suppress the default results dropdown |
| 44 | +
|
| 45 | + // Clear the suggestions list |
| 46 | + const results = map._map._container.querySelector('.mapml-search-results'); |
| 47 | + if (results) results.innerHTML = ''; |
| 48 | +
|
| 49 | + // Remove the previous results layer |
| 50 | + if (resultsLayer) { |
| 51 | + resultsLayer.remove(); |
| 52 | + resultsLayer = null; |
| 53 | + } |
| 54 | +
|
| 55 | + // Merge GeoJSON features from all responding layers into one collection |
| 56 | + const features = []; |
| 57 | + for (const { data } of e.detail.responses) { |
| 58 | + if (data?.features) features.push(...data.features); |
| 59 | + } |
| 60 | + if (features.length === 0) return; |
| 61 | +
|
| 62 | + const fc = { type: 'FeatureCollection', features }; |
| 63 | +
|
| 64 | + // geojson2mapml converts to <map-feature> elements and adds the layer |
| 65 | + resultsLayer = map.geojson2mapml(fc, { |
| 66 | + label: 'Search Results', |
| 67 | + projection: 'OSMTILE', |
| 68 | + caption: (feature) => { |
| 69 | + const p = feature.properties || {}; |
| 70 | + const parts = [p.name]; |
| 71 | + for (const k of ['city', 'county', 'state', 'country']) { |
| 72 | + if (p[k] && p[k] !== p.name) parts.push(p[k]); |
| 73 | + } |
| 74 | + return parts.filter(Boolean).join(', ') || 'Unnamed'; |
| 75 | + }, |
| 76 | + properties: (feature) => { |
| 77 | + const p = feature.properties || {}; |
| 78 | + const dl = document.createElement('dl'); |
| 79 | + if (p.name) { |
| 80 | + dl.innerHTML += `<dt>Name</dt><dd>${p.name}</dd>`; |
| 81 | + } |
| 82 | + if (p.osm_key) { |
| 83 | + dl.innerHTML += `<dt>Type</dt><dd>${p.osm_key}${p.osm_value ? ': ' + p.osm_value : ''}</dd>`; |
| 84 | + } |
| 85 | + if (p.city) dl.innerHTML += `<dt>City</dt><dd>${p.city}</dd>`; |
| 86 | + if (p.state) dl.innerHTML += `<dt>State</dt><dd>${p.state}</dd>`; |
| 87 | + if (p.country) dl.innerHTML += `<dt>Country</dt><dd>${p.country}</dd>`; |
| 88 | + return dl; |
| 89 | + } |
| 90 | + }); |
| 91 | +
|
| 92 | + // Navigate to the first result |
| 93 | + const first = features[0]; |
| 94 | + const bbox = first.bbox |
| 95 | + || (first.properties?.extent?.length === 4 ? first.properties.extent : null); |
| 96 | + if (bbox) { |
| 97 | + const [west, south, east, north] = bbox; |
| 98 | + map.zoomTo(south, west, north - south, east - west); |
| 99 | + } else if (first.geometry?.coordinates) { |
| 100 | + const [lon, lat] = first.geometry.coordinates; |
| 101 | + map.zoomTo(lat, lon, 0, 0); |
| 102 | + } |
| 103 | + }); |
| 104 | +</script> --> |
34 | 105 | </body> |
35 | 106 | </html> |
0 commit comments