openlayers 点击多边形弹框,高亮多边形,自定义属性传递,鼠标悬浮多边形上动态修改鼠标样式

本文主要是介绍openlayers 点击多边形弹框,高亮多边形,自定义属性传递,鼠标悬浮多边形上动态修改鼠标样式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本篇介绍一下使用openlayers点击多边形弹框,高亮多边形,自定义属性传递,鼠标悬浮多边形上动态修改鼠标样式

1 需求

  • 加载天地图,polygon
  • 传递自定义属性
  • 标悬浮在polygon上,根据自定义属性,动态修改鼠标样式为pointer
  • 点击polygon,根据自定义属性,高亮,弹框

2 分析

主要是 openlayers 中 地图事件,overlay等功能的使用

  • 为vectorSource填充features,有两种方法:

    1. features: [new Feature()]
    2. features: new GeoJSON().readFeatures(geoJSON)
  • 为vectorLayer填充style,有两种写法:

    1. style:new Style()
    2. style:{‘stroke-color’: ‘rgba(255, 128, 100, 1)’}
  • 获取鼠标点击活悬浮时的features,有两种写法:

    1. map.value.forEachFeatureAtPixel()
    2. map.value.getFeaturesAtPixel()

3 实现


没有录上鼠标样式改变,复制代码查看

<template><div id="map" class="map"></div><div id="popup" class="ol-popup" ref="container"><a href="#" id="popup-closer" class="ol-popup-closer" ref="closer"></a><div id="popup-content" ref="content"></div></div>
</template><script setup lang="ts">
import { Feature, Map, Overlay, View } from 'ol';
import { GeoJSON } from 'ol/format';
import { Polygon } from 'ol/geom';
import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer';
import { get, toLonLat } from 'ol/proj';
import { Vector, XYZ } from 'ol/source';
import { Fill, Stroke, Style } from 'ol/style';
import { toStringHDMS } from 'ol/coordinate.js';const projection = get('EPSG:4326');const layerTypeMap = {vector: ['vec', 'cva'], // [矢量底图, 矢量注记]image: ['img', 'cia'], // [影像底图, 影像注记]terrain: ['ter', 'cta'] // [地形晕渲, 地形注记]
};const map = ref();
const container = ref();
const content = ref();
const closer = ref();
const overlay = shallowRef();
const feature = ref();
const geoJSON = {type: 'FeatureCollection',crs: {type: 'name',properties: {name: 'EPSG:4326'}},features: [{type: 'Feature',geometry: {type: 'Polygon',coordinates: [[[112, 31],[113, 32.2],[114, 30.5],[112, 31]]]},properties: {//自定义属性pointer: true}}]
};onMounted(() => {initMap('image');
});const initMap = (layerType = 'image') => {const key = '替换为天地图key';overlay.value = new Overlay({element: container.value,autoPan: {animation: {duration: 250}}});// c: 经纬度投影 w: 墨卡托投影const matrixSet = 'c';map.value = new Map({target: 'map',layers: [// 底图new TileLayer({source: new XYZ({url: `https://t{0-7}.tianditu.gov.cn/DataServer?T=${layerTypeMap[layerType][0]}_${matrixSet}&tk=${key}&x={x}&y={y}&l={z}`,projection})}),// 注记new TileLayer({source: new XYZ({url: `https://t{0-7}.tianditu.gov.cn/DataServer?T=${layerTypeMap[layerType][1]}_${matrixSet}&tk=${key}&x={x}&y={y}&l={z}`,projection})}),new VectorLayer({source: new Vector({features: [new Feature({geometry: new Polygon([[[116, 31],[118, 32.2],[119, 30.5],[116, 31]]]),pointer: true //自定义属性,可用于修改鼠标样式、单击轮廓弹框,改变feature样式})]}),style: new Style({fill: new Fill({color: 'rgba(228, 147, 87, 0.4)'}),stroke: new Stroke({color: 'rgba(228, 147, 87, 1)',width: 3})})}),new VectorLayer({source: new Vector({features: [new Feature({geometry: new Polygon([[[114, 31],[115, 32.2],[116, 30.5],[114, 31]]])})]}),style: {//layer样式可以这样写'stroke-color': 'rgba(255, 255, 100, 1)','stroke-width': 1.5,'fill-color': 'rgba(255, 255, 100, 0.5)','circle-radius': 6,'circle-fill-color': 'rgba(255, 255, 100, 1)'}}),new VectorLayer({source: new Vector({features: new GeoJSON().readFeatures(geoJSON) //读取geojson格式数据}),style: {//layer样式可以这样写'stroke-color': 'rgba(255, 128, 100, 1)','stroke-width': 1.5,'fill-color': 'rgba(255, 128, 100, 0.5)','circle-radius': 6,'circle-fill-color': 'rgba(255, 128, 100, 1)'}})],overlays: [overlay.value],view: new View({center: [116.406393, 39.909006],projection: projection,zoom: 5,maxZoom: 17,minZoom: 1})});map.value.on('pointermove', (e: any) => {// 根据自定义属性改变鼠标样式// 方法一// map.value.getTargetElement().style.cursor = 'auto';// let pixel = map.value.getEventPixel(e.originalEvent);// map.value.forEachFeatureAtPixel(pixel, (feature: any) => {//   const property = feature.getProperties();//   if (property.pointer) {//     map.value.getTargetElement().style.cursor = 'pointer'; //设置鼠标样式//   } else {//     map.value.getTargetElement().style.cursor = 'auto';//   }// });// 方法二map.value.getTargetElement().style.cursor = 'auto';const features = map.value.getFeaturesAtPixel(e.pixel);features.forEach(feature => {const property = feature.getProperties();if (property.pointer) {map.value.getTargetElement().style.cursor = 'pointer'; //设置鼠标样式} else {map.value.getTargetElement().style.cursor = 'auto';}});});map.value.on('click', e => {// 根据自定义属性改变轮廓样式,也可以进行弹框if (feature.value) {feature.value.setStyle();closer.value.onclick();}const features = map.value.getFeaturesAtPixel(e.pixel);const f = features.find(f => f.getProperties().pointer);if (f) {feature.value = f;f.setStyle(new Style({fill: new Fill({color: 'rgba(255, 255, 100, 0.5)'}),stroke: new Stroke({color: 'rgba(255, 255, 100, 1)',width: 3})}));const coordinate = e.coordinate;const hdms = toStringHDMS(toLonLat(coordinate));content.value.innerHTML = '<p>当前经纬度:</p><code>' + hdms + '</code>';overlay.value.setPosition(coordinate);}});closer.value.onclick = function () {overlay.value.setPosition(undefined);closer.value.blur();if (feature.value) {feature.value.setStyle();}return false;};
};
</script>
<style scoped lang="scss">
.map {width: 100%;height: 100%;
}
.ol-popup {position: absolute;background-color: rgba(255, 255, 255, 0.7);box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);padding: 15px;border-radius: 10px;border: 1px solid #cccccc;bottom: 12px;left: -50px;min-width: 280px;
}
.ol-popup:after,
.ol-popup:before {top: 100%;border: solid transparent;content: ' ';height: 0;width: 0;position: absolute;pointer-events: none;
}
.ol-popup:after {border-top-color: rgba(255, 255, 255, 0.7);border-width: 10px;left: 48px;margin-left: -10px;
}
.ol-popup:before {border-top-color: #cccccc;border-width: 11px;left: 48px;margin-left: -11px;
}
.ol-popup-closer {text-decoration: none;position: absolute;top: 2px;right: 8px;
}
.ol-popup-closer:after {content: '✖';
}
</style>

这篇关于openlayers 点击多边形弹框,高亮多边形,自定义属性传递,鼠标悬浮多边形上动态修改鼠标样式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


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

相关文章

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (

go动态限制并发数量的实现示例

《go动态限制并发数量的实现示例》本文主要介绍了Go并发控制方法,通过带缓冲通道和第三方库实现并发数量限制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录带有缓冲大小的通道使用第三方库其他控制并发的方法因为go从语言层面支持并发,所以面试百分百会问到

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

一文详解SpringBoot中控制器的动态注册与卸载

《一文详解SpringBoot中控制器的动态注册与卸载》在项目开发中,通过动态注册和卸载控制器功能,可以根据业务场景和项目需要实现功能的动态增加、删除,提高系统的灵活性和可扩展性,下面我们就来看看Sp... 目录项目结构1. 创建 Spring Boot 启动类2. 创建一个测试控制器3. 创建动态控制器注

python删除xml中的w:ascii属性的步骤

《python删除xml中的w:ascii属性的步骤》使用xml.etree.ElementTree删除WordXML中w:ascii属性,需注册命名空间并定位rFonts元素,通过del操作删除属... 可以使用python的XML.etree.ElementTree模块通过以下步骤删除XML中的w:as

springboot如何通过http动态操作xxl-job任务

《springboot如何通过http动态操作xxl-job任务》:本文主要介绍springboot如何通过http动态操作xxl-job任务的问题,具有很好的参考价值,希望对大家有所帮助,如有错... 目录springboot通过http动态操作xxl-job任务一、maven依赖二、配置文件三、xxl-

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码