Vue2+OpenLayers给2个标点Feature分别添加独立的点击事件(提供Gitee源码)

前言:之前开发都是将所有的点位存放在一个图层上面,并统一赋予它们相同的点击事件,如果其中某些点的点击事件不一样呢,这种问题如何解决呢,本篇博客就是解决这个通点。 

目录

一、案例截图

二、安装OpenLayers库

三、代码实现

3.1、实现思路

3.2、添加两个点

3.3、初始化点击事件

3.4、完整代码

四、Gitee源码 


一、案例截图

点击2个点,可以看到控制台,它们点击事件打印出来的信息是不一样的,这就是我们需要实现的效果。

二、安装OpenLayers库

npm install ol

三、代码实现

3.1、实现思路

其实就是为这两个点分别新建两个图层,先初始化变量。

关键代码:

data() {return {map:null,overLay:null,//图层1firstLayer: new VectorLayer({source: new VectorSource(),}),//图层2secLayer: new VectorLayer({source: new VectorSource(),}),}
},

3.2、添加两个点

先封装好生成点位的方法,传入经纬度信息就可以生成点位了,传入的格式例如[116.222222,38.222222]。

关键代码:

/*** 根据经纬度坐标添加自定义图标 支持base64*/
addPoints(coordinate) {// 创建feature要素,一个feature就是一个点坐标信息let feature = new Feature({geometry: new Point(coordinate),});// 设置要素的图标feature.setStyle(new Style({// 设置图片效果image: new Icon({src: 'http://api.tianditu.gov.cn/img/map/markerA.png',// anchor: [0.5, 0.5],scale: 1,}),}),);return feature;
},

在地图初始化以后,调用上面封装好的方法新增两个点,并分别添加到之前创建好的两个图层上。

关键代码:

//分别添加2个图层
this.map.addLayer(this.firstLayer);
this.map.addLayer(this.secLayer);
//添加第一个点
let firstFeature = this.addPoints([118.958412, 32.119130]);
this.firstLayer.getSource().addFeature(firstFeature);
//添加第二个点
let secFeature = this.addPoints([118.948627, 32.120428]);
this.secLayer.getSource().addFeature(secFeature);

3.3、初始化点击事件

实现思路:在地图上监听用户的单击事件,获取用户点击的坐标后,检查这一区域是否存在于第一个或第二个图层(VectorLayer)的特征(Feature)中。如果找到特征,则输出其几何体的坐标;如果没有找到特征,则输出提示信息,表明用户点击了地图的空白处。

关键代码:

initPointEvent(){let _that = this;// 添加点击事件this.map.on('singleclick', (event) => {// 获取点击的坐标const pixel = this.map.getEventPixel(event.originalEvent);const firstFeature = this.map.forEachFeatureAtPixel(pixel, (o) => {let features = _that.firstLayer.getSource().getFeatures();let find;features.forEach(feature => {if(feature === o){find = feature;}});return find;});const secFeature = this.map.forEachFeatureAtPixel(pixel, (o) => {let features = _that.secLayer.getSource().getFeatures();let find;features.forEach(feature => {if(feature === o){find = feature;}});return find;});if (firstFeature) {// 获取 Feature 的几何体const geometry = firstFeature.getGeometry();// 获取坐标const coordinates = geometry.getCoordinates();console.log("第一个图层中点位的点击事件被触发了,当前经纬度:"+coordinates)}else if(secFeature){// 获取 Feature 的几何体const geometry = secFeature.getGeometry();// 获取坐标const coordinates = geometry.getCoordinates();console.log("第二个图层中点位的点击事件被触发了,当前经纬度:"+coordinates)}else {console.log("地图的空白处被点击了")}});

3.4、完整代码

<template><div><div id="map-container"></div></div>
</template>
<script>
import {Feature, Map, View} from 'ol'
import { Tile as TileLayer } from 'ol/layer'
import { get } from 'ol/proj';
import { getWidth, getTopLeft } from 'ol/extent'
import { WMTS } from 'ol/source'
import WMTSTileGrid from 'ol/tilegrid/WMTS'
import { defaults as defaultControls} from 'ol/control';
import {Point} from "ol/geom";
import VectorLayer from "ol/layer/Vector";
import VectorSource from "ol/source/Vector";
import {Icon, Style} from "ol/style";export const projection = get("EPSG:4326");
const projectionExtent = projection.getExtent();
const size = getWidth(projectionExtent) / 256;
const resolutions = [];
for (let z = 0; z < 19; ++z) {resolutions[z] = size / Math.pow(2, z);
}export default {data() {return {map:null,overLay:null,//图层1firstLayer: new VectorLayer({source: new VectorSource(),}),//图层2secLayer: new VectorLayer({source: new VectorSource(),}),}},mounted(){this.initMap() // 加载矢量底图},methods:{initMap() {const KEY = '你申请的KEY'this.map = new Map({target: 'map-container',layers: [// 底图new TileLayer({source: new WMTS({url: `http://t{0-6}.tianditu.com/vec_c/wmts?tk=${KEY}`,layer: 'vec', // 矢量底图matrixSet: 'c', // c: 经纬度投影 w: 球面墨卡托投影style: "default",crossOrigin: 'anonymous', // 解决跨域问题 如无该需求可不添加format: "tiles", //请求的图层格式,这里指定为瓦片格式wrapX: true, // 允许地图在 X 方向重复(环绕)tileGrid: new WMTSTileGrid({origin: getTopLeft(projectionExtent),resolutions: resolutions,matrixIds: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15','16','17','18']})})}),// 标注new TileLayer({source: new WMTS({url: `http://t{0-6}.tianditu.com/cva_c/wmts?tk=${KEY}`,layer: 'cva', //矢量注记matrixSet: 'c',style: "default",crossOrigin: 'anonymous',format: "tiles",wrapX: true,tileGrid: new WMTSTileGrid({origin: getTopLeft(projectionExtent),resolutions: resolutions,matrixIds: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15','16','17','18']})})})],view: new View({center: [118.958366,32.119577],projection: projection,zoom: 12,maxZoom: 17,minZoom: 1}),//加载控件到地图容器中controls: defaultControls({zoom: false,rotate: false,attribution: false})});//分别添加2个图层this.map.addLayer(this.firstLayer);this.map.addLayer(this.secLayer);//添加第一个点let firstFeature = this.addPoints([118.958412, 32.119130]);this.firstLayer.getSource().addFeature(firstFeature);//添加第二个点let secFeature = this.addPoints([118.948627, 32.120428]);this.secLayer.getSource().addFeature(secFeature);this.initPointEvent();},/*** 根据经纬度坐标添加自定义图标 支持base64*/addPoints(coordinate) {// 创建feature要素,一个feature就是一个点坐标信息let feature = new Feature({geometry: new Point(coordinate),});// 设置要素的图标feature.setStyle(new Style({// 设置图片效果image: new Icon({src: 'http://api.tianditu.gov.cn/img/map/markerA.png',// anchor: [0.5, 0.5],scale: 1,}),}),);return feature;},initPointEvent(){let _that = this;// 添加点击事件this.map.on('singleclick', (event) => {// 获取点击的坐标const pixel = this.map.getEventPixel(event.originalEvent);const firstFeature = this.map.forEachFeatureAtPixel(pixel, (o) => {let features = _that.firstLayer.getSource().getFeatures();let find;features.forEach(feature => {if(feature === o){find = feature;}});return find;});const secFeature = this.map.forEachFeatureAtPixel(pixel, (o) => {let features = _that.secLayer.getSource().getFeatures();let find;features.forEach(feature => {if(feature === o){find = feature;}});return find;});if (firstFeature) {// 获取 Feature 的几何体const geometry = firstFeature.getGeometry();// 获取坐标const coordinates = geometry.getCoordinates();console.log("第一个图层中点位的点击事件被触发了,当前经纬度:"+coordinates)}else if(secFeature){// 获取 Feature 的几何体const geometry = secFeature.getGeometry();// 获取坐标const coordinates = geometry.getCoordinates();console.log("第二个图层中点位的点击事件被触发了,当前经纬度:"+coordinates)}else {console.log("地图的空白处被点击了")}});}}
}
</script>
<style scoped>#map-container {width: 100%;height: 100vh;
}</style>

四、Gitee源码 

地址:Vue2+OpenLayers给2个标点Feature分别添加独立的点击事件

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/67315.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

山石防火墙命令行配置示例

现网1台山石SG6000防火墙&#xff0c;配置都可以通过GUI实现。 但有一些配置在命令行下配置效率更高&#xff0c;比如在1个已有策略中添加1个host或端口。 下面的双引号可以不加 1 创建服务 1.1 单个端口 service "tcp-901"tcp dst-port 901 1.2 端口范围 servi…

李宏毅机器学习课程笔记03 | 类神经网络优化技巧

文章目录 类神经网络优化技巧局部最小值local minima 与 鞍点saddle pointSaddle Point 的情况更常见 Tips for training&#xff1a;Batch and MomentumSmall Batch vs Large Batch回顾&#xff1a;optimization优化 找到参数使L最小问题&#xff1a;为什么要用Batch&#xff…

K8s 集群 IP 地址管理指南(K8s Cluster IP Address Management Guide)

K8s 集群 IP 地址管理指南 概述 你是否在小型初创公司或大型企业工作&#xff0c;并正在为公司评估 Kubernetes&#xff1f;你可能正在考虑运行十几个或更多的 Kubernetes (K8s) 集群。你期望每个集群支持几百个 K8s 节点&#xff0c;每个节点可能有 50 到 100 个 K8s Pod。这…

鸿蒙-点击Notification通知并打开App的具体页面

意图通知 获取router事件中传递参数并跳转 目前点击通知消息打开应用的指定页面&#xff0c;通过为通知添加行为意图的方式。也就是在wants的parameters中设置自定义参数&#xff0c;然后在UIAbility的onNewWant或者onCreate方法中 解析配置的自定义参数信息判断跳转不同页面&a…

go语言实现UTF8与GB2312内码转换

使用Go语言做个UTF-8转GB2312的代码,输入utf-8编码的文本&#xff0c;输出转换后的国标编码的hex内码 package mainimport ("fmt""os""strings""golang.org/x/text/encoding/simplifiedchinese""golang.org/x/text/transform&quo…

使用AKTools本地部署AKShare财经数据接口库

使用AKTools部署AKShare财经数据接口库&#xff0c;AKShare的介绍见&#xff1a;基于 Python 的财经数据接口库&#xff1a;AKShare-CSDN博客 AKTools 是一款用于快速搭建 AKShare HTTP API 的工具&#xff0c;通过 AKTools 可以利用一行命令来启动 HTTP 服务&#xff0c;从而…

FreeType 介绍及 C# 示例

FreeType 是一个开源的字体渲染引擎&#xff0c;用于将字体文件&#xff08;如 TrueType、OpenType、Type 1 等&#xff09;转换为位图或矢量图形。它广泛应用于操作系统、图形库、游戏引擎等领域&#xff0c;支持高质量的字体渲染和复杂的文本布局。 FreeType 的核心功能 字体…

winform监听全局鼠标事件

如果您希望监听全局鼠标事件&#xff0c;即使在其他应用程序&#xff08;如浏览器或其他软件&#xff09;中按下鼠标按钮也能捕捉到这些事件&#xff0c;您需要使用低级别的Windows API钩子。WinForms本身并不直接支持全局事件监听&#xff0c;但通过调用Windows API&#xff0…

如何学习网络安全?有哪些小窍门?

学好网络安全其实没有所谓的捷径&#xff0c;也没有什么小窍门。 入门网络安全首先要有浓厚的学习兴趣&#xff0c;不然很容易就变成了从入门到放弃了。 其次要能静下心&#xff0c;踏踏实实的打好基础。如果你是零基础&#xff0c;建议从Web安全入手&#xff0c;课程难度相对…

测试工程师的linux 命令学习(持续更新中)

1.ls """1.ls""" ls -l 除文件名称外&#xff0c;亦将文件型态、权限、拥有者、文件大小等资讯详细列出 ls -l等同于 ll第一列共10位&#xff0c;第1位表示文档类型&#xff0c;d表示目录&#xff0c;-表示普通文件&#xff0c;l表示链接文件。…

C++实现设计模式---享元模式 (Flyweight)

享元模式 (Flyweight) 享元模式 是一种结构型设计模式&#xff0c;它通过共享对象来减少内存使用和对象创建的开销。当系统中有大量相似对象时&#xff0c;享元模式可以避免重复创建相同对象&#xff0c;从而节省内存。 意图 通过共享相同对象来减少内存消耗。用于系统中存在…

如何在gitlab cicd中实现每月10号上午执行

在 GitLab CI/CD 中&#xff0c;可以通过设置定时触发器&#xff08;Schedules&#xff09;和脚本中的时间判断逻辑结合&#xff0c;确保任务只在每月 10 号的上午运行。 以下是实现的步骤&#xff1a; 1. 设置定时触发器 GitLab 提供了 Schedules 功能&#xff0c;可以指定每…

K8S 亲和性与反亲和性 深度好文

今天我们来实验 pod 亲和性。官网描述如下&#xff1a; 假设有如下三个节点的 K8S 集群&#xff1a; k8s31master 是控制节点 k8s31node1、k8s31node2 是工作节点 容器运行时是 containerd 一、镜像准备 1.1、镜像拉取 docker pull tomcat:8.5-jre8-alpine docker pull nginx…

安装指南:LLaMA Factory、AutoGPTQ 和 vllm

安装指南&#xff1a;LLaMA Factory、AutoGPTQ 和 vllm 在本文中&#xff0c;我们将详细介绍如何安装 LLaMA Factory、AutoGPTQ 和 vllm&#xff0c;这些工具在大型语言模型&#xff08;LLMs&#xff09;和视觉语言模型&#xff08;VLMs&#xff09;的微调和量化中非常有用。我…

Nginx三种不同类型的虚拟主机(基于域名、IP 和端口)

&#x1f3e1;作者主页&#xff1a;点击&#xff01; Nginx-从零开始的服务器之旅专栏&#xff1a;点击&#xff01; &#x1f427;Linux高级管理防护和群集专栏&#xff1a;点击&#xff01; ⏰️创作时间&#xff1a;2025年1月15日13点14分 目录 1. 基于域名的虚拟主机 …

解析OVN架构及其在OpenStack中的集成

引言 随着云计算技术的发展&#xff0c;虚拟化网络成为云平台不可或缺的一部分。为了更好地管理和控制虚拟网络&#xff0c;Open Virtual Network (OVN) 应运而生。作为Open vSwitch (OVS) 的扩展&#xff0c;OVN 提供了对虚拟网络抽象的支持&#xff0c;使得大规模部署和管理…

C#异步和多线程,Thread,Task和async/await关键字--12

目录 一.多线程和异步的区别 1.多线程 2.异步编程 多线程和异步的区别 二.Thread,Task和async/await关键字的区别 1.Thread 2.Task 3.async/await 三.Thread,Task和async/await关键字的详细对比 1.Thread和Task的详细对比 2.Task 与 async/await 的配合使用 3. asy…

doris:导入概览

Apache Doris 提供了多种导入和集成数据的方法&#xff0c;您可以使用合适的导入方式从各种源将数据导入到数据库中。Apache Doris 提供的数据导入方式可以分为四类&#xff1a; 实时写入&#xff1a;应用程序通过 HTTP 或者 JDBC 实时写入数据到 Doris 表中&#xff0c;适用于…

【Flink系列】9. Flink容错机制

9. 容错机制 在Flink中&#xff0c;有一套完整的容错机制来保证故障后的恢复&#xff0c;其中最重要的就是检查点。 9.1 检查点&#xff08;Checkpoint&#xff09; 9.1.1 检查点的保存 1&#xff09;周期性的触发保存 “随时存档”确实恢复起来方便&#xff0c;可是需要我…

《Keras 3 在 TPU 上的肺炎分类》

Keras 3 在 TPU 上的肺炎分类 作者&#xff1a;Amy MiHyun Jang创建日期&#xff1a;2020/07/28最后修改时间&#xff1a;2024/02/12描述&#xff1a;TPU 上的医学图像分类。 &#xff08;i&#xff09; 此示例使用 Keras 3 在 Colab 中查看 GitHub 源 简介 设置 本教程将介…