Vite+Vue+iClient for Leaflet引入并实现MapV/Eharts第三方可视化库示例

这篇具有很好参考价值的文章主要介绍了Vite+Vue+iClient for Leaflet引入并实现MapV/Eharts第三方可视化库示例。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

作者:gaogy

背景

最近很多小伙伴咨询关于基于Vue3框架搭建iClent开发环境并使用Echarts与MapV等第三方可视化库的问题。本文就以iClient for Leaflet为例,在新一代前端框架构建工具Vite与Vue3项目中,演示如何搭建iClent开发环境并使用Echarts与MapV等第三方可视化库。

本文所使用的依赖与配置项如下:

"@supermap/iclient-leaflet": "^11.0.1",
"vue": "^3.2.47",
"@vitejs/plugin-vue": "^4.1.0",
"vite": "^4.3.2"

一、使用Vite构建Vue3+JavaScript项目

使用vite构建必须先安装node与vue环境,并安装vite工具,安装操作可自行百度。以下为项目创建步骤:

1、运行创建项目命令

npm init vite

2、输入项目名称,选择前端框架为Vue,选择项目类型为JavaScript
Vite+Vue+iClient for Leaflet引入并实现MapV/Eharts第三方可视化库示例

3、执行cd leafletdemo、npm install 与 npm run dev命令
Vite+Vue+iClient for Leaflet引入并实现MapV/Eharts第三方可视化库示例

4、访问http://localhost:5174/,确认项目成功创建
Vite+Vue+iClient for Leaflet引入并实现MapV/Eharts第三方可视化库示例

二、搭建iClient for Leaflet开发环境

1、在命令行中输入以下命令安装 SuperMap iClient for Leaflet:

npm install @supermap/iclient-leaflet

2、在index.html文件的标签中引入 Leaflet CSS 文件和 iclient-leaflet CSS 文件,如下:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
    <link rel="stylesheet" href="https://iclient.supermap.io/dist/leaflet/iclient-leaflet.min.css" />
    <title>Vite + Vue + Leaflet</title>
  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/src/main.js"></script>
  </body>
</html>

3、在componets文件夹中新建一个Map组件,创建一个id为map的div标签,并设置其style样式;使用按需引入方式引入相应模块;然后在methods中写入初始化地图函数init,并在mouted中运行init函数。

<template>
  <div id="map"></div>
</template>

<script>
import L from 'leaflet'
import { TiledMapLayer } from '@supermap/iclient-leaflet'

export default {
  name: 'MapView',
  data() {
    return {
      map: {},
      mapUrl: 'https://iserver.supermap.io/iserver/services/map-china400/rest/maps/China'
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.map = L.map('map', {
        center: [40, 117],
        maxZoom: 18,
        zoom: 4
      })
      new TiledMapLayer(this.mapUrl).addTo(this.map)
    }
  }
}
</script>

<style>
#map {
  margin: 0;
  padding: 0;
  overflow: hidden;
  background: #fff;
  width: 100%;
  height: 100%;
  position: absolute;
}
</style>

4、在App.vue中引入我们刚才创建的Map组件

<script setup>
import Map from './components/Map.vue'
</script>

<template>
  <Map />
</template>

<style>
body {
  margin: 0;
  padding: 0;
}
</style>

5、使用npm run dev 命令运行项目,在浏览器中打开,发现控制台报如下错误:
Vite+Vue+iClient for Leaflet引入并实现MapV/Eharts第三方可视化库示例

产生原因与解决办法可以参考博客https://blog.csdn.net/supermapsupport/article/details/123569581。首先在package.json>devDependencies里增加

 "events": "^3.3.0",
 "util": "^0.12.4",

Vite+Vue+iClient for Leaflet引入并实现MapV/Eharts第三方可视化库示例

然后在vite.config.js ,增加define配置
Vite+Vue+iClient for Leaflet引入并实现MapV/Eharts第三方可视化库示例

最后ctrl+c结束运行项目,使用那npm install命令安装一下需要的模块后,再使用npm run dev命令运行项目如下:
Vite+Vue+iClient for Leaflet引入并实现MapV/Eharts第三方可视化库示例

至此,iClient for Leaflet开发环境搭建成功

三、第三方可视化库Echarts的使用

由于iclient-leaflet中已经包含了Echarts库,所以在搭建好iClient for Leaflet开发环境后不需要单独下载Echarts库,使用的时候只需要引入echarts模块就行

import echarts from 'echarts'

初始化Echarts实例对象

let div = L.DomUtil.create('div')
this.chart = echarts.init(div, '', {
  width: 600,
  height: 400
})

指定配置项和数据(option)

this.option = {
  title: {
    subtext: '数据纯属虚构'
  },
  tooltip: {
    trigger: 'axis'
  },
  legend: {
    //data:["最高气温"]
    data: ['最高气温', '最低气温']
  },
  toolbox: {
    show: true,
    feature: {
      dataZoom: {
        yAxisIndex: 'none'
      },
      magicType: { type: ['line', 'bar'] },
      restore: {},
      saveAsImage: {}
    }
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
  },
  yAxis: {
    type: 'value',
    axisLabel: {
      formatter: '{value} °C'
    }
  },
  series: [
    {
      name: '最高气温',
      type: 'line',
      data: [11, 11, 15, 13, 12, 13, 10],
      markPoint: {
        data: [
          { type: 'max', name: '最大值:' },
          { type: 'min', name: '最小值:' }
        ]
      },
      markLine: {
        data: [
          { type: 'average', name: '平均值' },
          [
            {
              symbol: 'none',
              x: '90%',
              yAxis: 'max'
            },
            {
              symbol: 'circle',
              label: {
                normal: {
                  position: 'start',
                  formatter: '最大值:'
                }
              },
              type: 'max',
              name: '最高点'
            }
          ]
        ]
      }
    },
    {
      name: '最低气温',
      type: 'line',
      data: [1, -2, 2, 5, 3, 2, 0],
      markPoint: {
        data: [
          { type: 'max', name: '最大值:' },
          { type: 'min', name: '最小值:' }
        ]
      },
      markLine: {
        data: [
          { type: 'average', name: '平均值' },
          [
            {
              symbol: 'none',
              x: '90%',
              yAxis: 'min'
            },
            {
              symbol: 'circle',
              label: {
                normal: {
                  position: 'start',
                  formatter: 'Z值范围 最小值:'
                }
              },
              type: 'min',
              name: '最低点'
            }
          ]
        ]
      }
    }
  ]
}
this.chart.setOption(this.option)

在这里使用地图服务查询省会城市,渲染到地图中,并且绑定popup

let param = new QueryBySQLParameters({
  queryParams: [
    {
      name: 'China_ProCenCity_pt@China',
      attributeFilter: '1 = 1'
    },
    {
      name: 'China_Capital_pt@China',
      attributeFilter: '1 = 1'
    }
  ]
})
new QueryService(this.mapChinaUrl).queryBySQL(param, serviceResult => {
  serviceResult.result.recordsets.map(record => {
    that.resultLayer = L.geoJSON(record.features, {
      coordsToLatLng: coords => {
        let latlng = L.CRS.EPSG3857.unproject(L.point(coords[0], coords[1]))
        latlng.alt = coords[2]
        return latlng
      }
    }).bindPopup(
      layer => {
        let city = layer.feature.properties.NAME
        that.chart.setOption({
          title: {
            text: city + '未来一周气温变化'
          },
          series: [
            {
              name: '最高气温',
              data: [10 + Math.ceil(Math.random() * 10), 10 + Math.ceil(Math.random() * 10), 10 + Math.ceil(Math.random() * 10), 10 + Math.ceil(Math.random() * 10), 10 + Math.ceil(Math.random() * 10), 10 + Math.ceil(Math.random() * 10), 10 + Math.ceil(Math.random() * 10)]
            },
            {
              name: '最低气温',
              data: [8 - Math.ceil(Math.random() * 10), 8 - Math.ceil(Math.random() * 10), 8 - Math.ceil(Math.random() * 10), 8 - Math.ceil(Math.random() * 10), 8 - Math.ceil(Math.random() * 10), 8 - Math.ceil(Math.random() * 10), 8 - Math.ceil(Math.random() * 10)]
            }
          ]
        })
        return that.chart.getDom()
      },
      { maxWidth: 700 }
    ).addTo(that.map)
  })
})

运行项目,查看效果如下:
Vite+Vue+iClient for Leaflet引入并实现MapV/Eharts第三方可视化库示例

四、第三方可视化库MapV的使用

由于iclient-leaflet中已经包含了MapV库,所以在搭建好iClient for Leaflet开发环境后不需要单独下载MapV库,使用的时候只需要引入相应模块就行。与Echarts不同的是,MapV的模块需要按需引入,或者使用以下方式全部引入

import * as mapv from 'mapv'

然后,按需引入MapVLayer

import { MapVLayer } from '@supermap/iclient-leaflet'

最后,我们随机构造一些数据作为蜂巢图的数据用于展示。

let randomCount = 1000
let data = []
// 构造数据
while (randomCount--) {
  let cityCenter = mapv.utilCityCenter.getCenterByCityName(this.citys[parseInt(Math.random() * this.citys.length)])
  data.push({
    geometry: {
      type: 'Point',
      coordinates: [cityCenter.lng - 2 + Math.random() * 4, cityCenter.lat - 2 + Math.random() * 4]
    },
    count: 30 * Math.random()
  })
}
let dataSet = new mapv.DataSet(data)
let options = {
  fillStyle: 'rgba(55, 50, 250, 0.8)',
  shadowColor: 'rgba(255, 250, 50, 1)',
  shadowBlur: 20,
  max: 100,
  size: 50,
  unit: 'px', // unit可选值['px', 'm'],默认值为'px'
  label: {
    show: true,
    fillStyle: 'white'
  },
  globalAlpha: 0.5,
  gradient: { 0.25: 'rgb(0,0,255)', 0.55: 'rgb(0,255,0)', 0.85: 'yellow', 1.0: 'rgb(255,0,0)' },
  draw: 'honeycomb'
}
new MapVLayer(dataSet, options).addTo(this.map) //创建MapV图层

运行项目,查看效果如下:
Vite+Vue+iClient for Leaflet引入并实现MapV/Eharts第三方可视化库示例

五、其他地图库

针对iClient for MapboxGL与iClient for OpenLayers引入方式与Leaflet相同,只需替换css、引入模块及初始化代码即可; iClient for Classic 引入地址,安装后按照module中的README进行使用。

完整Demo可参考下载资源,链接: Vite+Vue+iClient for Leaflet引入并实现MapV/Eharts第三方可视化库示例文章来源地址https://www.toymoban.com/news/detail-456245.html

到了这里,关于Vite+Vue+iClient for Leaflet引入并实现MapV/Eharts第三方可视化库示例的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Cesium引入vite + vue3

    卸载命令 使用 vite.config.js 组件使用 加载高德地图 高德地图的加载方便很多,并不需要申请key,可以直接加载到我们的Cesium中。 加载ArcGISMap地图

    2024年02月14日
    浏览(22)
  • vue3+vite导入第三方库报ERR_ABORTED 504 (Gateway Timeout)

    vue3+vite,使用了三方库NProgress,使用npm install --save nprogress 安装后在项目中使用,报错:ERR_ABORTED 504 (Gateway Timeout), 卸载后重新安装 ,还是报这个错, 难道是这个库有问题?,新建了一个空项目,安装后, 在新项目中可以正常使用, 排除库的问题,应该就是这个库没有正确

    2024年02月13日
    浏览(34)
  • vue3+ts import引入第三方js文件报错解决方法

    报错原因 :执行 import XXX from ‘XXX.js’ 报错,The requested module ‘xxx.js’ does not provide an export named ‘default’ 可能是第三方文件不支持模块化标准,不能按需导入 解决方案 : 第一种方法 : 在index.html里利用script全局引入 第二种方法 : 在所需的.vue文件里单独引入,最后在hea

    2024年02月16日
    浏览(30)
  • Vite+Vue3项目全局引入scss文件

    Sass 是世界上最成熟、最稳定、最强大的专业级CSS扩展语言!在日常项目开发过程中使用非常广泛,今天主要讲一下 Vite+Vue3 项目中该如何全局引入 scss 文件,引入混合 mixin 文件的不同配置。捎带说一下 Vue2 中的引入方式做一下简单的对比。 1.1 安装 vite 已经将 sass 预处理器的

    2024年02月06日
    浏览(29)
  • vue3 vite ts引入vue文件报错 ts(2307)

    vue3 vite ts 生成的项目模板,在ts文件中引入vue文件报错 ts(2307),只是ts报错,并不影响项目运行。 官方文档有说明:http://vue.dragonlm.com/guide/typescript/overview.html#ide-support 解决方法是安装插件,之后即可正常解析路径,并可以跳转到对应文件。 TypeScript Vue Plugin (Volar)

    2024年02月16日
    浏览(36)
  • VUE3+vite项目中动态引入组件和异步组件

    1. 把项目中所有vue文件注册成异步组件。 2. 获取组件 在setup函数获取组件 3. 参考如下 Glob 导入 Vite 支持使用特殊的 import.meta.glob 函数从文件系统导入多个模块: 以上将会被转译为下面的样子: 你可以遍历 modules 对象的 key 值来访问相应的模块: 匹配到的文件默认是懒加载的

    2024年02月10日
    浏览(85)
  • Vite vue 使用cdn引入element-plus

    vite-plugin-cdn-import:cdn的引入插件 vite.config.js 使用cdn 引入 element-plus 一定也要用cdn 引入 vue、vue-demi 并且引入顺序不能出错, 注意点 ①  如果不引入vue,就会提示createElementVnode找不到, 原因是在element-plus这个源码中也在使用vue这个变量   ②如果不引入vue-demi,可以理解为vu

    2024年02月01日
    浏览(39)
  • vue3 vite直接创建项目 添加 element-ui 按需引入和全部引入

     创建home 工程 yran dev就可以看到一个网址 点进去就可以看到效果了 添加element-ui 之前的main.js 修改后的 element-plus 在vue3中的按需引入。要比2简介很多 。自动导入也是推荐的一种写法 我们注释掉全局引用  然后改为按需引入 源文件  修改后 首先安装两款插件 修改后

    2024年02月17日
    浏览(42)
  • vite vue3+ts @引入无提示/解决提示报错

    1. setting.json  这时候已经有提示了 但是ts会报错   tsconfig.json  以@开头代表src目录下 以#开头代表ts目录下  配置好后 重新引入 没有红色波浪线但是 vite不认识 @符号 运行项目还是会报错  这时候就需要在vite.config.ts中添加配置 如果path会报错 需要下载一下@types/node 再重启下

    2024年02月15日
    浏览(33)
  • Vite按需引入自定义组件unplugin-vue-components

    1.安装插件 2.vite.config.ts文件加如下代码 3.tsconfig.json   include属性加入 ‘./components.d.ts’  目录里面会多一个文件 然后你在 components 里面封装组件就是全局组件了 我一开始是 先封装完组件然后 在配置的上面这些,就一直不显示 全局组件 ,然后你可以先把你封装的文件删除

    2024年02月12日
    浏览(84)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包