vue2+OpenLayers 地图上添加渐变色(6)

news2024/9/21 2:34:19

在这里插入图片描述
渐变还有些问题需要晚上
引入

import sxs from "@/views/json/sx.json";
import sx from "@/views/json/sx1.json";
import GeoJSON from "ol/format/GeoJSON";
import Polygon, { fromExtent } from "ol/geom/Polygon";
import LinearRing from 'ol/geom/LinearRing';

这块有两个部分的json数据 一个是全陕西省不包括市区的 一个是包括市区的
代码如下

<template>
  <div class="container">
    <div id="vue-openlayers" class="map-x"></div>
    <div
      id="info-box"
      class="info-box"
      style="width: 100px; height: 100px"
    ></div>
    <div id="canv" style="width: 100px; height: 100px"></div>
  </div>
</template>
<script>
import "ol/ol.css";
import { Map, View, style, Feature, geom, Overlay } from "ol";
import TileLayer from "ol/layer/Tile";
import XYZ from "ol/source/XYZ";
import { Vector as VectorSource } from "ol/source";
import VectorLayer from "ol/layer/Vector";
import { Point, LineString } from "ol/geom";
import { Style, Icon, Stroke, Text, Fill } from "ol/style";
import logo from "@/assets/logo.png";
import * as ol from "ol";
import "ol-ext/dist/ol-ext.css";


import sxs from "@/views/json/sx.json";
import sx from "@/views/json/sx1.json";
import GeoJSON from "ol/format/GeoJSON";
import Polygon, { fromExtent } from "ol/geom/Polygon";
import LinearRing from 'ol/geom/LinearRing';

export default {
  name: "FirstMap",
  data() {
    return {
      map: null,
      draw: null,
      maskLayer: null,
      logo,
      layers: [],
      polygonFeatureList: [],
    };
  },
  methods: {
    initMap() {
      let that = this;
      // 将图标样式应用到点要素
      const features = [];
      const point = new Point([108.56, 34.15]); // 修改坐标格式
      const feature = new Feature({
        geometry: point,
        custom: { data: "123", type: "icon" },
        type: "icon",
      });
      feature.setStyle([
        new Style({
          image: new Icon({
            crossOrigin: "anonymous",
            src: this.logo,
            // size: [40, 40],
            scale: 0.2, // 图标缩放比例
          }),
        }),
      ]);
      features.push(feature);
      //设置地图的数据源
      const source = new VectorSource({
        features,
      });
      let markLayerPoints = new VectorLayer({
        source: source,
      });

      let map = new Map({
        target: "vue-openlayers",
        layers: [
          new TileLayer({
            source: new XYZ({
              url: "https://gdtc.shipxy.com/tile.g?l=en&m=d&z={z}&x={x}&y={y}",
            }),
          }),
          markLayerPoints, // 确保图层顺序正确
          // vectorLayers,
        ],
        view: new View({
          projection: "EPSG:4326",
          center: [108.56, 34.15], // 修改中心坐标格式
          zoom: 6,
        }),
      });

      this.map = map;
      that.showSFArea() // 蓝色蒙层
      this.addAreaBoundary3();
    },
    addAreaBoundary3() {
    let that = this;
    that.removeFun("highLightLayer");
      sxs.features.forEach((item, index) => {
      that.addBoundary3(item, index);
    });
  },
  createGradientStyle(text1,adcode) {  
            const canvas = document.createElement('canvas');  
            const context = canvas.getContext('2d');  
            console.log(adcode,'adcode11111');
            // 创建渐变  
            let gradient = context.createLinearGradient(0, 0, 0, 256);  
            // gradient.addColorStop(0, 'rgba(255, 0, 0, 1)'); // 渐变开始颜色  
            // gradient.addColorStop(1, 'rgba(0, 0, 255, 1)'); // 渐变结束颜色  
            
            gradient.addColorStop(0, 'rgba(255, 0, 0, 1)'); // 渐变开始颜色  
            gradient.addColorStop(1, 'rgba(0, 0, 255, 1)'); // 渐变结束颜色  

            console.log(gradient,'gradient--------')

            // 创建渐变样式  
            // return new Style({  
            //     fill: new Fill({  
            //         color: gradient  
            //     }),  
            //     stroke: new Stroke({  
            //         color: 'black',  
            //         width: 1,  
            //     }),  
            // }); 
            
            return new Style({
        stroke: new Stroke({
          width: 2,
          color: "#65E6F9",
        }),
        fill: new Fill({
          color: gradient,
        }),
           text: new Text({
          text: text1.length > 5 ? text1.substring(0, 5) + '\r\n' + text1.substring(5):text1, // 文字内容
          fill: new Fill({
            color: "#fff"
          }),
          font: 120 / 10 + "px arial",
          overflow: true // 允许文本超出边界
        })
      })
        },
   // 行政区域标记颜色
   addBoundary3 (item, inx, type) {
    const that = this;
    that.removeFun("areaShape" + inx);
    var bdary = item.geometry.coordinates[0];
    let params = {};
    var text = item.properties.name;
    var adcode = item.properties.adcode;
    // const temp = that.countyData.filter(c => c.name === item.properties.name);
    // if (temp.length > 0) {
    //   params = temp[0]
    // }
    const polygonFeature = new Feature({
      name: text,
      // type: "polygon",
      type: item.geometry.type,
      geometry: new Polygon(bdary),
      // geometry: new MultiPolygon(item.geometry.coordinates),
      // params: params,
      // style: that.createGradientStyle() // 蓝色渐变
    });
    polygonFeature.setStyle(
      that.createGradientStyle(text,adcode) // 蓝色渐变
      // new Style({
      //   stroke: new Stroke({
      //     width: 2,
      //     color: "#65E6F9",
      //   }),
      //   fill: new Fill({
      //     color: "rgba(7,58,122,1)",
      //   }),
      //   text: new Text({
      //     text: text.length > 5 ? text.substring(0, 5) + '\r\n' + text.substring(5):text, // 文字内容
      //     fill: new Fill({
      //       color: "#fff"
      //     }),
      //     font: 120 / 10 + "px arial",
      //     overflow: true // 允许文本超出边界
      //   })
      // })
    );
    const polygonLayer = new VectorLayer({
      abc: '11111111',
      id: "areaShape" + inx,
      index: 10,
      source: new VectorSource({
        features: [polygonFeature],
      }),
    });
    that.polygonFeatureList.push(polygonLayer);
    that.map.addLayer(polygonLayer);
  },
    // 移除图层方法
    removeFun(layerId) {
      const that = this;
      const selArr = that.map.getLayers().getArray(); // 获取所有图层
      selArr.map((item, index) => {
        if (item.values_?.id == layerId) {
          // 移除图层
          that.map.removeLayer(selArr[index]);
        }
      });
    },

    // 创建蒙层,凸显区域
    showSFArea() {
      const initLayer = new VectorLayer({
        name: "blueLayer",
        // zIndex: 1,
        // opacity: 0.6,
        source: new VectorSource(),
        style: new Style({
          fill: new Fill({
            color: "rgba(3, 44, 79, 1)",
          }),
          stroke: new Stroke({
            color: "rgba(0,0,0,0.8)",
            width: 1
          })
        }),
      });
      this.map.addLayer(initLayer);

      this.addConver(initLayer);
    },
    // 添加遮罩
    addConver(converLayer) {
      let codeJson = sx;
      var fts = new GeoJSON().readFeatures(codeJson);
      const ft = fts[0];
      const converGeom = this.erase(ft.getGeometry());
      const convertFt = new Feature({
        geometry: converGeom,
      });
      converLayer.getSource().addFeature(convertFt);
    },

    // 擦除操作,生产遮罩范围
    erase(geom) {
      const extent = [-180, -90, 180, 90];
      const polygonRing = fromExtent(extent);
      const coords = geom.getCoordinates();
      coords.forEach((coord) => {
        const linearRing = new LinearRing(coord[0]);
        polygonRing.appendLinearRing(linearRing);
      });
      return polygonRing;
    },
  },
  mounted() {
    this.initMap();
  },
};
</script>
<style scoped lang="scss">
.input {
  position: fixed;
  top: 10px;
  right: 10px;
  border-radius: 10px;
  background: #fff;
  display: flex;
  flex-direction: column;
  padding: 5px;
  padding-bottom: 10px;

  > * {
    margin-top: 10px;
    display: flex;
    align-items: center;
  }
}
</style>

<style scoped lang="scss">
.container {
  position: relative;

  .btn {
    position: absolute;
    left: 4%;
    top: 1%;
  }
}

#vue-openlayers {
  width: 100vw;
  height: 100vh;
}

h3 {
  line-height: 40px;
}

/* 隐藏信息盒子的初始样式 */
#info-box {
  display: none;
  position: absolute;
  background: white;
  border: 1px solid black;
  padding: 10px;
  border-radius: 5px;
  font-size: 14px;
  pointer-events: none; /* 防止信息盒子影响鼠标事件 */
}
</style>


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2037550.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

文件上传漏洞-防御

防御文件上传的方法各种各样 1、限制文件上传的类型&#xff08;不让上传php等脚本类文件、只允许上传图片&#xff09; 2、给上传的文件重命名&#xff0c;让攻击者找不到自己传的文件在哪 3、限制文件上传大小 4、压缩上传文件 5、把上传的文件存储到文件服务器或者OSS平…

uniapp获取头像文件(二进制文件显示图片)

一、描述 由于在获取头像文件过程中&#xff0c;传递参数之后&#xff0c;请求成功了&#xff0c;但是后端给我返回了一串二进制数据流&#xff0c;傻傻的我&#xff0c;以为是乱码&#xff0c;跑去问后端大哥&#xff0c;人家跟我说这不是二进制吗&#xff0c;突然就觉得自己傻…

引领端侧多模态新时代:MiniCPM-V 2.6重磅登场

前沿科技速递&#x1f680; 在人工智能领域&#xff0c;每一次技术的进步都伴随着参数规模的提升和计算力的突破。然而&#xff0c;面壁智能公司最新推出的MiniCPM-V 2.6端侧多模态模型&#xff0c;却以相对“小巧”的8B参数量级&#xff0c;打破了传统思维&#xff0c;实现了端…

初学者入门的可视化超级色彩公式

色彩不仅是视觉元素&#xff0c;也是数据表达的重要工具。在临床数据的可视化过程中&#xff0c;合理的色彩搭配能帮助观众迅速理解数据背后的意义。例如&#xff0c;高危状态的患者可能用红色表示&#xff0c;而健康状态用绿色表示。不同色彩之间的对比度和相对位置将决定数据…

蓝牙耳机怎么连接手机?苹果用户关注这3个方法

在这个无线连接日益普及的时代&#xff0c;蓝牙耳机已成为我们日常生活中不可或缺的伴侣。然而&#xff0c;对于初次使用或遇到连接问题的用户来说&#xff0c;如何解决蓝牙耳机怎么连接手机的问题可能会有些许困惑。本文将为您详细介绍3种简单易行的方法&#xff0c;帮助您轻松…

Transformer动画讲解-多模态

Transformer模型是一种基于自注意力机制的神经网络架构&#xff0c;广泛应用于自然语言处理任务&#xff0c;如机器翻译、文本摘要等。 Transformer模型在多模态数据处理中扮演着重要角色&#xff0c;其能够高效、准确地处理包含不同类型数据&#xff08;如图像、文本、音频等&…

Polars简明基础教程十一:可视化(一)

到本次讲座结束时&#xff0c;你将能够&#xff1a; 使用Polars的内部plot方法从Polars创建图表使用外部绘图库从Polars创建图表了解这些库如何支持Polars 通常&#xff0c;需要可视化库的最新版本来实现最大程度的兼容性 import polars as plimport hvplot as hv import ma…

陈丽华珍藏梁永和书画作品,展现中华艺术之美

近两年来&#xff0c;富华国际集团董事局主席、中国紫檀博物馆馆长陈丽华女士收藏了多件由人民艺术家、著名画家梁永和先生亲笔创作的书画作品&#xff0c;其中包括《松鹤延年》、《香荷》、《江山多娇》和《硕果累累》等佳作。此外&#xff0c;梁永和先生还特地为陈丽华女士创…

【喜报】祝贺青创智通签约广州进德生物科技SunFMEA项目

SunFMEA SunFMEA是由北京青创智通科技有限公司自主研发的一款基于AIAG-VDA-FMEA标准的失效模式和影响分析软件&#xff0c;软件满足新版七步法分析流程&#xff0c;兼具DFMEA和PFMEA&#xff0c;以结构树的方式直观、完整、快速地指导用户完成FMEA分析的整个流程&#xff0c;能…

前端纯数组转树形结构

问题描述 前端需要处理后端返回的数据&#xff0c;展示如下。 解决方式 因为使用ProTable组件&#xff0c;那么数据只要携带children字段&#xff0c;就可以如上图展示。 方式一&#xff1a;后端返回数据的时候&#xff0c;直接封装好&#xff0c;如下&#xff1a; const…

electron+vue搭建命令

electronvue搭建 要使用Electron和Vue来搭建一个桌面应用程序&#xff0c;你可以遵循以下步骤&#xff1a; 1.创建一个Vue项目&#xff1a; vue create my-electron-app2.进入项目目录&#xff1a; cd my-electron-app3.添加Electron&#xff1a; vue add electron-builde…

指针初阶1(学习编程的第二十四天)

1.指针是什么&#xff1f; 编号可以抽象为地址&#xff0c;地址就可以抽象为指针 2.指针和指针类型 指针类型的意义&#xff1a;1.指针类型决定了 指针解引用的权限有多大 int有4个字节 double有8个 char只有1个 2.指针类型决定了&#xff0c;指针走一步&#xff0c;能走多…

自回归分布滞后模型 (ARDL)及 Stata 具体操作步骤

目录 一、引言 二、文献综述 三、理论原理 四、实证模型 五、程序代码及解释 六、代码运行结果 一、引言 自回归分布滞后模型&#xff08;Autoregressive Distributed Lag Model&#xff0c;简称 ARDL&#xff09;在时间序列分析中具有重要的地位&#xff0c;它能够同时捕…

八种排序算法的复杂度(C语言)

归并排序(递归与非递归实现,C语言)-CSDN博客 快速排序(三种方法,非递归快排,C语言)-CSDN博客 堆排序(C语言)-CSDN博客 选择排序(C语言)以及选择排序优化-CSDN博客 冒泡排序(C语言)-CSDN博客 直接插入排序(C语言)-CSDN博客 希尔排序( 缩小增量排序 )(C语言)-CSDN博客 计数…

【文件IO】文件内容操作

读文件、写文件&#xff0c;都是操作系统提供了 API&#xff0c;在 Java 中也进行了封装&#xff0c;叫“文件流”/“IO流” Stream 流&#xff0c;形象比喻&#xff0c;水流/气流 水流的特点&#xff1a;我要通过水龙头&#xff0c;接 1000ml 水 直接一口气&#xff0c;把 100…

µC/OS-III

第一章 μCOS 简介 1.1 初识 μCOS 实际上&#xff0c;一个 CPU 核心在某一时刻只能运行一个任务&#xff0c;由于切换处理任务的速度非常快&#xff0c;因此给人造成了一种同一时刻有多个任务同时运行的错觉。 操作系统的分类方式可以由任务调度器的工作方式决定&am…

RCE漏洞基础初了解

目录 一、简介 二、php的命令执行函数 2.1 exec 2.2 passthru 2.3 shell_exec 2.4 popen 三、代码执行 3.1 php的回调后门 3.1.1 回调后门的老祖宗 3.1.2 数组造成单参数回调后门 3.1.3 绕过安全狗 ​编辑 四、来看看php中webshell奇淫技巧 4.1eval长度限制突破方法…

problem with running OpenAI Cookbook‘s chatbot

题意&#xff1a;运行 OpenAI Cookbook 的聊天机器人时遇到问题 问题背景&#xff1a; Im having trouble running the chatbot app in the OpenAI Cookbook repository. 我在运行 OpenAI Cookbook 仓库中的聊天机器人应用程序时遇到了问题。 What I tried 我尝试的内…

240810-Gradio通过HTML组件打开本地文件+防止网页跳转到about:blank

A. 最终效果 B. 可通过鼠标点击打开文件&#xff0c;但会跳转到about:blank import gradio as gr import subprocessdef open_pptx():pptx_path /Users/liuguokai/Downloads/240528-工业大模型1.pptxtry:subprocess.Popen([open, pptx_path])return "PPTX file opened s…

七、3 AD单通道(代码)

1、步骤 &#xff08;1&#xff09;开启GPIO和ADC的时钟、配置ADCCLK的分频器 &#xff08;2&#xff09;配置GPIO &#xff08;3&#xff09;配置多路开关&#xff08;把左边的通道接入到右边的规则组中&#xff09; &#xff08;4&#xff09;配置ADC转换器 &#xff08;…