leaflet读取{"y":115.9057200,"x":29.1660800,"label":0}格式的json文件,在地图上用不同颜色的点表示不同的label
时间: 2023-09-16 09:09:35 浏览: 150
以下是一个简单的例子,使用Leaflet和jQuery库读取JSON文件,并将其转换为地图上的标记。该代码将根据标记的标签值使用不同的颜色来呈现标记。
HTML文件:
```html
<!DOCTYPE html>
<html>
<head>
<title>Leaflet JSON Example</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<style>
#map {
height: 500px;
}
</style>
</head>
<body>
<h1>Leaflet JSON Example</h1>
<div id="map"></div>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<script src="json/markers.json"></script>
<script src="js/map.js"></script>
</body>
</html>
```
JSON文件:
```json
{
"markers": [
{
"y": 115.9057200,
"x": 29.1660800,
"label": 0
},
{
"y": 116.4084860,
"x": 39.9042000,
"label": 1
},
{
"y": 114.0661120,
"x": 22.5485150,
"label": 2
}
]
}
```
JavaScript文件:
```javascript
var map = L.map('map').setView([30, 115], 5);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Map data © OpenStreetMap contributors'
}).addTo(map);
$.getJSON("json/markers.json", function(data) {
var markers = L.markerClusterGroup();
$.each(data.markers, function(key, val) {
var marker = L.marker([val.x, val.y]);
marker.bindPopup("Label: " + val.label);
if (val.label == 0) {
marker.setIcon(L.icon({
iconUrl: 'http://maps.google.com/mapfiles/ms/icons/red.png'
}));
} else if (val.label == 1) {
marker.setIcon(L.icon({
iconUrl: 'http://maps.google.com/mapfiles/ms/icons/green.png'
}));
} else if (val.label == 2) {
marker.setIcon(L.icon({
iconUrl: 'http://maps.google.com/mapfiles/ms/icons/blue.png'
}));
}
markers.addLayer(marker);
});
map.addLayer(markers);
});
```
在JavaScript文件中,我们首先创建一个地图对象,并添加一个图层。然后,我们使用jQuery库的getJSON方法读取JSON文件。我们使用L.markerClusterGroup方法创建一个标记集合。我们使用$.each函数遍历JSON文件中的标记。对于每个标记,我们创建一个标记对象,并使用bindPopup方法添加一个标记。根据标记的标签值,我们使用setIcon方法为标记设置不同的图标。最后,我们将标记添加到标记集合中,并将其添加到地图中。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)