openlayer选择移动图标

我们通常使用OpenLayers的内置交互,但有时需要扩展功能。ol-ext是一个OpenLayers的扩展库,其中包含了一些额外的交互和控件。ol-ext 库中的 Transform 交互组件,用于在地图上对要素进行交互式变换(旋转、缩放、平移)。

  1. 首先,确保已经安装了ol-ext。如果没有,可以通过npm安装
js 复制代码
npm install ol-ext
  1. 导入ExtTransform。
  2. 创建一个ExtTransform交互对象,并可以配置一些选项。
  3. 将该交互添加到地图中。
js 复制代码
import ExtTransform from 'ol-ext/interaction/Transform'

 this.transform = new ExtTransform({
     // 交互模式
      enableRotatedTransform: true,    // 启用旋转后的变换
      translate: true,                 // 启用平移手柄
      stretch: true,                   // 启用拉伸(角手柄)
      scale: true,                     // 启用缩放(边手柄)
      rotate: true,                    // 启用旋转手柄
  
      // 行为控制
      keepAspectRatio: false,          // 拉伸时保持宽高比(按住Shift键)
      translateFeature: false,         // 点击要素内部时是否平移
      translateAnchor: false,          // 是否平移锚点
      noFlip: false,                   // 禁止几何图形翻转
  
      // 样式
      style: new Style({              // 控制点的样式
        image: new Circle({
          radius: 6,
          fill: new Fill({
            color: '#ff0000'
          }),
          stroke: new Stroke({
            color: '#ffffff',
            width: 2
          })
        })
      }),
  
      // 过滤器
      filter: function(feature, layer) {
        // 返回 true 表示该要素可变换
        return feature.get('transformable') === true
      }
   })
   this.map.addInteraction(this.transform)

   this.transform.setActive(false)

  //移动结束
   this.transform.on('translateend', (e)=>{
     console.log(e.target.coordinate_);
    
     // 获取变换后的几何图形
      const geometry = e.feature.getGeometry()
      console.log('变换后的坐标', geometry.getCoordinates())
  });
  
 
    // 删除变换控制点
    this.transform.on('stop', function(e) {
      console.log('变换交互停止')
    })
  

ExtTransform交互提供了多种事件,比如正在变换('transforming') 变换过程中(transform)、变换结束('transformend'),我们可以监听这些事件来执行一些操作(比如更新要素的属性或保存状态)。

ExtTransform可以动态控制变换

kotlin 复制代码
// 启用/禁用特定变换类型
this.transform.setTranslate(true)
this.transform.setScale(false)
this.transform.setRotate(true)
this.transform.setStretch(false)

// 设置要变换的要素
const features = this.vectorSource.getFeatures()
this.transform.setFeatures(features) // 所有要素
// 或
this.transform.setFeatures([feature1, feature2]) // 特定要素

// 清空当前变换的要素
this.transform.clear()

// 激活/停用交互
this.transform.setActive(true)  // 激活
this.transform.setActive(false) // 停用

// 设置锚点位置(默认在要素中心)
this.transform.setAnchor([x, y])
相关推荐
夏幻灵10 小时前
HTML5里最常用的十大标签
前端·html·html5
Mr Xu_10 小时前
Vue 3 中 watch 的使用详解:监听响应式数据变化的利器
前端·javascript·vue.js
未来龙皇小蓝10 小时前
RBAC前端架构-01:项目初始化
前端·架构
程序员agions11 小时前
2026年,微前端终于“死“了
前端·状态模式
万岳科技系统开发11 小时前
食堂采购系统源码库存扣减算法与并发控制实现详解
java·前端·数据库·算法
程序员猫哥_11 小时前
HTML 生成网页工具推荐:从手写代码到 AI 自动生成网页的进化路径
前端·人工智能·html
龙飞0511 小时前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
我爱加班、、11 小时前
Websocket能携带token过去后端吗
前端·后端·websocket
AAA阿giao11 小时前
从零拆解一个 React + TypeScript 的 TodoList:模块化、数据流与工程实践
前端·react.js·ui·typescript·前端框架
杨超越luckly11 小时前
HTML应用指南:利用GET请求获取中国500强企业名单,揭秘企业增长、分化与转型的新常态
前端·数据库·html·可视化·中国500强