Skip to content

Commit

Permalink
Show search results on map (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddewie authored Jul 21, 2024
1 parent 6fd6f27 commit 08097ed
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 13 deletions.
3 changes: 0 additions & 3 deletions proxy/css/ui.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ body {
#search-results, #search-milestones-form {
display: none;
}
#search-results .no-results {
text-align: center;
}

#legend {
position: absolute;
Expand Down
2 changes: 1 addition & 1 deletion proxy/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ <h5 class="modal-title">Search</h5>
</div>
</div>
</form>
<div class="list-group" id="search-results"></div>
<div id="search-results"></div>
</div>
</div>
</div>
Expand Down
24 changes: 24 additions & 0 deletions proxy/js/styles.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,13 @@ const sources = {
tileSize: 256,
attribution: '<a href="https://www.openstreetmap.org/about" target="_blank">&copy; OpenStreetMap contributors</a>'
},
search: {
type: 'geojson',
data: {
type: 'FeatureCollection',
features: [],
},
},
openrailwaymap_low: {
type: 'vector',
url: `${origin}/railway_line_low`,
Expand Down Expand Up @@ -954,6 +961,18 @@ const backgroundMap = {
}
};

const searchResults = {
id: 'search',
type: 'circle',
source: 'search',
paint: {
'circle-radius': 8,
'circle-color': 'rgba(183, 255, 0, 0.7)',
'circle-stroke-width': 2,
'circle-stroke-color': 'black',
}
};

// TODO remove all [switch, [zoom]] to ensure legend displays only visible features
const layers = {
standard: [
Expand Down Expand Up @@ -1838,6 +1857,7 @@ const layers = {
'text-padding': 10,
},
},
searchResults,
],

speed: [
Expand Down Expand Up @@ -2011,6 +2031,7 @@ const layers = {
'symbol-spacing': 100,
},
},
searchResults,
],

signals: [
Expand Down Expand Up @@ -2303,6 +2324,7 @@ const layers = {
],
}
},
searchResults,
],

electrification: [
Expand Down Expand Up @@ -2562,6 +2584,7 @@ const layers = {
'symbol-spacing': 100,
},
},
searchResults,
],

gauge: [
Expand Down Expand Up @@ -2839,6 +2862,7 @@ const layers = {
'symbol-spacing': 100,
},
},
searchResults,
],
};

Expand Down
67 changes: 58 additions & 9 deletions proxy/js/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ const backgroundRasterUrlControl = document.getElementById('backgroundRasterUrl'
const legend = document.getElementById('legend')
const legendMapContainer = document.getElementById('legend-map')

function registerLastSearchResults(results) {
const data = {
type: 'FeatureCollection',
features: results.map(result => ({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [result.latitude, result.longitude],
},
})),
};
map.getSource('search').setData(data);
}

function facilitySearchQuery(type, term) {
const encoded = encodeURIComponent(term)

Expand Down Expand Up @@ -67,20 +81,48 @@ function searchForMilestones(ref, position) {
}

function showSearchResults(results, renderItem) {
let content = '';
if (results.length === 0) {
content += `<div class="list-group-item no-results"><i>No results</i></div>`
} else {
results.forEach(result => {
content += `<a class="list-group-item list-group-item-action" href="javascript:hideSearchResults(); map.easeTo({center: [${result.latitude}, ${result.longitude}], zoom: 15}); hideSearch()">${renderItem(result)}</a>`
})
}
searchResults.innerHTML = content;
registerLastSearchResults(results);

const bounds = results.length > 0
? JSON.stringify(results.reduce(
(bounds, result) =>
bounds.extend({lat: result.longitude, lon: result.latitude}),
new maplibregl.LngLatBounds({lat: results[0].longitude, lon: results[0].latitude})
).toArray())
: null;

searchResults.innerHTML = results.length === 0
? `
<div class="mb-1 d-flex align-items-center">
<span class="flex-grow-1">
<span class="badge badge-light">0 results</span>
</span>
</div>
`
: `
<div class="mb-1 d-flex align-items-center">
<span class="flex-grow-1">
<span class="badge badge-light">${results.length} results</span>
</span>
<button class="btn btn-sm btn-primary" type="button" style="vertical-align: text-bottom" onclick="viewSearchResultsOnMap(${bounds})">
<svg width="auto" height="16" viewBox="-4 0 36 36" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd"><path d="M14 0c7.732 0 14 5.641 14 12.6C28 23.963 14 36 14 36S0 24.064 0 12.6C0 5.641 6.268 0 14 0Z" fill="white"/><circle fill="var(--primary)" fill-rule="nonzero" cx="14" cy="14" r="7"/></g></svg>
Show on map
</button>
</div>
<div class="list-group">
${results.map(result =>
`<a class="list-group-item list-group-item-action" href="javascript:hideSearchResults(); map.easeTo({center: [${result.latitude}, ${result.longitude}], zoom: 15}); hideSearch()">
${renderItem(result)}
</a>`
).join('')}
</div>
`;
searchResults.style.display = 'block';
}

function hideSearchResults() {
searchResults.style.display = 'none';
registerLastSearchResults([]);
}

function showSearch() {
Expand Down Expand Up @@ -118,6 +160,13 @@ function searchMilestones() {
hideSearchResults();
}

function viewSearchResultsOnMap(bounds) {
hideSearch();
map.fitBounds(bounds, {
padding: 40,
});
}

function showConfiguration() {
backgroundSaturationControl.value = configuration.backgroundSaturation ?? defaultConfiguration.backgroundSaturation;
backgroundOpacityControl.value = configuration.backgroundOpacity ?? defaultConfiguration.backgroundOpacity;
Expand Down

0 comments on commit 08097ed

Please sign in to comment.