新闻中心
使用 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
基于Intranet/Internet 的Web下的办公自动化系统,采用了当今最先进的PHP技术,是综合大量用户的需求,经过充分的用户论证的基础上开发出来的,独特的即时信息、短信、电子邮件系统、完善的工作流、数据库安全备份等功能使得信息在企业内部传递效率极大提高,信息传递过程中耗费降到最低。办公人员得以从繁杂的日常办公事务处理中解放出来,参与更多的富于思考性和创造性的工作。系统力求突出体系结构简明
0
查看详情
$(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 的使用场景,使其能够更好地满足复杂的业务需求。
以上就是使用 OpenLayers 在自定义事件处理程序中触发 Map 事件的详细内容,更多请关注其它相关文章!
# 拖拽
# 云浮抖音营销推广招聘
# 翠峦网站建设
# 奉化seo优化要多少钱
# 奉贤区网站建设代理
# 新余网站推广优化
# 乐安seo优化公司
# 增城百度seo费用
# 网站优化外包是什么
# 龙华软文营销推广
# bdk营销推广服务商
# 容器内
# 同步更新
# 是一个
# html
# 服务端
# 图层
# 如何实现
# 创建一个
# 办公自动化系统
# 自定义
# 点击事件
# 常见问题
# win
# ai
# app
# go
相关栏目:
【
科技资讯46185 】
【
网络学院92790 】
相关推荐:
深入理解J*a合成构造器:何时以及为何阻止其生成
PHP URL参数传递与500错误调试指南
想当下一个《2077》?《心之眼》Steam评价升至"多半好评"
C++如何生成随机数_C++ random库使用方法与范围设置
优化 Jest 模拟:强制未实现函数抛出错误以提升测试效率
解决 MongoDB 聚合查询中对象数组 _id 匹配问题
快手网页版在线登录 快手网页版官网入口快速访问
利用5118提升短视频内容效果_5118短视频关键词优化方法
PHP中获取MongoDB服务器运行时间(Uptime)的专业指南
Go语言HTML解析:利用Goquery精准获取指定元素内容
修复二维数组索引越界异常:一维循环到二维坐标的正确映射
如何在CSS中使用浮动制作导航栏_float实现水平菜单
夸克浏览器桌面版同步不了书签怎么处理 夸克浏览器跨设备同步异常解决方案
创客贴用户入口官网登录 创客贴网页版电脑版系统
J*aScript生成器_j*ascript异步迭代
Django表单验证失败时保留用户输入数据的最佳实践
CSS实现侧边栏导航项全宽圆角悬停背景效果
谷歌google账号注册详细步骤 谷歌账号注册官方教程
如何在更新Composer依赖后自动运行测试_使用post-update-cmd钩子触发PHPUnit
小红书网页版入口链接分享 小红书官网直接进
win11开机启动修复循环怎么办 Win11无法进入系统高级启动解决方法【修复】
海棠电脑版入口_通过电脑访问海棠官网阅读
深入理解rpy2中的类型转换:优化Python对象到R矩阵的映射
Win11怎么查看显卡显存 Win11显示适配器属性及专用视频内存查询
谷歌浏览器如何快速清除某个网站的数据_Chrome网站缓存清理方法
电脑IP地址怎么查 查看本机IP地址的几种方法
《铁拳8》黑皮辣妹新实机:元气满满的18岁少女!
痛风发作了怎么办? 快速止痛和后期饮食调理
机构:以往存储涨价周期小米利润率实际上有所改善 能转嫁给消费者等
C++如何实现一个智能指针_手动实现C++ shared_ptr的引用计数功能
windows10怎么查看硬盘序列号_windows10硬盘id查询命令
Mac终端命令大全_Mac常用Terminal指令速查
UE5.7引擎表现爆炸优化无敌!5090跑4K稳定60FPS
zookeeper 都有哪些功能?
Django模型中自动计算可用余额的实现方法
深入理解字体排版:Adobe光学字偶距与CSS字偶距的差异与实现
处理动态列数据:J*a ArrayList的正确初始化与字符累加教程
网站内容防复制粘贴的实现策略与局限性
C++如何连接MySQL数据库_C++使用Connector/C++操作MySQL数据库教程
蛙漫画网页版全站入口 蛙漫热门作品免费浏览
sublime怎么设置启动时打开的窗口_sublime会话管理与热退出
J*a里如何使用N*igableMap进行导航操作_可导航Map操作技巧解析
新手怎么开始学化妆 零基础化妆入门教程
c++如何实现一个简单的ECS框架_c++数据驱动设计与游戏开发
2026春节假期时间安排 2026春节假日查询
消息称三星明年 2 月正式发布 HBM4,与 SK 海力士同台竞技
苹果手机如何防止被恶意App追踪
AO3最新可访问网址 Archive of Our Own官方在线入口
word邮件合并后日期格式不对怎么改_Word邮件合并日期格式修改方法
ExcelARRAYTOTEXT函数怎么自定义分隔符输出数组文本_ARRAYTOTEXT实现动态生成SQL语句


2025-10-08
浏览次数:次
返回列表
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;
});
});