Leaflet?數(shù)據(jù)可視化實(shí)現(xiàn)地圖下鉆示例詳解
前言
說到地圖下鉆功能,做過可視化的應(yīng)該都不陌生,地圖下鉆就是通過用戶交互從全國地圖到下一級(jí)省份地圖的切換,比如在Echarts中用戶點(diǎn)擊某個(gè)省份,地圖就會(huì)切換成該省份的地圖,當(dāng)然本篇文章不介紹echarts如何去實(shí)現(xiàn)地圖下鉆,而是用Leaflet去實(shí)現(xiàn)地圖下鉆功能。
使用的框架是Vue,如果這是你第一次使用leaflet框架,還有沒對其進(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ù)需求來,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請求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, } ); // 首先加載全國地圖 getGeoJson('100000_full').then((res) => { updateGeoJson(res); }) }); </script>
效果視頻這里只是把大屏中核心部分下鉆功能實(shí)現(xiàn)了,后續(xù)會(huì)更新,關(guān)于Leaflet的其他案例。
關(guān)于Leaflet實(shí)現(xiàn)地圖下鉆到這里基本就結(jié)束了,整體思路不是很難理解,核心部分建議多看一下。 俗話說“知識(shí)是無限的,生命是有限的”,充實(shí)的一天,過的才有價(jià)值!
以上就是Leaflet數(shù)據(jù)可視化實(shí)現(xiàn)地圖下鉆示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Leaflet數(shù)據(jù)可視化地圖下鉆的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
可以自動(dòng)輪換的頁簽 tabs with auto play fucntion
HTML、CSS方面改寫了一下結(jié)構(gòu),用了一個(gè)DL javascript方面可以選擇不斷自動(dòng)循環(huán),或者只循環(huán)一次的,點(diǎn)擊以后繼續(xù)循環(huán),或者停止循環(huán)2008-02-02iframe與主框架跨域相互訪問實(shí)現(xiàn)方法
今天正好需要用到iframe 與主框架相互訪問的實(shí)現(xiàn)方法,正好看到了這篇文章,確實(shí)不錯(cuò),特分享一下,需要的朋友可以參考下2017-09-09JavaScript實(shí)現(xiàn)簡易輪播圖最全代碼解析(ES6面向?qū)ο?
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡易輪播圖最全代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09js實(shí)現(xiàn)彈出窗口、頁面變成灰色并不可操作的例子分享
為了更好的用戶體驗(yàn),現(xiàn)在網(wǎng)頁中好多地方都使用彈出層。比如提示登陸,掃描微信二維碼圖片,論壇下載彈出扣除積分提醒等。2014-05-05iframe src為圖片時(shí)的高度自適應(yīng)的代碼
iframe src為圖片時(shí)的高度自適應(yīng)的代碼...2007-10-10JavaScript和jQuery獲取input框的絕對位置實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狫avaScript和jQuery獲取input框的絕對位置實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10原生js實(shí)現(xiàn)class的添加和刪除簡單代碼
下面小編就為大家?guī)硪黄鷍s實(shí)現(xiàn)class的添加和刪除簡單代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07