当前位置: 首页 > news >正文

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

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

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><divid="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>

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 负载均衡相关概念介绍(一)
  • java后端都要学那些知识
  • 3D 技术对我们的生活有哪些影响?
  • 【Java】解析方法的调用关系
  • Linux Shell编程--数组
  • Linux服务管理(五)Apache服务优化
  • 贷齐乐hpp+php特性注入
  • Gerrit 使用教程
  • C++ | stack/queue
  • 【安卓】Service的基本用法
  • 排序算法【希尔排序】
  • python识别车辆标志
  • 前端开发攻略---图片裁剪上传的原理
  • Hackademic.RTB1靶场实战【超详细】
  • S71200 - 编程 - 笔记
  • “寒冬”下的金三银四跳槽季来了,帮你客观分析一下局面
  • 【从零开始安装kubernetes-1.7.3】2.flannel、docker以及Harbor的配置以及作用
  • CSS 三角实现
  • JavaScript对象详解
  • Java小白进阶笔记(3)-初级面向对象
  • PV统计优化设计
  • React 快速上手 - 07 前端路由 react-router
  • Redis 中的布隆过滤器
  • Redux系列x:源码分析
  • ⭐ Unity 开发bug —— 打包后shader失效或者bug (我这里用Shader做两张图片的合并发现了问题)
  • 安装python包到指定虚拟环境
  • 猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
  • 前端_面试
  • 算法系列——算法入门之递归分而治之思想的实现
  • 一个JAVA程序员成长之路分享
  • 以太坊客户端Geth命令参数详解
  • 源码安装memcached和php memcache扩展
  • ​软考-高级-系统架构设计师教程(清华第2版)【第12章 信息系统架构设计理论与实践(P420~465)-思维导图】​
  • ‌分布式计算技术与复杂算法优化:‌现代数据处理的基石
  • ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTr
  • #nginx配置案例
  • #pragma once
  • #QT 笔记一
  • (10)STL算法之搜索(二) 二分查找
  • (13)[Xamarin.Android] 不同分辨率下的图片使用概论
  • (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
  • (done) 两个矩阵 “相似” 是什么意思?
  • (k8s)Kubernetes 从0到1容器编排之旅
  • (PHP)设置修改 Apache 文件根目录 (Document Root)(转帖)
  • (搬运以学习)flask 上下文的实现
  • (附源码)springboot高校宿舍交电费系统 毕业设计031552
  • (机器学习-深度学习快速入门)第三章机器学习-第二节:机器学习模型之线性回归
  • (简单) HDU 2612 Find a way,BFS。
  • (剑指Offer)面试题41:和为s的连续正数序列
  • (十)【Jmeter】线程(Threads(Users))之jp@gc - Stepping Thread Group (deprecated)
  • (顺序)容器的好伴侣 --- 容器适配器
  • (一)appium-desktop定位元素原理
  • (译)2019年前端性能优化清单 — 下篇
  • (转) RFS+AutoItLibrary测试web对话框
  • (转)Android中使用ormlite实现持久化(一)--HelloOrmLite