mapboxgl+echarts 简单制作专题地图

mapboxgl+echarts 简单制作专题地图

实现效果

在这里插入图片描述

思路

1.引用mapboxgl脚本库及样式库与echart脚本库。
2.准备专题数据,此处我们使用模拟数据,使用json模拟后台返回的统计数据。【专题数据包含行政区划坐标、统计字段(多个)】
eg:

行政区划编码行政区划坐标行政区划名称统计字段统计字段2
110000[经度,维度]北京市100200
行政区划编码[经度2,维度2]xx省50210
3.使用mapboxgl api中的Marker在地图上加载专题数据中的行政区划的点坐标,然后将每个Marker渲染成div,并将每个marker的div的id设置跟专题数据的每条数据对应起来。
4.使用echart 通过专题数据的行政区划编码将每个行政区划的div初始化为饼状图,生成专题图层。

实现过程

1.在页面引入相关依赖

    <script src="https://api.tiles.mapbox.com/mapbox-gl-js/v1.6.1/mapbox-gl.js"></script>
    <link href="https://api.tiles.mapbox.com/mapbox-gl-js/v1.6.1/mapbox-gl.css" rel="stylesheet" />
    <script src="https://cdn.jsdelivr.net/npm/echarts@3.8.5/dist/echarts.js"></script>

2.加载底图

function addBasicMap(){
  mapboxgl.accessToken =
      "your mapboxgl access_token";
    window.map = new mapboxgl.Map({
      container: "thematicMapDiv",
      style: "mapbox://styles/mapbox/streets-v11",
       center: [117.9, 33.5],
       zoom: 4
    });}

3.加载专题点坐标

//加载专题地图
function addThematicEchartLayer() {
      //销毁echartLists
      clearEchartInstantAndPoint();
	  //专题数据处理及加载到地图
      window.thematicdata.forEach(element => {
        var geojson = JSON.parse(element.GEOJSON);
        var el1 = document.createElement("div");
        el1.id = "thematicchart" + element.XZQH;
        el1.style = "height:100px;width:100px;";
        //加载专题点坐标
      window.echartMapPoints.push( new mapboxgl.Marker(el1, { offset: [-50 / 2, -50 / 2] })
          .setLngLat([
            geojson.geometry.coordinates[0],
            geojson.geometry.coordinates[1]
          ])
          .addTo(window.map));
		  //echart初始化专题图
		  addEchartInit(element);
	
})}

4.使用echart生成专题图。
备注:(此处可以根据系统需求,使用饼状图、扇形图等,参考echart api)

//echart初始化专题图
function addEchartInit(element){
		var data = [];
        Object.keys(window.thematicHeader).forEach(key => {
          if (key.indexOf("BZ") != -1) {
            data.push({ name: window.thematicHeader[key], value: element[key] });
          }
          });
        var option1 = {
            tooltip : {
            trigger: 'item',
            formatter: "{a}<br/>{b} : {c} ({d}%)"
          },
          series: {
            name:element.XZQHMC,
            type: "pie",
            data: data,
            radius: "50%",
            center: ["50%", "50%"],
            label: {
              normal: {
                position: "inner",
                fontSize:10
              },
              
            }
          }
        };
		window.echartInitLists.push(initEchart("thematicchart" + element.XZQH, option1));
   
		
}
//初始化echart表
function initEchart(id,options){
  var mychart = echarts.init(document.getElementById(id));
  mychart.setOption(options);
  return mychart;
}

全部代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>vue-manage-system</title>
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no">

    <!-- mapbox-gl -->
    <script src="https://api.tiles.mapbox.com/mapbox-gl-js/v1.6.1/mapbox-gl.js"></script>
    <link href="https://api.tiles.mapbox.com/mapbox-gl-js/v1.6.1/mapbox-gl.css" rel="stylesheet" />
	<!-- echart-->
    <script src="https://cdn.jsdelivr.net/npm/echarts@3.8.5/dist/echarts.js"></script>
  
</head>
<body>
<div style=" width: 100%;
  height: 100%;
  position: absolute;" id="thematicMapDiv"></div>
<script>
//数据为模拟数据,仅供参考
window.onload=function(){
	addBasicMap();
	//专题数据
	window.thematicdata=[{"XZQH":"110000","BZ3":30.0,"XZQHMC":"北京市","BZ2":10.0,"GEOJSON":"{\"type\": \"Feature\",\"properties\": {},\"geometry\": {\"type\": \"Point\",\"coordinates\": [116.412616834352,40.1855914174138 ]}}"},{"XZQH":"140000","BZ3":50.0,"XZQHMC":"山西省","BZ2":20.0,"GEOJSON":"{\"type\": \"Feature\",\"properties\": {},\"geometry\": {\"type\": \"Point\",\"coordinates\": [112.288799420842,37.5712486062551 ]}}"},{"XZQH":"220000","BZ3":500.0,"XZQHMC":"吉林省","BZ2":300.0,"GEOJSON":"{\"type\": \"Feature\",\"properties\": {},\"geometry\": {\"type\": \"Point\",\"coordinates\": [126.187672031028,43.6682548638262 ]}}"},{"XZQH":"360000","BZ3":100.0,"XZQHMC":"江西省","BZ2":2000.0,"GEOJSON":"{\"type\": \"Feature\",\"properties\": {},\"geometry\": {\"type\": \"Point\",\"coordinates\": [115.72225295253,27.6141576799222 ]}}"}];
	window.thematicHeader={"XZQH":"行政区划","BZ3":"小学","XZQHMC":"行政区划名称","BZ2":"初中","GEOJSON":"坐标"};
	//需要清理的对象
	 window.echartMapPoints=[];
	 window.echartInitLists=[];
	
	addThematicEchartLayer();
	
}
//加载底图
function addBasicMap(){
  //这个可以自己去mapboxgl官网申请key
  mapboxgl.accessToken =
      "pk.eyJ1IjoieWFuZ3pob3VoYW8iLCJhIjoiY2ppbG9sMXJnMXc5YjNxbnhrOWl1bXppMyJ9.-JL3W32NYV_lHqsTUIGw9Q";
    window.map = new mapboxgl.Map({
      container: "thematicMapDiv",
      style: "mapbox://styles/mapbox/streets-v11",
       center: [117.9, 33.5],
       zoom: 4
    });
}

//加载专题地图
function addThematicEchartLayer() {
      //销毁echartLists
      clearEchartInstantAndPoint();
	  //专题数据处理及加载到地图
      window.thematicdata.forEach(element => {
        var geojson = JSON.parse(element.GEOJSON);
        var el1 = document.createElement("div");
        el1.id = "thematicchart" + element.XZQH;
        el1.style = "height:100px;width:100px;";
        window.echartMapPoints.push( new mapboxgl.Marker(el1, { offset: [-50 / 2, -50 / 2] })
          .setLngLat([
            geojson.geometry.coordinates[0],
            geojson.geometry.coordinates[1]
          ])
          .addTo(window.map));
		  //echart初始化专题图
		  addEchartInit(element);
	
})}

//echart初始化专题图
function addEchartInit(element){
		var data = [];
        Object.keys(window.thematicHeader).forEach(key => {
          if (key.indexOf("BZ") != -1) {
            data.push({ name: window.thematicHeader[key], value: element[key] });
          }
          });
        var option1 = {
            tooltip : {
            trigger: 'item',
            formatter: "{a}<br/>{b} : {c} ({d}%)"
          },
          series: {
            name:element.XZQHMC,
            type: "pie",
            data: data,
            radius: "50%",
            center: ["50%", "50%"],
            label: {
              normal: {
                position: "inner",
                fontSize:10
              },
              
            }
          }
        };
		window.echartInitLists.push(initEchart("thematicchart" + element.XZQH, option1));
   
		
}
//初始化echart表
function initEchart(id,options){
  var mychart = echarts.init(document.getElementById(id));
  mychart.setOption(options);
  return mychart;
}

//清除echart及marker
function clearEchartInstantAndPoint(){
		window.echartInitLists.forEach(echartItem => {
                  echartItem.dispose();
          });
        window.echartMapPoints.forEach(pointItem => {
            pointItem.remove();
          });
}
</script>
</body>
</html>


  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用MapboxGL和Vue3来加载高德地图实例。首先,确保你已经安装了MapboxGL和Vue3的依赖。 接下来,你可以创建一个Vue组件来加载地图。在这个组件中,你需要先引入MapboxGL和高德地图的JavaScript库: ```javascript import mapboxgl from 'mapbox-gl'; import AMapLoader from '@amap/amap-jsapi-loader'; export default { mounted() { // 加载高德地图 AMapLoader.load({ key: 'your-amap-api-key', version: '2.0', plugins: ['AMap.Geocoder'] }).then(() => { // 初始化MapboxGL mapboxgl.accessToken = 'your-mapbox-access-token'; // 创建地图实例 const map = new mapboxgl.Map({ container: 'map', style: 'mapbox://styles/mapbox/streets-v11', center: [lng, lat], zoom: 12 }); // 在地图上添加高德地图瓦片图层 const amapLayer = new AMap.TileLayer(); map.addLayer(amapLayer); // 在地图上添加其他图层、标记等 // ... }); } } ``` 在上面的代码中,你需要替换`your-amap-api-key`为你的高德地图API密钥,以及`your-mapbox-access-token`为你的Mapbox访问令牌。你还可以根据需要设置地图的中心点和缩放级别。 最后,将地图容器添加到你的Vue模板中: ```html <template> <div id="map"></div> </template> <script> import mapComponent from '@/components/MapComponent.vue'; export default { components: { mapComponent } } </script> ``` 通过这种方式,你可以在Vue组件中使用MapboxGL和高德地图来加载地图实例。记得替换你的API密钥和访问令牌,以及根据需要自定义地图样式和添加其他图层。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值