Vue.js2+ArcGIS3四、热力图

Github

添加热力图


<template>
  <div id="map-container"
    style="width:100%;height:100%;">
    <div
      style="position:absolute;right:50px;top:50px;z-index:999;">
      <button
        @click="addHeatMap()">热力图</button>
      <button
        @click="clearHeatMap()">clear</button>
    </div>
  </div>
</template>
<script>
import { loadCss, loadModules } from 'esri-loader'
export default {
  name: 'HeatMap',
  data() {
    return {
      map: '',
      gisConstructor: {}, // gis 构造函数
      gisModules: [
        'esri/SpatialReference',
        'esri/graphic',
        'esri/geometry/Point',
        'esri/layers/FeatureLayer',
        'esri/renderers/HeatmapRenderer',
        'esri/geometry/Extent',
        'esri/map',
      ],
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    /**
     * @name: 清除热力图
     */
    clearHeatMap() {
      let layer = this.map.getLayer('热力图')
      this.map.removeLayer(layer)
    },

    /**
     * @name: 添加热力图
     * @param {data} Array
     * @param {type} String 权重值
     */
    // eslint-disable-next-line no-unused-vars
    addHeatMap(data, type) {
      let mockData = [
        { x: -13043465.062410325, y: 3857375.365345625, count: '10' },
        { x: -13041492.031617718, y: 3857031.398718342, count: '100' },
        { x: -13041042.964076541, y: 3855808.4062657813, count: '1000' },
        { x: -13044606.840520333, y: 3854317.8842142224, count: '100' },
        { x: -13043035.104126222, y: 3855120.4730112157, count: '10' },
      ]
      // let _type = type || 'ID'
      let _type = 'count'
      let layerDefinition = {
        geometryType: 'esriGeometryPoint',
        fields: [
          {
            name: _type,
            type: 'esriFieldTypeInteger',
            alias: _type,
          },
        ],
      }
      let featureCollection = {
        layerDefinition: layerDefinition,
        featureSet: null,
      }
      let heatMapLayer = new this.gisConstructor.FeatureLayer(
        featureCollection,
        {
          mode: this.gisConstructor.FeatureLayer.MODE_SNAPSHOT,
          outFields: ['*'],
          opacity: 1,
          id: '热力图',
        }
      )
      let heatmapRenderer = ''
      let colors = [
        'rgba(255, 0, 0, 0)',
        'rgb(0, 255, 0)',
        'rgb(255, 255, 0)',
        'rgb(255, 0, 0)',
      ]
      if (_type !== 'ID') {
        // 有权重值
        heatmapRenderer = new this.gisConstructor.HeatmapRenderer({
          field: _type,
          colors: colors,
          blurRadius: 10,
        })
      } else {
        // 无权重值
        heatmapRenderer = new this.gisConstructor.HeatmapRenderer({
          colors: colors,
          blurRadius: 50,
          maxPixelIntensity: 200,
          minPixelIntensity: 10,
        })
      }
      heatMapLayer.setRenderer(heatmapRenderer)
      this.map.addLayer(heatMapLayer, 9)
      for (let i = 0; i < mockData.length; i++) {
        let point = new this.gisConstructor.Point(
          mockData[i].x,
          mockData[i].y,
          new this.gisConstructor.SpatialReference(this.map.spatialReference)
        )
        let graphic = new this.gisConstructor.graphic(point)
        if (_type !== 'ID') {
          let count = Number(mockData[i].count)
          graphic.setAttributes({ count: count })
        }
        heatMapLayer.refresh()
        heatMapLayer.add(graphic)
      }
    },

    mapClickFun() {
      this.map.on('click', e => {
        console.log(e)
      })
    },

    /**
     * @name: 初始化地图
     */
    init() {
      // 加载 css
      loadCss('https://js.arcgis.com/3.32/esri/css/esri.css')
      // 加载模块
      loadModules(this.gisModules, {
        url: 'https://js.arcgis.com/3.32/',
      })
        .then(this.initMap)
        .then(this.mapClickFun)
    },
    initMap(args) {
      // 将 ArcGIS 的每个功能模块都存放到 gisConstructor 中
      for (let k in args) {
        let name = this.gisModules[k].split('/').pop()
        this.gisConstructor[name] = args[k]
      }
      this.map = new this.gisConstructor.map('map-container', {
        basemap: 'osm',
        logo: false,
        slider: true,
      })
      // 设置初始化范围
      let extent = {
        xmin: -117.1839455,
        ymin: 32.68087830000002,
        xmax: -117.15035189999998,
        ymax: 32.732100979999984,
      }
      /*
        如果坐标系是 4490,初始化范围需要设置空间参考坐标系
        // 'esri/SpatialReference',
        new this.gisConstructor.SpatialReference({
          wkid: 4490
        })
      */
      this.map.setExtent(
        new this.gisConstructor.Extent(
          extent.xmin,
          extent.ymin,
          extent.xmax,
          extent.ymax
        )
      )
    },
  },
}
</script>
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
Vue是一种流行的JavaScript框架,用于构建用户界面。而ArcGIS是一套强大的地理信息系统软件开发工具包,包括各种地理空间功能。ArcGIS API for JavaScript是一个用于在Web应用程序中使用ArcGIS功能的JavaScript API。而ArcGIS API for JavaScript 4.x是其中最新版本。 ArcGIS API for JavaScript 4.x提供了许多功能和组件,可以轻松地在Vue应用程序中集成和使用。其中之一就是热力图功能。热力图是一种通过颜色的渐变来可视化密度或权重的数据分布的方法。 在Vue使用ArcGIS API for JavaScript 4.x的热力图功能,首先需要在Vue项目中引入ArcGIS API的库文件。可以通过CDN引入或者使用本地安装的库文件。然后在Vue组件中使用JavaScript代码来创建和配置热力图。 在代码中,需要定义一个包含要显示的数据和热力图的参数对象。数据可以是地理数据或其他类型的数据,根据需要进行设置。然后使用热力图的构造函数创建一个独立的热力图实例。最后将热力图对象添加到地图或指定的视图中,以显示热力图热力图的外观可以通过设置参数对象中的属性进行自定义。可以设置颜色、不透明度、最小和最大权重等。还可以自定义热力图数据点的尺寸和形状等样式属性。 总之,通过在Vue应用程序中使用ArcGIS API for JavaScript 4.x,可以轻松地实现热力图功能。这为开发人员提供了一种可视化空间数据的方法,从而更好地理解和分析数据。
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值