Spaces:
Running
Running
File size: 7,339 Bytes
300e434 55ac8d9 300e434 55ac8d9 300e434 55ac8d9 300e434 55ac8d9 300e434 55ac8d9 300e434 55ac8d9 300e434 55ac8d9 300e434 55ac8d9 300e434 55ac8d9 300e434 55ac8d9 300e434 55ac8d9 300e434 55ac8d9 300e434 55ac8d9 300e434 55ac8d9 300e434 55ac8d9 300e434 55ac8d9 300e434 55ac8d9 300e434 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | ```javascript
// Initialize map centered on UWI St. Augustine
var map = L.map('map').setView([10.6411, -61.3995], 16);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 21,
attribution: '© OpenStreetMap contributors'
}).addTo(map);
// Variables to hold layers
var buildingsLayer, roadsLayer, walkwaysLayer, poisLayer;
var promises = [];
// Also load blocks from backend once
let blockMap = new Map(); // block_id -> block_name
promises.push(
fetch('/blocks').then(r => r.json()).then(rows => {
rows.forEach(b => blockMap.set(b.block_id, b.name));
})
);
// Load Buildings
promises.push(
fetch('data/st_augustine_buildings.geojson')
.then(res => res.json())
.then(data => {
buildingsLayer = L.geoJSON(data, {
style: { color: 'black', fillColor: 'lightgrey', weight: 1 },
onEachFeature: function(feature, layer) {
if (feature.properties && feature.properties.name) {
layer.bindPopup("Building: " + feature.properties.name);
}
}
}).addTo(map);
})
);
// Load Roads
promises.push(
fetch('data/drive_edges.geojson')
.then(res => res.json())
.then(data => {
roadsLayer = L.geoJSON(data, {
style: { color: 'blue', weight: 1 }
}).addTo(map);
})
);
// Load Walkways
promises.push(
fetch('data/walk_edges.geojson')
.then(res => res.json())
.then(data => {
walkwaysLayer = L.geoJSON(data, {
style: { color: 'green', weight: 3, dashArray: '4' }
}).addTo(map);
})
);
// Load POIs
promises.push(
fetch('data/pois.geojson')
.then(res => res.json())
.then(data => {
poisLayer = L.geoJSON(data, {
pointToLayer: (feature, latlng) => {
var marker = L.circleMarker(latlng, {
radius: 5,
fillColor: 'red',
color: 'lightgrey',
weight: 0.5,
fillOpacity: 0.5
});
if (feature.properties && feature.properties.name) {
marker.bindPopup(feature.properties.name);
}
return marker;
}
}).addTo(map);
})
);
// After all layers are loaded
Promise.all(promises).then(() => {
var overlays = {
"Buildings": buildingsLayer,
"Roads": roadsLayer,
"Walkways": walkwaysLayer,
"POIs": poisLayer
};
L.control.layers(null, overlays).addTo(map);
// Search functionality
let highlightedLayers = { from: null, to: null };
function clearHighlight(type) {
if (highlightedLayers[type]) {
buildingsLayer.resetStyle(highlightedLayers[type]);
highlightedLayers[type] = null;
}
}
function normalize(s) { return (s || '').toString().toLowerCase().trim(); }
function findBuildingLayerByName(blockName) {
if (!blockName) return null;
let target = null;
const targetNorm = normalize(blockName);
buildingsLayer.eachLayer(function(layer) {
const n = normalize(layer?.feature?.properties?.name);
if (!n) return;
if (n === targetNorm || n.includes(targetNorm) || targetNorm.includes(n)) {
target = layer;
}
});
return target;
}
function zoomToSelection(fromName, toName) {
const fromLayer = findBuildingLayerByName(fromName);
const toLayer = findBuildingLayerByName(toName);
if (fromLayer && toLayer) {
clearHighlight('from');
clearHighlight('to');
// Highlight from location
highlightedLayers.from = fromLayer;
fromLayer.setStyle({ color: '#ff7800', weight: 3, fillColor: '#ffd27f', fillOpacity: 0.6 });
// Highlight to location
highlightedLayers.to = toLayer;
toLayer.setStyle({ color: '#4CAF50', weight: 3, fillColor: '#8BC34A', fillOpacity: 0.6 });
// Fit both locations in view
const bounds = L.latLngBounds([fromLayer.getBounds(), toLayer.getBounds()]);
map.fitBounds(bounds, { padding: [50, 50] });
// TODO: Add path finding logic here using walkwaysLayer
} else {
alert('Could not find one or both locations');
}
}
// Handle search and suggestions
let activeSearch = null;
const sug = document.getElementById('suggestions');
function renderSuggestions(items, inputId) {
sug.innerHTML = '';
if (!items.length) { sug.style.display = 'none'; return; }
items.forEach(item => {
const div = document.createElement('div');
div.className = 'item';
const blockName = (item.type === 'block') ? item.name : (blockMap.get(item.block_id) || '');
const label = (item.type === 'block') ? item.name : `${item.room_name} — ${blockName}`;
div.textContent = label;
div.dataset.blockName = blockName;
div.onclick = () => {
sug.style.display = 'none';
document.getElementById(inputId).value = label;
const layer = findBuildingLayerByName(blockName);
if (layer) {
map.fitBounds(layer.getBounds(), { padding: [20, 20] });
}
};
sug.appendChild(div);
});
sug.style.display = 'block';
}
// Event listeners for search inputs
document.getElementById('fromSearch').addEventListener('input', function() {
const q = this.value.trim();
if (q.length < 1) { sug.style.display = 'none'; return; }
activeSearch = 'fromSearch';
fetch(`/search?q=${encodeURIComponent(q)}`)
.then(res => res.json())
.then(results => renderSuggestions(results, 'fromSearch'))
.catch(() => { sug.style.display = 'none'; });
});
document.getElementById('toSearch').addEventListener('input', function() {
const q = this.value.trim();
if (q.length < 1) { sug.style.display = 'none'; return; }
activeSearch = 'toSearch';
fetch(`/search?q=${encodeURIComponent(q)}`)
.then(res => res.json())
.then(results => renderSuggestions(results, 'toSearch'))
.catch(() => { sug.style.display = 'none'; });
});
// Find path button
document.getElementById('findPathBtn').addEventListener('click', function() {
const from = document.getElementById('fromSearch').value;
const to = document.getElementById('toSearch').value;
if (!from || !to) {
alert('Please select both starting point and destination');
return;
}
// Extract building names (removing room numbers if present)
const fromBuilding = from.split('—')[0].trim();
const toBuilding = to.split('—')[0].trim();
zoomToSelection(fromBuilding, toBuilding);
});
// Hide suggestions when clicking outside
document.addEventListener('click', (e) => {
if (!document.getElementById('search-container').contains(e.target)) {
sug.style.display = 'none';
}
});
// Handle suggestion clicks
sug.addEventListener('click', function(e) {
if (e.target.classList.contains('item') && e.target.dataset.blockName && activeSearch) {
document.getElementById(activeSearch).value = e.target.textContent;
sug.style.display = 'none';
}
});
});
```
These changes will:
1. Replace the single search box with two search bars for "from" and "to" locations
2. Add labels and a "Find Path" button
3. Style the new elements to match the existing design
4. Update the JavaScript to handle both search inputs and path finding
5. Keep all the existing map functionality while adding the new features |