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

五十四、openlayers官网示例LineString Arrows解析——在地图上绘制箭头

官网demo地址:

LineString Arrows 

 这篇介绍了在地图上绘制箭头。

创建一个矢量数据源,将其绑定为draw的数据源并展示在矢量图层上。

  const source = new VectorSource();const vector = new VectorLayer({source: source,style: styleFunction,});map.addInteraction(new Draw({source: source,type: "LineString",}));

绘制直线时,通过style函数将直线的末端添加箭头图标。通过forEachSegment 函数拿到箭头的起点和终点坐标,使用 Math.atan2计算出箭头图标的旋转角度。 

const styleFunction = function (feature) {const geometry = feature.getGeometry();const styles = [new Style({stroke: new Stroke({color: "#ffcc33",width:2,}),}),];geometry.forEachSegment(function (start, end) {const dx = end[0] - start[0];const dy = end[1] - start[1];const rotation = Math.atan2(dy, dx);styles.push(new Style({geometry: new Point(end),image: new Icon({src: "https://openlayers.org/en/latest/examples/data/arrow.png",anchor: [0.75, 0.5],rotateWithView: true,rotation: -rotation,}),}));});return styles;};

完整代码:

<template><div class="box"><h1>LineString Arrows</h1><div id="map" class="map"></div></div>
</template><script>
import StadiaMaps from "ol/source/StadiaMaps.js";
import Draw from "ol/interaction/Draw.js";
import Map from "ol/Map.js";
import Point from "ol/geom/Point.js";
import View from "ol/View.js";
import { Icon, Stroke, Style } from "ol/style.js";
import { OSM, Vector as VectorSource } from "ol/source.js";
import { Tile as TileLayer, Vector as VectorLayer } from "ol/layer.js";
import { get } from "ol/proj.js";
export default {name: "",components: {},data() {return {map: null,};},computed: {},created() {},mounted() {const layer = new TileLayer({source: new StadiaMaps({layer: "stamen_terrain_background",}),});const source = new VectorSource();const styleFunction = function (feature) {const geometry = feature.getGeometry();const styles = [new Style({stroke: new Stroke({color: "#ffcc33",width:2,}),}),];geometry.forEachSegment(function (start, end) {const dx = end[0] - start[0];const dy = end[1] - start[1];const rotation = Math.atan2(dy, dx);styles.push(new Style({geometry: new Point(end),image: new Icon({src: "https://openlayers.org/en/latest/examples/data/arrow.png",anchor: [0.75, 0.5],rotateWithView: true,rotation: -rotation,}),}));});return styles;};const vector = new VectorLayer({source: source,style: styleFunction,});const extent = get("EPSG:3857").getExtent().slice();extent[0] += extent[0];extent[2] += extent[2];const map = new Map({layers: [layer, vector],target: "map",view: new View({center: [-11000000, 4600000],zoom: 4,extent,}),});map.addInteraction(new Draw({source: source,type: "LineString",}));},methods: {},
};
</script><style lang="scss" scoped>
#map {width: 100%;height: 500px;
}
.box {height: 100%;
}</style>

 

http://www.lryc.cn/news/378941.html

相关文章:

  • 内核学习——3、自旋锁的作用及其实现
  • 恒昌公益第五所“云杉校园”于湖南怀化正式揭牌
  • 番外篇 | YOLOv8算法解析和实战应用:车辆检测 + 车辆追踪 + 行驶速度计算
  • 【React】useState 的原理
  • 从二元一次方程组到二阶行列式再到克拉默法则
  • 示例:WPF中绑定枚举到ComboBox想显示成中文或自定义名称如何实现
  • 嵌入式系统软件架构设计方法
  • 【面试题】风险评估和应急响应的工作流程
  • Vue70-路由的几个注意点
  • Aidlux 1.4 部署Nextcloud 2024.6实录 没成功
  • 网络与协议安全复习 - 电子邮件安全
  • Python里的序列化是什么?
  • 自动抓取服务器功耗
  • 服务器接收苹果订阅通知
  • 2024年旅游与经济发展国际会议(ICTED 2024)
  • 【NLP练习】Transformer实战-单词预测
  • 使用Lua脚本保证原子性的Redis分布式锁实现
  • 什么是nginx到底怎么配置,什么是网关到底怎么配置?
  • 轻量级服务器内存不够编译的情况解决方案(以安装Ta-Lib库为例)
  • 学校校园考场电子钟,同步授时,助力考场公平公正-讯鹏科技
  • MySQL存储管理(一):删数据
  • 深度剖析现阶段的多模态大模型做不了医疗
  • Zabbix 监控 Kubernetes 集群
  • 网上预约就医取号系统
  • 概念描述——TCP/IP模型中的两个重要分界线
  • ECharts,拿来吧你!
  • 【DICOM】BitsAllocated字段值为8和16时区别
  • 【MySQL】 -- 事务
  • c#调用c++生成的dll,c++端使用opencv, c#端使用OpenCvSharp, 返回一张图像
  • 【Android面试八股文】你能说一说View绘制流程与自定义View注意点吗?