feat: migrate mini-map and home map to MapLibre GL
This commit is contained in:
@@ -34,47 +34,46 @@
|
||||
<a class="feed-map-link" href="{{ page.parent().url }}/map">View full map →</a>
|
||||
</div>
|
||||
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.min.css">
|
||||
<script src="https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.min.js"></script>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.css">
|
||||
<script src="https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.js"></script>
|
||||
<script src="{{ url('theme://js/maplibre-utils.js') }}"></script>
|
||||
<script>
|
||||
var FEED_ENTRIES = {{ map_entries|json_encode|raw }};
|
||||
|
||||
var map = L.map('feed-map', { minZoom: 2, maxZoom: 18, zoomControl: true });
|
||||
L.tileLayer('https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png', {
|
||||
maxZoom: 20,
|
||||
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>'
|
||||
}).addTo(map);
|
||||
|
||||
var latLngs = FEED_ENTRIES.map(function(e) { return [parseFloat(e.lat), parseFloat(e.lng)]; });
|
||||
|
||||
if (latLngs.length > 1) {
|
||||
L.polyline(latLngs, { color: '#1F6B5A', weight: 3, opacity: 0.7 }).addTo(map);
|
||||
}
|
||||
|
||||
FEED_ENTRIES.forEach(function(entry, i) {
|
||||
var isLatest = (i === FEED_ENTRIES.length - 1);
|
||||
var size = isLatest ? 16 : 10;
|
||||
var color = isLatest ? '#155244' : '#1F6B5A';
|
||||
var icon = L.divIcon({
|
||||
className: '',
|
||||
html: '<div style="width:' + size + 'px;height:' + size + 'px;background:' + color + ';border:2px solid #fff;border-radius:50%;box-shadow:0 1px 3px rgba(0,0,0,0.35);cursor:pointer;"></div>',
|
||||
iconSize: [size, size],
|
||||
iconAnchor: [size/2, size/2]
|
||||
});
|
||||
L.marker([parseFloat(entry.lat), parseFloat(entry.lng)], { icon: icon })
|
||||
.addTo(map)
|
||||
.on('click', function() {
|
||||
var card = document.getElementById('entry-' + entry.slug);
|
||||
if (card) { card.scrollIntoView({ behavior: 'smooth', block: 'center' }); }
|
||||
});
|
||||
var feedMap = new maplibregl.Map({
|
||||
container: 'feed-map',
|
||||
style: MapUtils.MAP_STYLE,
|
||||
center: [20, 20],
|
||||
zoom: 2,
|
||||
cooperativeGestures: true
|
||||
});
|
||||
|
||||
if (latLngs.length === 1) {
|
||||
map.setView(latLngs[0], 10);
|
||||
} else {
|
||||
map.fitBounds(L.latLngBounds(latLngs), { padding: [20, 20] });
|
||||
}
|
||||
setTimeout(function() { map.invalidateSize(); }, 100);
|
||||
feedMap.on('load', function () {
|
||||
var bounds = new maplibregl.LngLatBounds();
|
||||
var coords = [];
|
||||
|
||||
FEED_ENTRIES.forEach(function (entry, i) {
|
||||
var isLatest = (i === FEED_ENTRIES.length - 1);
|
||||
var lngLat = [parseFloat(entry.lng), parseFloat(entry.lat)];
|
||||
coords.push(lngLat);
|
||||
bounds.extend(lngLat);
|
||||
|
||||
var el = MapUtils.createDotMarker(isLatest);
|
||||
el.addEventListener('click', function () {
|
||||
window.location.href = entry.url;
|
||||
});
|
||||
|
||||
new maplibregl.Marker({ element: el }).setLngLat(lngLat).addTo(feedMap);
|
||||
});
|
||||
|
||||
MapUtils.addJourneyLine(feedMap, coords, 'feed-journey');
|
||||
|
||||
if (FEED_ENTRIES.length === 1) {
|
||||
feedMap.jumpTo({ center: coords[0], zoom: 10 });
|
||||
} else {
|
||||
feedMap.fitBounds(bounds, { padding: 20, maxZoom: 11 });
|
||||
}
|
||||
});
|
||||
</script>
|
||||
{% endif %}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user