openlayer加载热力图

效果:

 

代码:


<template>
  <div class="home">
    <el-row :gutter="20">
      <el-col :span="20">
        <el-form label-width="100px">
          <el-form-item label="冷热度">
            <el-slider v-model="option_heatmap.blur" show-input></el-slider>
          </el-form-item>
          <el-form-item label="半径">
            <el-slider v-model="option_heatmap.radius" show-input></el-slider>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="4">
        <el-button @click="refreshData()">加载热力图</el-button>
      </el-col>
    </el-row>

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

<script>
import 'ol/ol.css'
import { Map, View } from 'ol'
import OSM from 'ol/source/OSM'
import XYZ from 'ol/source/XYZ'
import VectorSource from 'ol/source/Vector'
import GeoJSON from 'ol/format/GeoJSON'
import { Heatmap as HeatmapLayer, Tile as TileLayer } from 'ol/layer'

export default {
  data () {
    return {
      map: {},
      option_heatmap: {
        blur: 43,
        radius: 50
      },
      heatMapLayer: {},
      heatData: {
        type: 'FeatureCollection',
        features: [
          {
            type: 'Feature',
            geometry: { type: 'Point', coordinates: [112.4, 31.19] },
            properties: { weight: 0.9 }
          },
          {
            type: 'Feature',
            geometry: { type: 'Point', coordinates: [113.3, 30.6] },
            properties: { weight: 0.19 }
          },
          {
            type: 'Feature',
            geometry: { type: 'Point', coordinates: [123.3, 30.6] },
            properties: { weight: 0.419 }
          },
          {
            type: 'Feature',
            geometry: { type: 'Point', coordinates: [105.3, 30.6] },
            properties: { weight: 0.319 }
          },
          {
            type: 'Feature',
            geometry: { type: 'Point', coordinates: [106.3, 30.6] },
            properties: { weight: 0.719 }
          },
          {
            type: 'Feature',
            geometry: { type: 'Point', coordinates: [109.3, 31.6] },
            properties: { weight: 0.519 }
          },
          {
            type: 'Feature',
            geometry: { type: 'Point', coordinates: [109.3, 30.6] },
            properties: { weight: 0.319 }
          },
          {
            type: 'Feature',
            geometry: { type: 'Point', coordinates: [108.3, 32.6] },
            properties: { weight: 0.139 }
          },
          {
            type: 'Feature',
            geometry: { type: 'Point', coordinates: [118.3, 31.6] },
            properties: { weight: 0.129 }
          },
          {
            type: 'Feature',
            geometry: { type: 'Point', coordinates: [108.3, 33.6] },
            properties: { weight: 0.19 }
          },
          {
            type: 'Feature',
            geometry: { type: 'Point', coordinates: [108.3, 32.6] },
            properties: { weight: 0.189 }
          },
          {
            type: 'Feature',
            geometry: { type: 'Point', coordinates: [100.3, 30.6] },
            properties: { weight: 0.1 }
          }
        ]
      },

      newHeatData: {
        type: 'FeatureCollection',
        features: [
          {
            type: 'Feature',
            geometry: { type: 'Point', coordinates: [112.4, 31.19] },
            properties: { weight: 0.9 }
          },
          {
            type: 'Feature',
            geometry: { type: 'Point', coordinates: [113.3, 30.6] },
            properties: { weight: 0.19 }
          },
          {
            type: 'Feature',
            geometry: { type: 'Point', coordinates: [123.3, 30.6] },
            properties: { weight: 0.419 }
          },
          {
            type: 'Feature',
            geometry: { type: 'Point', coordinates: [105.3, 30.6] },
            properties: { weight: 0.319 }
          }
        ]
      }
    }
  },
  components: {},
  created () {},
  mounted () {
    this.initMap()
  },
  computed: {},
  methods: {
    initMap () {
      this.map = new Map({
        target: 'map',
        layers: [
          new TileLayer(
            {
              source: new XYZ({
                url: 'http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}'
              })
            },
            { zoomOffset: 1 }
          )
        ],
        view: new View({
          projection: 'EPSG:4326', // 使用这个坐标系
          center: [104.704968, 31.540962], // 西南科技大学
          zoom: 5
        })
      })
    },
    addHeatMapLayer () {
      this.heatMapLayer = new HeatmapLayer({
        source: new VectorSource({
          features: new GeoJSON().readFeatures(this.heatData)
        }),
        blur: this.option_heatmap.blur,
        radius: this.option_heatmap.radius
      })
      this.map.addLayer(this.heatMapLayer)
    },
    refreshData () {
      this.heatData = this.newHeatData
      this.map.removeLayer(this.heatMapLayer)
      this.addHeatMapLayer()
    }
  },
  watch: {
    option_heatmap: {
      handler (newVal, oldVal) {
        this.heatMapLayer.setBlur(this.option_heatmap.blur)
        this.heatMapLayer.setRadius(this.option_heatmap.radius)
      },
      deep: true
    }
  }
}
</script>

<style scoped lang="scss">
.home {
  height: 100vh;
  border: 1px solid red;
  .content {
    width: 1400px;
    height: 600px;
    border: 1px solid blue;
    margin: auto;
  }
}
</style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值