Leaflet?數(shù)據(jù)可視化實(shí)現(xiàn)地圖下鉆示例詳解
前言
說(shuō)到地圖下鉆功能,做過(guò)可視化的應(yīng)該都不陌生,地圖下鉆就是通過(guò)用戶交互從全國(guó)地圖到下一級(jí)省份地圖的切換,比如在Echarts中用戶點(diǎn)擊某個(gè)省份,地圖就會(huì)切換成該省份的地圖,當(dāng)然本篇文章不介紹echarts如何去實(shí)現(xiàn)地圖下鉆,而是用Leaflet去實(shí)現(xiàn)地圖下鉆功能。
使用的框架是Vue,如果這是你第一次使用leaflet框架,還有沒(méi)對(duì)其進(jìn)行了解,還有如何在vue安裝leaflet,建議你去看我上篇文章,(基礎(chǔ)篇)
獲取GeoJson,如果有現(xiàn)成的可以本地導(dǎo)入。
創(chuàng)建文件/api/getGeoJson.ts
const getGeoJson = (code) => {
return new Promise((resolve, inject) => {
// /geojson代理
axios.get(`/geojson/areas_v3/bound/geojson?code=$[code]`).then((res) => {
if (res.data.features){
resolve(res.data.features);
} else {
inject(res);
}
});
});
}
初始化地圖
創(chuàng)建文件/lib/useLeaflet.ts
function useLeaflet(idName){
// options 參數(shù)
const initMap = (options) => {
// 實(shí)例
map.value = L.map(idName, options);
// @ts-ignore
// 記載瓦片
L.tileLayer?.chinaProvider('Geoq.Normal.PurplishBlue').addTo(map.value);
map.value.setView([41.02999636902566, 108.984375], 3);
}
}
渲染GeoJson地圖&添加事件-核心部分
function useLeaflet(idName){
// options 參數(shù)
const initMap = (options) => {
// 實(shí)例
map.value = L.map(idName, options);
// @ts-ignore
// 記載瓦片
L.tileLayer?.chinaProvider('Geoq.Normal.PurplishBlue').addTo(map.value);
map.value.setView([41.02999636902566, 108.984375], 3);
}
const updateGeoJson = (json) => {
if (isArray(json) && json.length < 1) {
return;
}
// 每次跨層級(jí)清除面板-- 性能優(yōu)化
map.value.removeLayer(geoarr.value);
// 清除Layer組
feiLineGroupLayer.value.clearLayers();
feiMakerGroupLayer.value.clearLayers();
// 遍歷json
json.forEach((item, index) => {
let areaName = item.properties.name;
let Areaitems =
datasArr.value[datasArr.value.length - 1][
areaName.replace(/(省|市|自治區(qū)|維吾爾自治區(qū))$/g, '')
];
let colors = Areaitems ?? '#008c8c';
// 設(shè)置事件
const onEachFeature = (feature, layer) => {
layer.on({
click: (e) => {
// 點(diǎn)擊
let codes = e.target.feature.properties.adcode;
let { name } = e.target.feature.properties;
// 這里根據(jù)需求來(lái),acroutes主要限制下鉆的層級(jí)
if (acroutes.value.length > 1) {
return;
}
// 處理特殊地圖
let prov =
datasArr.value[datasArr.value.length - 1][
name.replace(/(省|市|自治區(qū)|維吾爾自治區(qū))$/g, '')
];
let childrenArr = prov.children;
let objs = {};
// 更改provinces的值
provinces.value = prov;
childrenArr.map((item, index) => {
objs[item.name] = item;
});
datasArr.value.push(objs);
acroutes.value.push(codes);
// 根據(jù)輪廓修正地圖位置,以及縮放大小
map.value.fitBounds(e.target.getBounds());
// 根據(jù)地區(qū)的code請(qǐng)求geojson數(shù)據(jù)
getGeoJson(`${codes}_full`).then((res) => {
geoarr.value.clearLayers(geoarr.value);
// 因?yàn)閷蛹?jí)有限 可以考慮遞歸渲染
updateGeoJson(res);
});
},
});
};
// 設(shè)置區(qū)域默認(rèn)顏色+添加點(diǎn)擊事件
let geojson = L.geoJSON(item, {
style: {
weight: 2, //邊框粗細(xì)
opacity: 0.4, //透明度
fillColor: 'transparent', //區(qū)域填充顏色
fillOpacity: 0.3, //區(qū)域填充顏色的透明
},
onEachFeature: onEachFeature,
});
// 添加geo模塊
geoarr.value.addLayer(geojson);
});
// 添加將geoarr添加到地圖中去
map.value.addLayer(geoarr.value);
}
return{
initMap,
updateGeoJson
}
}
App.vue中
<template>
<div id="map"></div>
</template>
<script lang="ts" setup>
// 插件可加載各種地圖(如:智圖地圖,谷歌地圖,高德地圖等包含衛(wèi)星地圖)
import L from 'leaflet';
import 'leaflet/dist/leaflet.css';
import '@geoman-io/leaflet-geoman-free';
import '@geoman-io/leaflet-geoman-free/dist/leaflet-geoman.css';
import { getGeoJson } from '@/api/getGeoJson';
import { onMounted, unref } from '@vue/runtime-core';
import { defineExpose, defineEmits } from 'vue';
import { useLeaflet } from '@/lib/useLeaflet';
let { initMap, updateGeoJson } = useLeaflet('map');
onMounted(() => {
initMap(
{
attributionControl: false, // 是否使用工具集
zoomAnimation: true,
maxZoom: 13,
minZoom: 4,
zoom: 4,
worldCopyJump: true,
markerZoomAnimation: true,
zoomControl: false,
}
);
// 首先加載全國(guó)地圖
getGeoJson('100000_full').then((res) => {
updateGeoJson(res);
})
});
</script>
效果視頻這里只是把大屏中核心部分下鉆功能實(shí)現(xiàn)了,后續(xù)會(huì)更新,關(guān)于Leaflet的其他案例。
關(guān)于Leaflet實(shí)現(xiàn)地圖下鉆到這里基本就結(jié)束了,整體思路不是很難理解,核心部分建議多看一下。 俗話說(shuō)“知識(shí)是無(wú)限的,生命是有限的”,充實(shí)的一天,過(guò)的才有價(jià)值!
以上就是Leaflet數(shù)據(jù)可視化實(shí)現(xiàn)地圖下鉆示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Leaflet數(shù)據(jù)可視化地圖下鉆的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
javascript模擬鼠標(biāo)點(diǎn)擊事件原理和實(shí)現(xiàn)方法
本文詳細(xì)介紹了JS模擬鼠標(biāo)點(diǎn)擊事件的原理以及應(yīng)用場(chǎng)景,并提供了模擬鼠標(biāo)左鍵點(diǎn)擊事件、右鍵點(diǎn)擊事件、滾輪事件和移動(dòng)事件的代碼實(shí)現(xiàn),了解JS模擬鼠標(biāo)點(diǎn)擊事件的原理和實(shí)現(xiàn)方法對(duì)于開(kāi)發(fā)人員非常重要,這對(duì)于許多面向用戶的web應(yīng)用程序的開(kāi)發(fā)和測(cè)試都具有很重要的意義2023-09-09
JavaScript時(shí)間死區(qū)的問(wèn)題
JavaScript中的時(shí)間死區(qū)是指從進(jìn)入作用域到變量聲明之間的區(qū)域,在這段時(shí)間內(nèi)訪問(wèn)變量會(huì)拋出ReferenceError,本文就來(lái)介紹一下JavaScript時(shí)間死區(qū),感興趣的可以了解一下2025-03-03
Javascript驗(yàn)證上傳圖片大小[前臺(tái)處理]
javascript getElementsByClassName 和js取地址欄參數(shù)
JS中利用localStorage防止頁(yè)面動(dòng)態(tài)添加數(shù)據(jù)刷新后數(shù)據(jù)丟失
js實(shí)現(xiàn)的下拉框二級(jí)聯(lián)動(dòng)效果

