在地图上显示GeoJSON数据可以通过使用地图库和相应的编程语言来实现。以下是一种常见的方法:
// 引入Leaflet库
<script src="https://cdn.jsdelivr.net/npm/leaflet@1.7.1/dist/leaflet.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet@1.7.1/dist/leaflet.css" />
// 创建地图容器
<div id="map" style="width: 100%; height: 400px;"></div>
// 初始化地图
var map = L.map('map').setView([51.505, -0.09], 13);
// 添加地图图层
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors',
maxZoom: 18,
}).addTo(map);
// 加载GeoJSON数据
fetch('path/to/your/geojson/file.geojson')
.then(response => response.json())
.then(data => {
L.geoJSON(data).addTo(map);
});
path/to/your/geojson/file.geojson
替换为你的GeoJSON文件的路径。领取专属 10元无门槛券
手把手带您无忧上云