39 openlayers 对接地图图层 绘制点线面圆

前言

这里主要是展示一下 openlayers 的一个基础的使用 

主要是设计 接入地图服务器的 卫星地图, 普通的二维地图, 增加地区标记 

增加 省市区县 的边界标记

基础绘制 点线面园 等等

测试用例

<template><div style="width: 1920px; height:1080px;" ><div class="olMapUsageClass"></div><div class="overdelay1" ref="overdelay1" >this is over delay1</div></div></template><script>import Map from 'ol/Map'import View from 'ol/View'import DragPan from 'ol/interaction/DragPan'import MouseWheelZoom from 'ol/interaction/MouseWheelZoom'import PointerInteraction from 'ol/interaction/Pointer'import GeoJSON from 'ol/format/GeoJSON'import {Tile as TileLayer} from 'ol/layer'import {Vector as VectorLayer} from 'ol/layer'import {Image as ImageLayer} from 'ol/layer'import {Vector as VectorSource} from 'ol/source'import {Feature as Feature} from 'ol'import Point from 'ol/geom/Point'import LineString from 'ol/geom/LineString'import Polygon from 'ol/geom/Polygon'import CircleGeo from 'ol/geom/Circle'import XYZ from "ol/source/XYZ"import ImageStatic from "ol/source/ImageStatic"import {Circle, Fill, Icon, RegularShape, Stroke, Style, Text} from 'ol/style'import Overlay from 'ol/Overlay'import {transformExtent, transform} from "ol/proj"let sichuanJson = require(`../../public/json/sichuan.json`)export default {name: "olMapUsage",components: {},props: {},data() {return {map: null,tdtImgLayer: null,labelLayer: null,overlay: null,};},computed: {},watch: {},created() {},mounted() {this.initMap()this.test01AddBoundsLayer()this.test02AddDtImageLayer()// this.test03AddDtTDLayer()this.test04AddDtLabelLayer()this.test05AddImageLayer()this.test06AddCircleLayer([104.065735, 30.359462])this.test06AddCircleLayer([104.565735, 30.859462], "red")this.test07AddLineLayer()this.test08AddAreaLayer()this.test09AddCircleLayer()// this.test12SetCenterThenAddOverlay()},methods: {initMap() {let center = [104.065735, 30.659462]let projection = "EPSG:4326"let zoom = 10let minZoom = 5let maxZoom = 20const layer = []const view = new View({...(this.viewOptions || {}),projection,center,zoom,minZoom,maxZoom})this.map = new Map({...(this.mapOptions || {}),layers: [].concat(layer),view: view,target: this.$el,controls: [],interactions: [new DragPan(),new MouseWheelZoom(),new PointerInteraction({handleEvent: this.handleEvent})]})},test01AddBoundsLayer() {const source = new VectorSource({})const style = {color: 'rgba(75,165,234,1)', width: '3'}const fill = 'rgba(255,255,255,0)'let parsedStyle = this.parseStyle({style, fill})const boundsLayer = new VectorLayer({zIndex: 1,source,parsedStyle})this.appendBounds2VectorLayer(boundsLayer, sichuanJson)this.map.addLayer(boundsLayer)},test02AddDtImageLayer() {this.tdtImgLayer = new TileLayer({zIndex: 2,source: new XYZ({projection: "EPSG:4326",url: "http://192.168.1.111:8888/tianditu/servlet/GoogleSatelliteMap?x={x}&y={y}&z={z}",}),});this.map.addLayer(this.tdtImgLayer);},test03AddDtTDLayer() {this.tdtImgLayer = new TileLayer({zIndex: 2,source: new XYZ({projection: "EPSG:4326",url: "http://192.168.1.111:8888/tianditu/servlet/GoogleTDMap?x={x}&y={y}&z={z}",}),});this.map.addLayer(this.tdtImgLayer);},test04AddDtLabelLayer() {this.labelLayer = new TileLayer({zIndex: 2,source: new XYZ({projection: "EPSG:4326",url: "http://192.168.1.111:8888/tianditu/servlet/GoogleTransparentMap?x={x}&y={y}&z={z}",}),});this.map.addLayer(this.labelLayer);},test05AddImageLayer() {// let extent = transformExtent([104.065735, 30.659462, 104.165735, 30.759462], "EPSG:4326", "EPSG:4326")let extent = [104.065735, 30.659462, 104.165735, 30.759462]let imageLayer = new ImageLayer({zIndex: 20,source: new ImageStatic({url: "/img/theme/desktop/17.jpg",projection: "EPSG:4326",imageExtent: extent}),});this.map.addLayer(imageLayer);},test06AddCircleLayer(coord, color) {color = color || 'green'let style = new Style({image: new Circle({radius:20,fill: new Fill({color: color})})})let feature = new Feature({geometry: new Point(coord)})feature.setStyle(style)let source = new VectorSource()source.addFeature(feature)let layer = new VectorLayer({zIndex: 20,source: source})this.map.addLayer(layer);},test07AddLineLayer() {let style = new Style({stroke: new Stroke({color: "blue",width: 3})})let feature = new Feature({geometry: new LineString([[104.065735, 30.359462],[104.165735, 30.359462],[104.265735, 30.459462],])})feature.setStyle(style)let source = new VectorSource()source.addFeature(feature)let layer = new VectorLayer({zIndex: 20,source: source})this.map.addLayer(layer);},test08AddAreaLayer() {let style = new Style({fill: new Fill({color: "#ff0000"}),stroke: new Stroke({color: "blue",width: 3})})let feature = new Feature({geometry: new Polygon([[transform([104.065735, 30.559462], "EPSG:4326", "EPSG:4326"),transform([104.165735, 30.559462], "EPSG:4326", "EPSG:4326"),transform([104.165735, 30.659462], "EPSG:4326", "EPSG:4326"),]])})feature.setStyle(style)let source = new VectorSource()source.addFeature(feature)let layer = new VectorLayer({zIndex: 20,source: source})this.map.addLayer(layer);},test09AddCircleLayer() {let style = new Style({fill: new Fill({color: "#ffff00"}),stroke: new Stroke({color: "#00ffff",width: 3})})let feature = new Feature({geometry: new CircleGeo(transform([104.665735, 30.559462], "EPSG:4326", "EPSG:4326"), 0.2)// geometry: new Circle([104.265735, 30.559462], 300)})feature.setStyle(style)let source = new VectorSource()source.addFeature(feature)let layer = new VectorLayer({zIndex: 20,source: source})this.map.addLayer(layer);},test10SetCenter(coord, color) {this.map.getView().setCenter(coord)this.test06AddCircleLayer(coord, color)},test11AddOverlay(coord) {this.overlay && this.map.removeOverlay(this.overlay)this.overlay = new Overlay({element: this.$refs.overdelay1,position: coord,positioning: "bottom-center",offset: [0, 0],autoPan: true,autoPanMargin: 200,autoPanAnimation: {duration: 1000},map: this.map})this.map.addOverlay(this.overlay)},test12SetCenterThenAddOverlay() {// refer cyclethis.test06AddCircleLayer([10.265735, 10.659462], "#007f5a")this.test06AddCircleLayer([105.565735, 30.759462], "#0039ff")let _this = this// use this for map.addOverlay's animation updatesetTimeout(function() {_this.test11AddOverlay([10.065735, 10.459462])_this.test10SetCenter([10.065735, 10.459462], "yellow")}, 2000)// the core case, normal or exception or compensatedsetTimeout(function() {// case1. function of addOverlay_this.test11AddOverlay([105.065735, 30.259462])// case2. normal case// _this.test11AddOverlay([105.065735, 30.259462])// _this.test10SetCenter([105.065735, 30.259462], "red")// case3. exception case// _this.test10SetCenter([105.065735, 30.259462], "red")// _this.test11AddOverlay([105.065735, 30.259462])// case4. compensated case// _this.test10SetCenter([105.065735, 30.259462], "red")// setTimeout(function() {//   _this.test11AddOverlay([105.065735, 30.259462])// }, 1000)}, 5000)},appendBounds2VectorLayer(layer, json) {const geoJson = this.geoJsonDecode(json);let features = new GeoJSON().readFeatures(geoJson) || [];features = features.map(feature => {feature.__vm__ = this;return feature});const source = layer.getSource();source.addFeatures(features)},handleEvent(e) {if (e.type === "pointermove") {return true}console.log(" handle event : ", e)return true},geoJsonDecode(json) {const features = json.features || []features.forEach(feature => {const geometry = feature.geometry || {}const {coordinates, encodeOffsets} = geometryif (!encodeOffsets) returngeometry.coordinates = coordinates.map((coordinate, i) => {if (Array.isArray(coordinate)) {return coordinate.map((item, j) => {return this.decodePolygon(item, encodeOffsets[i][j])})} else {return this.decodePolygon(coordinate, encodeOffsets[i])}})geometry.encodeOffsets = null})return json},decodePolygon(coordinate, encodeOffsets) {const result = [];let prevX = encodeOffsets[0];let prevY = encodeOffsets[1];for (let i = 0; i < coordinate.length; i += 2) {let x = coordinate.charCodeAt(i) - 64;let y = coordinate.charCodeAt(i + 1) - 64;x = (x >> 1) ^ (-(x & 1));y = (y >> 1) ^ (-(y & 1));x += prevX;y += prevY;prevX = x;prevY = y;result.push([x / 1024, y / 1024]);}return result;},parseStyle(settings, StyleModel) {const PROPS_MAP = {fill: Fill,text: Text,stroke: Stroke,circle: Circle,icon: Icon,regularShape: RegularShape,backgroundFill: Fill,backgroundStroke: Stroke}const IMAGE_PROPS = [Circle, Icon, RegularShape]const opts = {};Object.entries(settings).forEach(([key, value]) => {const Model = PROPS_MAP[key];if (key === 'font') {value = `${value} sans-serif`}opts[IMAGE_PROPS.includes(Model) ? 'image' : key] = this.parseValue(Model, key, value)});return new (StyleModel || Style)(opts)},parseValue(Model, key, value) {if (value === undefined || value === null) return;if (!Model) return value;if (['fill', 'backgroundFill'].includes(key)) return this.parseFill(value);if (key === 'text') {return isObject(value) ? parse(value, Model) : value}return parse(value, Model)},parseFill(fill) {const opts = this.isObject(fill) ? fill : {color: fill}return new Fill(opts)},isObject(value) {return typeof value === 'object'}}};
</script><style lang="scss">.olMapUsageClass {}.overdelay1 {position: absolute;border: 1px greenyellow solid;width: 200px;height: 50px;}
</style>

绘制卫星地图 + 地图标注

执行效果如下 

二维地图 + 地图标注

执行效果如下 

二维地图 + 地图边界

执行效果如下 

绘制点线面园 

执行效果如下 

卫星地图 + 地图标注 + 点线面园 

执行效果如下 

完 

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

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

相关文章

Rancher(v2.6.3)——Rancher部署Mysql(单机版)

[详细说明请查看Rancher部署Mysql说明文档]&#xff1a;https://gitee.com/WilliamWangmy/snail-knowledge/blob/master/Rancher/Rancher%E4%BD%BF%E7%94%A8%E6%96%87%E6%A1%A3.md#2rancher%E9%83%A8%E7%BD%B2mysql ps&#xff1a;如果觉得作者写的还行&#xff0c;能够满足您的…

nginx有哪些安装方法

Nginx 有多种安装方法&#xff0c;适用于不同的操作系统和使用场景。以下是几种常见的安装方式&#xff1a; 通过官方源码编译安装&#xff1a; 从Nginx官网下载源代码包。解压缩并进入源码目录。安装编译所需的依赖包&#xff0c;如pcre、zlib、openssl等。运行./configure配置…

前端canvas项目实战——简历制作网站(六):加粗、斜体、下划线、删除线(上)

目录 前言一、效果展示二、实现步骤1. 视图部分&#xff1a;实现用于切换字体属性的按钮2. 逻辑部分&#xff1a;点击按钮之后要做什么&#xff1f;3. 根据Textbox的属性实时更新按钮的状态 三、Show u the code后记 前言 上一篇博文中&#xff0c;我们实现了对文字的字体、字…

【系统设计】面试问题:设计 Spotify

目录 初始阶段:基础版本估计:数据计算高层设计**数据存储**SQL数据库结构把它们放在一起规模化阶段:5000 万用户、2 亿首歌曲引入 CDN扩展数据库:领导者-跟随者技术推荐超级课程: Docker快速入门到精通Kubernetes入门到大师通关课AWS云服务快速入门实战这是一道系统设计面…

优化选址问题 | 基于灰狼算法求解基站选址问题含Matlab源码

目录 问题代码问题 灰狼优化算法(Grey Wolf Optimizer, GWO)是一种基于自然界中灰狼群体狩猎行为的优化算法。这种算法通过模拟灰狼的社会等级和狩猎行为来寻找问题的最优解。 基站选址问题通常是一个多目标优化问题,涉及到覆盖范围、信号质量、成本等多个因素。使用灰狼算…

量化交易入门(十一)Python开发-数据结构

Python提供了几种内置的数据结构,可以用来存储和组织数据。以下是Python中常见的数据结构:列表&#xff0c;元组&#xff0c;字典&#xff0c;集合&#xff0c;字符串&#xff0c;栈&#xff0c;队列&#xff0c;树&#xff0c;图。我们将介绍这些数据结构&#xff0c;并举例说…

使用JMeter从JSON响应的URL参数中提取特定值

在使用Apache JMeter进行API测试时&#xff0c;我们经常需要从JSON格式的响应中提取特定字段的值。这可以通过使用JMeter内置的JSON提取器和正则表达式提取器来完成。以下是一个具体的例子&#xff0c;展示了如何从一个JSON响应中提取rowId的值&#xff0c;同时处理字符串终止符…

C#使用Poll/Select实现多路I/O复用

在实际的应用中&#xff0c;如果全部采用异步的操作来&#xff0c;会增加代码的复杂程度&#xff0c;某些时候使用Poll/Select来实现单线程多路的I/O复用会更合适一些 一、Poll 原型函数 public bool Poll ( int microSeconds, SelectMode mode ) 1&#xff1a;客户端 pri…

web服务架构

1 Web服务器&#xff08;如Nginx、Apache等&#xff09;和Web应用框架&#xff08;如Flask、Django等&#xff09; Web服务器&#xff08;如Nginx、Apache等&#xff09;和Web应用框架&#xff08;如Flask、Django等&#xff09;在Web应用开发和部署中扮演着不同的角色&#xf…

python turtle库简单应用题

三角形 import turtle for i in range(3):turtle.seth(i * 120)turtle.fd(100) turtle.done()矩形 import turtle angle 90 for x in range(4):turtle.fd(100)turtle.seth(angle)angle 90 turtle.done()五边形 import turtle angle 360/5 for x in range(5):turtle.fd(10…

软考中级 --网络工程师真题试卷 2023下半年

在EIGRP协议中&#xff0c;某个路由器收到了两条路径到达目标网络&#xff0c;路径1的带宽为100Mbps&#xff0c;延迟2ms&#xff0c;路径2的带宽为50Mbps&#xff0c;迟为4ms&#xff0c;如果EIGRP使用带宽和延迟的综合度量标准&#xff0c;那么该路由器选择的最佳路径是(D)。…

Codeforces Round 930 (Div. 2)(A,B,C,D)

比赛链接 C是个交互&#xff0c;D是个前缀和加二分。D还是很难写的。 A. Shuffle Party 题意&#xff1a; 您将得到一个数组 a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1​,a2​,…,an​ 。最初&#xff0c;每个 1 ≤ i ≤ n 1 \le i \le n 1≤i≤n 对应 a i i a_ii…

你用对const了吗?C++中const小结

const 修饰普通变量 表示变量的值不能被改变。下面两条语句(第2行和第3行)表示的意思一致。 int a; const int ca 42; //int const ca 42;const 修饰指针 指向常量的指针不能改变其指对象的值。第 5 行代码是错误的。 int a 42;const int * ip &a; int const * ipp…

win10 禁止谷歌浏览器自动更新(操作贼简单)

禁止谷歌浏览器自动更新 &#xff08;1&#xff09;修改 "C:\Windows\System32\drivers\etc\hosts 文件&#xff0c;在最后增加 127.0.0.1 update.googleapis.com&#xff08;2&#xff09;保存后&#xff0c;winr 快捷键&#xff0c;输入cmd &#xff0c;打开命令行 &am…

RK3588 rknpu2及rknn-toolkit2使用说明

RKNN模型推理共有四种方式&#xff1a; 第一种是借助RKNN-Toolkit2的功能在模拟NPU上运行RKNN模型并获取推理结果&#xff08;在PC端&#xff09; 第二种是借助RKNN-Toolkit2的功能, 将板子与PC连接&#xff0c;将RKNN模型分发到指定的NPU设备进行推理并获取推理结果&#xff0…

AJAX踩坑指南(知识点补充)

JWT JSON Web Token是目前最为流行的跨域认证解决方案 如何获取&#xff1a;在使用JWT身份验证中&#xff0c;当用户使用其凭据成功登录时&#xff0c;将返回JSON Web Token(令牌&#xff09; Token本质就是一个包含了信息的字符串 如何获取Token:登录成功之后&#xff0c;服务…

Springboot解决跨域问题方案总结(包括Nginx,Gateway网关等)

&#x1f3f7;️个人主页&#xff1a;牵着猫散步的鼠鼠 &#x1f3f7;️系列专栏&#xff1a;Java全栈-专栏 &#x1f3f7;️个人学习笔记&#xff0c;若有缺误&#xff0c;欢迎评论区指正 目录 前言 解决跨域问题方案 1.Spring Boot 中解决跨域 1.1 通过注解跨域 1.2 通…

什么是RabbitMQ的死信队列

RabbitMQ的死信队列&#xff08;Dead Letter Queue&#xff0c;简称DLQ&#xff09;是一种用于处理消息失败或无法路由的消息的机制。它允许将无法被正常消费的消息重新路由到另一个队列&#xff0c;以便稍后进行进一步处理、分析或排查问题。 当消息对立里面的消息出现以下几…

C语言经典面试题目(二十七)

1、什么是头文件&#xff1f;为什么在C语言中需要使用头文件&#xff1f; 头文件是C语言中的一种文件&#xff0c;通常以.h为文件扩展名&#xff0c;用于存放函数声明、宏定义、结构体声明等。在C语言中&#xff0c;头文件的主要作用是将程序的接口与实现分离开来&#xff0c;…

大数据的实时计算和离线计算你理解吗?

不管是实时计算还是离线计算&#xff0c;都有着同样的业务目标&#xff0c;那就是根据业务要求把数据源计算处理成业务需要的直接可用的数据结果。 如果把数据源比作是水龙头里的水&#xff0c;把数据计算比作是生产纯净水的过程&#xff1b;那么实时计算就是用一根水管接在水龙…