- 本文以临汾市地图举例,效果如下图:
- 地理小工具网站:地理json小工具

1. html代码
<div class="comps_map" ref="mapRef"></div>
2. js代码
import {ref, onMounted, nextTick} from 'vue';
import * as echarts from 'echarts';
import linFenJson from '@/assets/mapJson/linFen.json'
import router from '@/router/index.js'
onMounted(()=>{
nextTick(()=>{
drawMap()
})
})
const myChart = ref(null);
const mapData = ref([]);
const mapRef = ref(null);
const drawMap = ()=> {
myChart.value = echarts.init(mapRef.value);
console.log('myChart.value',myChart.value)
echarts.registerMap("linfen", linFenJson);
let curDataList = [
{
name:'蒲县',
value:-1
},
{
name:'安泽县',
value:-1
},
{
name:'乡宁县',
value:-1
},
{
name:'霍州市',
value:6
},
{
name:'洪洞县',
value:2
},
{
name:'尧都区',
value:3
}
]
mapData.value = linFenJson.features.map((item, index) => {
let obj = curDataList.find(curItem=>curItem.name===item.properties.name);
return {
name: item.properties.name,
value: obj?obj.value : 0,
id: item.id,
level: item.properties.level,
cityCode: item.properties.adcode,
};
});
myChart.value.setOption(
{
visualMap: {
type: "piecewise",
min: 50,
max: 200,
right: 20,
top: 10,
orient: "horizontal",
align: "left",
textGap: 3,
itemSymbol: "circle",
show: true,
seriesIndex: 0,
pieces: [
{
gt: 50,
label: ">50个",
color: "rgb(100, 16, 19)",
},
{
gte: 10,
lte: 50,
label: "10-50",
color: "rgb(149, 29, 29)",
},
{
gte: 5,
lte: 10,
label: "5-10",
color: "rgb(189, 49, 36)",
},
{
gte: 0,
lte: 5,
label: "<5",
color: "rgb(222, 134, 143)",
},
{
lt:1,
gte:0,
label: "0",
color: "rgb(227, 227, 255)",
},
{
lt:0,
gte:-1,
label: "<0",
color: "rgb(170, 170, 170)",
},
],
},
tooltip: {
show: true,
trigger: "item",
alwaysShowContent: false,
backgroundColor: "#427d7a",
borderColor: "rgba(0, 0, 0, 0.16);",
hideDelay: 100,
triggerOn: "mousemove",
enterable: true,
textStyle: {
color: "#DADADA",
fontSize: "12",
width: 20,
height: 30,
overflow: "break",
},
formatter(params) {
return `${params.data.name + ":"}</br>xxx数量:${
params.data.value
}`;
},
showDelay: 100,
},
geo: {
map: "linfen",
type: "map",
center: [104.114129, 37.550339],
zoom: 1,
},
series: [
{
name: "linfen",
type: "map",
roam: false,
mapType: "linfen",
data: mapData.value,
layoutCenter: ["50%", "50%"],
layoutSize: "92%",
itemStyle: {
normal: {
show: true,
areaColor: "#59a3a0",
},
},
emphasis: {
itemStyle: {
show: true,
areaColor: "#2b6461",
},
label: {
show: true,
textStyle: {
color: "#fff",
},
},
},
select: {
label: {
color: "#fff",
},
itemStyle: {
areaColor: "#2b6461",
},
},
label: {
normal: {
show: true,
textStyle: {
color: "#000",
},
},
},
},
],
},
true
);
myChart.value.on("click", function (params) {
let areaName = params.data.name;
});
}
3. css代码
.comps_map{
width: 800px;
height: 500px;
background-color: white;
}