|
|
|
|
|
|
|
|
|
|
|
|
|
let map = null; |
|
|
|
|
|
function initMap() { |
|
|
|
if (map !== null) { |
|
map.remove(); |
|
} |
|
|
|
|
|
map = L.map('map').setView([40.7, -74.0], 10); |
|
|
|
|
|
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { |
|
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors', |
|
maxZoom: 19 |
|
}).addTo(map); |
|
|
|
|
|
L.control.scale().addTo(map); |
|
|
|
return map; |
|
} |
|
|
|
|
|
function displayGeoJSON(geojsonData) { |
|
if (!map) { |
|
initMap(); |
|
} |
|
|
|
|
|
map.eachLayer(function(layer) { |
|
if (layer instanceof L.GeoJSON) { |
|
map.removeLayer(layer); |
|
} |
|
}); |
|
|
|
|
|
const geojsonLayer = L.geoJSON(geojsonData, { |
|
style: function(feature) { |
|
|
|
return { |
|
fillColor: getRandomColor(), |
|
weight: 2, |
|
opacity: 1, |
|
color: '#666', |
|
fillOpacity: 0.7 |
|
}; |
|
}, |
|
pointToLayer: function(feature, latlng) { |
|
|
|
return L.circleMarker(latlng, { |
|
radius: 8, |
|
fillColor: getRandomColor(), |
|
color: "#000", |
|
weight: 1, |
|
opacity: 1, |
|
fillOpacity: 0.8 |
|
}); |
|
}, |
|
onEachFeature: function(feature, layer) { |
|
|
|
if (feature.properties) { |
|
let popupContent = '<div class="feature-popup">'; |
|
|
|
popupContent += '<h5>Feature Properties</h5>'; |
|
|
|
for (const [key, value] of Object.entries(feature.properties)) { |
|
popupContent += `<strong>${key}:</strong> ${value}<br>`; |
|
} |
|
|
|
popupContent += '</div>'; |
|
|
|
layer.bindPopup(popupContent); |
|
} |
|
} |
|
}).addTo(map); |
|
|
|
|
|
if (geojsonLayer.getBounds().isValid()) { |
|
map.fitBounds(geojsonLayer.getBounds()); |
|
} |
|
} |
|
|
|
|
|
function getRandomColor() { |
|
const colors = [ |
|
'#3388ff', '#33a02c', '#1f78b4', '#ff7f00', '#6a3d9a', |
|
'#a6cee3', '#b2df8a', '#fb9a99', '#fdbf6f', '#cab2d6' |
|
]; |
|
return colors[Math.floor(Math.random() * colors.length)]; |
|
} |
|
|
|
|
|
function formatGeoJSON(geojson) { |
|
return JSON.stringify(geojson, null, 2); |
|
} |
|
|
|
|
|
document.addEventListener('DOMContentLoaded', function() { |
|
|
|
}); |
|
|