0

0

使用 OpenLayers 在自定义事件处理程序中触发 Map 事件

DDD

DDD

发布时间:2025-10-08 09:32:18

|

758人浏览过

|

来源于php中文网

原创

使用 openlayers 在自定义事件处理程序中触发 map 事件

在 OpenLayers 项目中,ol.interaction.Draw 提供了一种便捷的方式来进行地图上的绘制和测量。通常,该交互直接添加到 ol.Map 对象上,并在地图元素上进行操作。然而,在某些场景下,我们可能需要在非 OpenLayers Map 容器(例如一个独立的 HTML 元素)上进行测量操作,并希望 OpenLayers Map 上的测量结果能够同步更新。本文将介绍如何实现这一目标,并解决一些常见问题

使用 ol.interaction.Draw 进行测量

首先,我们需要创建一个 ol.interaction.Draw 实例,并将其添加到 ol.Map 对象中。

import Draw from 'ol/interaction/Draw';
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';

// 创建一个用于绘制的 VectorSource 和 VectorLayer
const source = new VectorSource();
const vector = new VectorLayer({
  source: source,
});

// 创建 Draw 交互
const draw = new Draw({
  source: source,
  type: 'Polygon', // 可以是 'Point', 'LineString', 'Polygon', 'Circle'
});

// 添加 Draw 交互到 Map 对象
const map = new ol.Map({
  target: 'map',
  layers: [
    // ... 其他图层
    vector
  ],
  view: new ol.View({
    center: [0, 0],
    zoom: 2
  })
});

map.addInteraction(draw);

在自定义事件处理程序中添加坐标

当在非 OpenLayers Map 容器上发生点击事件时,我们需要将坐标添加到 ol.interaction.Draw 中。appendCoordinates() 方法可以实现这个功能。

// 假设 container 是一个非 OpenLayers Map 的 HTML 元素
$(container).on("click.ol", () => {
  if (this.measureHandler.viewerClick === true) {
    this.lastCoord = ol.proj.transform([this.measureHandler.clickCoords[0], this.measureHandler.clickCoords[1]], "EPSG:4326", "EPSG:3857");

    if (measureType !== "Polygon") {
      this.coords.push(this.lastCoord);
    } else {
      if (this.coords.length <= 1) {
        this.coords.splice(0, 0, this.lastCoord);
        this.coords.push(this.lastCoord);
      } else {
        this.coords.splice(this.coords.length - 1, 0, this.lastCoord);
      }
    }

    this.draw.appendCoordinates([this.lastCoord]);
  }
});

模拟 pointermove 事件

为了实时更新测量结果,我们需要模拟 ol.Map 的 "pointermove" 事件。ol.interaction.Draw 内部有一个 handlePointerMove_() 方法,可以用来处理 pointermove 事件。我们需要创建一个 ol.MapBrowserEvent 对象,并将其传递给 handlePointerMove_() 方法。

杰易OA办公自动化系统6.0
杰易OA办公自动化系统6.0

基于Intranet/Internet 的Web下的办公自动化系统,采用了当今最先进的PHP技术,是综合大量用户的需求,经过充分的用户论证的基础上开发出来的,独特的即时信息、短信、电子邮件系统、完善的工作流、数据库安全备份等功能使得信息在企业内部传递效率极大提高,信息传递过程中耗费降到最低。办公人员得以从繁杂的日常办公事务处理中解放出来,参与更多的富于思考性和创造性的工作。系统力求突出体系结构简明

下载
$(container).on("mousemove.ol", (evt) => {
  // ... 获取坐标逻辑 ...

  const olEvt = {
    map: this.map,
    pixel: this.measureHandler.pixelObj,
    coordinate: this.lastCoord,
    originalEvent: {
      pointerType: "mouse"
    },
    frameState: this.map.frameState
  };

  this.draw.handlePointerMove_(olEvt);
});

处理圆形几何

appendCoordinates() 方法对于圆形几何可能无法正常工作。在这种情况下,我们需要在点击事件处理程序中添加额外的逻辑来处理圆形几何。

$(container).on("click.ol", () => {
  // ... 其他逻辑 ...

  if (measureType === "Circle") {
    if (this.clickCount === 0) {
      this.draw.appendCoordinates([this.lastCoord]);
      this.clickCount++;
    } else {
      this.draw.finishDrawing();
      this.clickCount = 0;
    }
  } else {
    this.draw.appendCoordinates([this.lastCoord]);
    this.clickCount++;
  }
});

完整示例

this.measureHandler.containers.forEach((container, nr) => {
  $(container).on("click.ol", () => {
    if (this.measureHandler.viewerClick === true) {
      this.lastCoord = ol.proj.transform([this.measureHandler.clickCoords[0], this.measureHandler.clickCoords[1]], "EPSG:4326", "EPSG:3857");

      if (measureType !== "Polygon") {
        this.coords.push(this.lastCoord);
      } else {
        if (this.coords.length <= 1) {
          this.coords.splice(0, 0, this.lastCoord);
          this.coords.push(this.lastCoord);
        } else {
          this.coords.splice(this.coords.length - 1, 0, this.lastCoord);
        }
      }

      if (measureType === "Circle") {
        if (this.measureHandler.activePlugins[nr] !== "Ortofoto" && this.measureHandler.activePlugins[nr] !== "Ukosne" && this.measureHandler.activePlugins[nr] !== "OSMPlugin") {
          if (this.clickCount === 0) {
            this.draw.appendCoordinates([this.lastCoord]);
            this.clickCount++;
          } else {
            this.draw.finishDrawing();
            this.clickCount = 0;
          }
        }
      } else {
        this.draw.appendCoordinates([this.lastCoord]);
        this.clickCount++;
      }
    }
  });

  $(container).on("mousemove.ol", (evt) => {
    this.maps[nr].removeLayer(this.drawLayer);
    if (nr === 0) {
      this.map2.removeLayer(this.drawLayer);
      this.map2.addLayer(this.drawLayer);
    } else {
      this.map.removeLayer(this.drawLayer);
      this.map.addLayer(this.drawLayer);
    }
    this.maps[nr].addInteraction(this.draw);

    this.lastCoord = ol.proj.transform([this.measureHandler.moveCoords[0], this.measureHandler.moveCoords[1]], "EPSG:4326", "EPSG:3857");

    if (measureType !== "Polygon") {
      this.coords.pop();
      this.coords.push(this.lastCoord);
    } else {
      if (this.coords.length <= 1) {
        this.coords.pop();
        this.coords.push(this.lastCoord);
      } else {
        this.coords.splice(this.coords.length - 2, 1, this.lastCoord);
      }
    }

    if (nr === 0) {
      olEvt = {
        map: this.map2,
        pixel: this.measureHandler.pixelObj,
        coordinate: this.lastCoord,
        originalEvent: {
          pointerType: "mouse"
        },
        frameState: this.map2.frameState
      };
    } else {
      olEvt = {
        map: this.map,
        pixel: this.measureHandler.pixelObj,
        coordinate: this.lastCoord,
        originalEvent: {
          pointerType: "mouse"
        },
        frameState: this.map.frameState
      };
    }
    this.draw.handlePointerMove_(olEvt);
  });

  $(container).on("dblclick.ol", () => {
    this.draw.removeLastPoint();
    this.draw.finishDrawing();
    this.clickCount = 0;
  });
});

注意事项

  • 确保正确转换坐标系,将自定义容器上的坐标转换为 OpenLayers Map 使用的坐标系。
  • 根据实际需求调整 ol.MapBrowserEvent 对象的属性,例如 pixel 和 frameState。
  • 在模拟 pointermove 事件时,需要频繁更新图层和交互,以保证测量结果的实时性。
  • 圆形几何的处理可能需要根据具体情况进行调整,例如使用不同的绘制方法或添加额外的逻辑。

总结

通过 appendCoordinates() 方法和模拟 pointermove 事件,我们可以在自定义事件处理程序中触发 OpenLayers Map 事件,实现非 OpenLayers Map 容器上的测量操作与 OpenLayers Map 上的测量结果同步更新。 这种方法扩展了 ol.interaction.Draw 的使用场景,使其能够更好地满足复杂的业务需求。

相关专题

更多
html版权符号
html版权符号

html版权符号是“©”,可以在html源文件中直接输入或者从word中复制粘贴过来,php中文网还为大家带来html的相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。

616

2023.06.14

html在线编辑器
html在线编辑器

html在线编辑器是用于在线编辑的工具,编辑的内容是基于HTML的文档。它经常被应用于留言板留言、论坛发贴、Blog编写日志或等需要用户输入普通HTML的地方,是Web应用的常用模块之一。php中文网为大家带来了html在线编辑器的相关教程、以及相关文章等内容,供大家免费下载使用。

655

2023.06.21

html网页制作
html网页制作

html网页制作是指使用超文本标记语言来设计和创建网页的过程,html是一种标记语言,它使用标记来描述文档结构和语义,并定义了网页中的各种元素和内容的呈现方式。本专题为大家提供html网页制作的相关的文章、下载、课程内容,供大家免费下载体验。

470

2023.07.31

html空格
html空格

html空格是一种用于在网页中添加间隔和对齐文本的特殊字符,被用于在网页中插入额外的空间,以改变元素之间的排列和对齐方式。本专题为大家提供html空格的相关的文章、下载、课程内容,供大家免费下载体验。

245

2023.08.01

html是什么
html是什么

HTML是一种标准标记语言,用于创建和呈现网页的结构和内容,是互联网发展的基石,为网页开发提供了丰富的功能和灵活性。本专题为大家提供html相关的各种文章、以及下载和课程。

2895

2023.08.11

html字体大小怎么设置
html字体大小怎么设置

在网页设计中,字体大小的选择是至关重要的。合理的字体大小不仅可以提升网页的可读性,还能够影响用户对网页整体布局的感知。php中文网将介绍一些常用的方法和技巧,帮助您在HTML中设置合适的字体大小。

505

2023.08.11

html转txt
html转txt

html转txt的方法有使用文本编辑器、使用在线转换工具和使用Python编程。本专题为大家提供html转txt相关的文章、下载、课程内容,供大家免费下载体验。

312

2023.08.31

html文本框代码怎么写
html文本框代码怎么写

html文本框代码:1、单行文本框【<input type="text" style="height:..;width:..;" />】;2、多行文本框【textarea style=";height:;"></textare】。

425

2023.09.01

PS使用蒙版相关教程
PS使用蒙版相关教程

本专题整合了ps使用蒙版相关教程,阅读专题下面的文章了解更多详细内容。

23

2026.01.19

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Bootstrap 5教程
Bootstrap 5教程

共46课时 | 2.9万人学习

AngularJS教程
AngularJS教程

共24课时 | 2.7万人学习

CSS教程
CSS教程

共754课时 | 20.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号