Skip to content

Commit 9cb146a

Browse files
authored
Search control - use default handler (#165)
* Add search control to several experiments * Change screenreader/restaurants experiment to use default search handler
1 parent 9c98155 commit 9cb146a

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

screenreader/restaurants/index.html

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
<map-layer label="CBMT" src="/experiments/refactoring-temporary/cbmtile-cbmt.mapml" checked hidden></map-layer>-->
1414
<mapml-viewer projection="OSMTILE" zoom="14" lat="45.406314" lon="-75.6883335" controls controlslist="geolocation search">
1515
<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 -->
1618
<map-layer label="OpenStreetMap" src="../../refactoring-temporary/osm.mapml" checked hidden></map-layer>
1719
<map-layer id="restaurant" label="Restaurants" checked>
1820
<map-meta name="cs" content="gcrs" ></map-meta>
@@ -31,5 +33,74 @@
3133
</map-extent>
3234
</map-layer>
3335
</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> -->
34105
</body>
35106
</html>

0 commit comments

Comments
 (0)