zoukankan      html  css  js  c++  java
  • openlayers加载图片图层(base64图片或geojson)

    背景:后台返回base64加密图片

          this.dzmData = 'data:image/jpeg;base64,' + data  //data :后台返回值
          this.drawRaindata('data:image/jpeg;base64,' + data)
    
        drawRaindata(data) {
          // 图片的位置
          var extent = [78.3937045720084882, 26.6196231671198298, 99.1151731627114430, 36.2507410702885622]
          var name = 'rain'
          var imageLayer = new Image({
            layerName: name,
            visible: false,
            source: new ImageStatic({
              url: data,
              imageExtent: extent
            })
          })
          this.addLayer(name, imageLayer)
        }
    
    

    geojson数据加载时

        drawRaindata(data) {
          var name = 'rain'
          const features = new GeoJSON().readFeatures(data)
          console.log(typeof features)
          features.forEach(feature => {
            this.dzmStyle(feature)
          })
          //与图片加载source的不同
          const vectorSource = new VectorSource({
            features: features
          })
          var layer = new VectorLayer({
            layerName: name,
            source: vectorSource,
            visible: false // setVisible(true/false)控制显示隐藏
            // style: this.dzmStyle()
          })
          this.addLayer(name, layer)
          this.getLayerByLayerName(name)
        },
        dzmStyle(feature) {
          const max = feature.get('highValue')
          const min = feature.get('lowValue')
          const avg = (max + min) / 2
          // console.log(avg)
          let cr = 'rgba(255, 255, 255, 0)' //默认透明
          cr = this.getFeaturesColor(avg)
          feature.setStyle(new Style({
            fill: new Fill({
              color: cr
            })
          }))
        },
    

    封装简易方法

        // 降雨等值面(更具雨量值设置颜色)
        getFeaturesColor(avg) {
          if (avg <= 1) {
            return 'rgba(255, 255, 255, 0)'
          } else if (avg > 1 && avg < 5) {
            return '#A6F28E'
          } else if (avg >= 5 && avg < 10) {
            return '#258C30'
          } else if (avg >= 10 && avg < 15) {
            return '#61B8FF'
          } else if (avg >= 15 && avg < 30) {
            return '#0000E1'
          } else if (avg >= 30 && avg < 50) {
            return '#FA00FA'
          } else if (avg >= 50) {
            return '#880015'
          } else {
            return 'rgba(255, 255, 255, 0)'
          }
        },
    
        //添加图层
        addLayer(layerName, layer) {
          this.layers[layerName] = layer
          this.map.addLayer(layer)
        },
        //移除图层
        removeLayer(layerName) {
          const layer = this.layers[layerName]
          if (layer) {
            this.map.removeLayer(layer)
          }
        },
        //获取指定图层
        getLayerByLayerName(layerName) {
          let targetLayer = null
          const me = this
          if (Vue.prototype.map) {
            const layers = Vue.prototype.map.getLayers().getArray()
            layers.forEach((layer, index) => {
              const _layerName = layers[index].values_.layerName
              if (_layerName === layerName) {
                me.idx = index
                targetLayer = layers[index]
              }
            })
          }
          return targetLayer
        }
    
  • 相关阅读:
    [转]Greenplum的工作负载及资源管理
    [转]Tomcat中的Session小结
    [转]Class.forName()的作用与使用总结
    [转]如何在 Git 里撤销(几乎)任何操作
    [转]session和cookie的区别和联系,session的生命周期,多个服务部署时session管理
    piwik获取访客头像,自定义显示访问者头像(URL)和描述(标题和替代)
    php解析url并得到url中的参数及获取url参数
    php结合phantomjs实现网页截屏、抓取js渲染的页面
    利用PhantomJS进行网页截屏,完美解决截取高度的问题
    多线程编程
  • 原文地址:https://www.cnblogs.com/wwj007/p/15029187.html
Copyright © 2011-2022 走看看