
在 OpenLayers 项目中,ol.interaction.Draw 提供了一种便捷的方式来进行地图上的绘制和测量。通常,该交互直接添加到 ol.Map 对象上,并在地图元素上进行操作。然而,在某些场景下,我们可能需要在非 OpenLayers Map 容器(例如一个独立的 HTML 元素)上进行测量操作,并希望 OpenLayers Map 上的测量结果能够同步更新。本文将介绍如何实现这一目标,并解决一些常见问题。
首先,我们需要创建一个 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]);
}
});为了实时更新测量结果,我们需要模拟 ol.Map 的 "pointermove" 事件。ol.interaction.Draw 内部有一个 handlePointerMove_() 方法,可以用来处理 pointermove 事件。我们需要创建一个 ol.MapBrowserEvent 对象,并将其传递给 handlePointerMove_() 方法。
CA.LA
第一款时尚产品在线设计平台,服装设计系统
86
查看详情
$(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;
});
});通过 appendCoordinates() 方法和模拟 pointermove 事件,我们可以在自定义事件处理程序中触发 OpenLayers Map 事件,实现非 OpenLayers Map 容器上的测量操作与 OpenLayers Map 上的测量结果同步更新。 这种方法扩展了 ol.interaction.Draw 的使用场景,使其能够更好地满足复杂的业务需求。
以上就是使用 OpenLayers 在自定义事件处理程序中触发 Map 事件的详细内容,更多请关注其它相关文章!
# 并在
# 关键词排名快速公司每淹云速捷牛X彳
# 网站关键词排名优化新闻
# 网络营销推广平台去哪找
# 深圳推广营销咨询热线
# 临沧营销推广公司招聘电话
# 啄木鸟的营销推广思路
# 青海seo优化什么价格
# 宣传册推广好还是网站方便
# 合肥关键词快速排名系统
# seo1 在线播放
# 相关文章
# 我们可以
# 并将其
# html
# 同步更新
# 是一个
# 图层
# 创建一个
# 表单
# 自定义
# 点击事件
# 常见问题
# win
# ai
# app
# go
相关栏目:
【
Google疑问12 】
【
Facebook疑问10 】
【
优化推广96088 】
【
技术知识133117 】
【
IDC资讯59369 】
【
网络运营7196 】
【
IT资讯61894 】
相关推荐:
《kimi智能助手》制作ppt教程
跨语言测试实践:使用Python Selenium测试现有J*a Web项目
POKI小游戏在线免费入口链接 POKI小游戏无下载秒玩玩
AO3官方镜像链接 | 最新防走失网址永久收藏
视频转蓝光m2ts格式
c++中的const关键字用法大全_c++ const正确使用指南
《猎聘》筛选猎头岗位方法
秋风萧瑟洪波涌起中的萧瑟指的是什么
小米倒班助手添加日历提醒
PHP 4 函数中引用参数的默认值限制与解决方案
聚水潭ERP后台管理系统登录 聚水潭ERP官方登录通道
windows10怎么设置电源按钮_windows10按下电源键功能修改
抖音如何解除|直播|权限绑定_抖音关闭并解绑|直播|功能的方法
PHP中获取HTTP响应状态消息:方法与限制
《飞猪旅行》购买汽车票方法
小红书网页版在线直达 小红书网页版免费登录入口
《微信》视频号原创声明开启方法
优化2xN网格最大路径和的动态规划算法实践
虫虫漫画绿色安全入口_虫虫漫画绿色安全入口安全看漫画
firefox火狐浏览器最新官网主页_ firefox火狐浏览器平台入口直达官方链接
键盘测试软件哪个好_键盘故障检测工具推荐
steam缓存文件在哪儿_steam缓存文件的路径查找方法与结构说明
Win11怎么录屏_Windows 11自带Xbox Game Bar录制视频
有道AI翻译入口 智能写作官方网站入口
PointNet++语义分割模型中类别变更引发的断言错误及标签处理策略
抖音官网入口快速访问 抖音网页版账号注册解析
《气泡星球》兑换码礼包大全
微星主板BIOS怎么调整内存时序_内存参数手动优化BIOS设置教程
rabbitmq 持久化有什么缺点?
OpenWeatherMap API:通过城市名称获取天气预报数据指南
德邦快递收费标准详解
抖音网页版官方链接 抖音网页版官网链接入口
虫虫助手如何更新游戏
TikTok搜索结果不显示怎么办 TikTok搜索刷新与优化方法
SQLAlchemy 2.0 与 Pydantic 模型类型安全集成指南
C++如何将字符串转换为大写或小写_C++ transform函数的使用技巧
告别阻塞等待:如何使用GuzzlePromises优雅处理PHP异步操作,提升应用响应速度
快递优选如何查优选物流_快递优选专属物流渠道查询与配送时效
三角洲行动2025年9月10日摩斯密码分享
Three.js中动态更换3D模型纹理的教程
Python csv 模块处理非字符串数据:列表写入 CSV 文件的机制解析
vivo云服务一直提示空间不足怎么办 怎么办vivo云服务老是提示空间不足
《腾讯相册管家》注销账号方法
附近酒吧怎么找?
12306不能订票的时间段是固定的吗? | 节假日购票时间有无变化
Go Goroutine调度与并发执行深度解析
J*aScript调试技巧_性能分析与内存快照
J*aScript实现下拉菜单驱动的动态表格数据展示
byrutor直接访问入口 byrutor官方游戏库
Pandas中基于动态偏移量实现DataFrame列值位移的策略
2025-10-08
运城市盐湖区信雨科技有限公司是一家深耕海外推广领域十年的专业服务商,作为谷歌推广与Facebook广告全球合作伙伴,聚焦外贸企业出海痛点,以数字化营销为核心,提供一站式海外营销解决方案。公司凭借十年行业沉淀与平台官方资源加持,打破传统外贸获客壁垒,助力企业高效开拓全球市场,成为中小企业出海的可靠合作伙伴。