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

【openlayers框架学习】六:绘制点、圆、文字标注


文章目录

    • openlayers入门
      • 20 openlayers中的要素实现绘制点
      • 21 openlayers中使用canvas绘制圆
      • 22 地图点击事件实现标注功能(addFeature)
      • 23 openlayers内置circle绘制圆(style->image:CircleStyle)
      • 24 openlayers添加文字标注(Style->text)


openlayers入门

20 openlayers中的要素实现绘制点

//通过ol中提供的要素(feature)
//创建要素(点 线 面)并且将要素绘制到底图上
//1.创建一个点要素,并且在这个点要素绘制一个简单的icon,在武汉上绘制一个小的logo
const iconFeature = new Feature({geometry:new Point([114.25,30.59])
});
iconFeature.setStyle(new Style({image: new Icon({src:'/icon.jpeg',}),}),
);
const iconSource = new Vector({features:[iconFeature],
});
const IconLayer = new VectorLayer({source:iconSource,
});
map.addLayer(IconLayer);

21 openlayers中使用canvas绘制圆

//通过ol中提供的要素(feature)
//创建要素(点 线 面)并且将要素绘制到底图上
//1.创建一个点要素,并且在这个点要素绘制一个简单的icon,在武汉上绘制一个小的logo
const iconFeature = new Feature({geometry:new Point([114.25,30.59])
});
const canvas = document.createElement("canvas");
canvas.width = 32;
canvas.height = 32;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "red";
ctx.beginPath();
ctx.arc(16,16,8,0,2*Math.PI);
ctx.fill();
iconFeature.setStyle(new Style({image: new Icon({img:canvas,// size:[160,160], //调整圆的大小,与canvas.width和canvas.geight作用相同// width:160,// height:160,}),}),
);
// iconFeature.setStyle(
//     new Style({
//         image: new Icon({
//             src:'/icon.jpeg',
//         }),
//     }),
// );
const iconSource = new Vector({features:[iconFeature],
});
const IconLayer = new VectorLayer({source:iconSource,
});
map.addLayer(IconLayer);

22 地图点击事件实现标注功能(addFeature)

main.js

import './style.css';
import Map from 'ol/Map';
import 'ol/ol.css';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import { XYZ } from 'ol/source';
import VectorLayer from 'ol/layer/Vector.js';
import Style from 'ol/style/Style';
import Feature from 'ol/Feature';
import { Point } from 'ol/geom';
import { Vector } from "ol/source";
import Icon from 'ol/style/Icon';
const center = [114.25, 30.59];
const view = new View({center: center,zoom: 4,projection: "EPSG:4326",
});
//城市矢量地图-高德地图瓦片
const gaodeSource = new XYZ({url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
});
const gaodeLayer = new TileLayer({source: gaodeSource,
});
const map = new Map({target: "map",view: view,layers: [gaodeLayer],
});
//通过ol中提供的要素(feature)
//创建要素(点 线 面)并且将要素绘制到底图上
//1.创建一个点要素,并且在这个点要素绘制一个简单的icon,在武汉上绘制一个小的logo
const iconFeature = new Feature({geometry:new Point([114.25,30.59])
});
const canvas = document.createElement("canvas");
canvas.width = 32;
canvas.height = 32;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "red";
ctx.beginPath();
ctx.arc(16,16,8,0,2*Math.PI);
ctx.fill();
iconFeature.setStyle(new Style({image: new Icon({img:canvas,// width:160,// height:160,}),}),
);
const iconSource = new Vector({features:[iconFeature],
});
const IconLayer = new VectorLayer({source:iconSource,
});
map.addLayer(IconLayer);
map.on('click',function(e){const coordinate = e.coordinate;const iconFeature = new Feature({geometry:new Point(coordinate),});iconFeature.setStyle(new Style({image: new Icon({img:canvas,width:30,height:30,}),}),);iconSource.addFeature(iconFeature);
});

23 openlayers内置circle绘制圆(style->image:CircleStyle)

import CircleStyle from 'ol/style/Circle.js';
import Fill from 'ol/style/Fill';
map.on('click',function(e){const coordinate = e.coordinate;const iconFeature = new Feature({geometry:new Point(coordinate),});iconFeature.setStyle(new Style({image: new CircleStyle({fill: new Fill({color:"green",}),radius: 10,}),}),);iconSource.addFeature(iconFeature);
});

24 openlayers添加文字标注(Style->text)

import Text from 'ol/style/Text.js';
map.on('click',function(e){const coordinate = e.coordinate;const iconFeature = new Feature({geometry:new Point(coordinate),});iconFeature.setStyle(new Style({image: new CircleStyle({fill: new Fill({color:"green",}),radius: 10,}),text: new Text({text:'圆',fill: new Fill({color:'black',}),// offsetX: -10,offsetY: -15,}),}),);iconSource.addFeature(iconFeature);
});
http://www.dtcms.com/a/313659.html

相关文章:

  • 关于vllm【常见问题解决方案】
  • XtraBackup备份与恢复
  • Python 程序设计讲义(61):Python 的函数——变量的作用域
  • 【运维基础】Linux 硬盘分区管理
  • [Oracle] DUAL数据表
  • [自动化Adapt] 录制引擎 | iframe 穿透 | NTP | AIOSQLite | 数据分片
  • 第二节 YOLOv5参数
  • Python 程序设计讲义(59):Python 的函数——labmda函数(匿名函数)
  • 四、驱动篇-HDF驱动介绍2
  • sublime 乱码问题
  • JavaEE文件泄露与修复方案
  • Linux | i.MX6ULL移植 Gdb+Gdbserver 调试(第十四章)
  • 深入解析 Linux Kernel 中的设备树:使用、修改与实际应用
  • 经典文献阅读之--ViNT(视觉导航的基础模型)
  • 《汇编语言:基于X86处理器》第11章 MS-Windows编程(3)
  • 8.3 Java Web(JavaScript P15-P28)
  • Leetcode——365. 水壶问题
  • 决策树模型知识点整理:从原理到实战(含可视化与调参)
  • [硬件电路-134]:模拟电路 - 运算放大器常见运算:两模拟信号相加、相减、单模拟信号的积分、微分...
  • HTTPS的概念和工作过程
  • Ollama模型库模型下载慢完美解决(全平台)
  • 模型学习系列之参数
  • pytorch深度学习全流程:以简易数据、模型介绍
  • linux火焰图
  • vuhub Noob靶场攻略
  • 雪花算法重复id问题
  • Maxscript在选择的可编辑多边形每个面上绘制一个内部圆形
  • 自动驾驶中的传感器技术19——Camera(10)
  • OS21.【Linux】环境变量
  • CMake 命令行参数完全指南(5)