如何使用echarts加载天地图
时间: 2023-05-22 21:04:14 浏览: 875
你可以使用以下代码来使用echarts加载天地图:
```html
<html>
<head>
<meta charset="utf-8">
<title>使用echarts加载天地图</title>
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/echarts/4.9.0/echarts.min.js"></script>
<script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
<script type="text/javascript" src="https://api.tianditu.gov.cn/api?v=4.0&tk=您的密钥"></script>
<style>
#main {
width: 100%;
height: 600px;
}
</style>
</head>
<body>
<div id="main"></div>
<script>
var myChart = echarts.init(document.getElementById('main'));
// 使用天地图作为底图
var tdtLayer = new ol.layer.Tile({
preload: Infinity,
source: new ol.source.XYZ({
url: 'http://t0.tianditu.com/DataServer?T=vec_w&X={x}&Y={y}&L={z}&tk=您的密钥'
})
});
var baseMapLayer = new ol.layer.Group({
layers: [tdtLayer]
});
var map = new ol.Map({
target: 'main',
layers: [baseMapLayer],
view: new ol.View({
projection: 'EPSG:4326',
center: [104.07, 30.67],
zoom: 12
})
});
echarts.util.mapData.params.params['成都'] = {
getGeoJson: function (callback) {
$.getJSON('../geoJson/chengdu.json', callback);
}
};
var convertedData = [];
//向地图添加人口密度
$.get('../data/population.json', function (data) {
echarts.registerMap('成都', data);
data.features.forEach(function (feature) {
convertedData.push({
name: feature.properties.name,
value: Math.random() * 10000
});
});
myChart.setOption(option = {
tooltip: {
trigger: 'item'
},
visualMap: {
min: 0,
max: 10000,
left: 'left',
top: 'bottom',
text: ['High', 'Low'],
seriesIndex: [1],
inRange: {
color: ['#e0ffff', '#006edd']
},
calculable: true
},
geo: {
map: '成都',
roam: true,
label: {
normal: {
show: true,
textStyle: {
color: 'rgba(0,0,0,0.4)'
}
}
},
itemStyle: {
normal: {
borderColor: 'rgba(0, 0, 0, 0.2)'
}
}
},
series: [
{
name: 'Top 5',
type: 'effectScatter',
coordinateSystem: 'geo',
data: convertedData.sort(function (a, b) {
return b.value - a.value;
}).slice(0, 5),
symbolSize: function (val) {
return val[2] / 10;
},
showEffectOn: 'render',
rippleEffect: {
brushType: 'stroke'
},
hoverAnimation: true,
label: {
normal: {
formatter: '{b}',
position: 'right',
show: true
}
},
itemStyle: {
normal: {
color: 'purple',
shadowBlur: 10,
shadowColor: '#333'
}
},
zlevel: 1
},
{
name: 'Area Name',
type: 'map',
geoIndex: 0,
data: convertedData
}
]
});
});
</script>
</body>
</html>
```
其中,需要把`您的密钥`替换成您的天地图和百度地图的密钥。
阅读全文