四十四、openlayers官网示例Geographic Coordinates解析——在地图上添加弹窗,点击显示图形信息

本文主要是介绍四十四、openlayers官网示例Geographic Coordinates解析——在地图上添加弹窗,点击显示图形信息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用Overlay在地图上添加弹窗,点击控制显隐。

初始化图层的时候,添加一个矢量的点数据到地图上,在new Feature时添加一些自定义属性。

  const place = [-110, 45];const point = new Point(place);const map = new Map({target: "map",view: new View({center: place,zoom: 8,}),layers: [new TileLayer({source: new StadiaMaps({layer: "outdoors",}),}),new VectorLayer({source: new VectorSource({features: [new Feature({ geometry: point, name: "sss", color: "red" }),],}),style: {"circle-radius": 20,"circle-fill-color": "red",},}),],});

 写一个元素,用来展示信息。

 <div id="map" class="map"><div id="popup"><div>name:{{ message.name }}</div><div>color:{{ message.color }}</div><div class="triangle"></div></div></div>

添加一些样式。 

#popup {width: 160px;height: 80px;border-radius: 10px;background: #fff;position: absolute;padding: 10px;box-sizing: border-box;
}
.triangle {position: absolute;left: 50%;transform: translateX(-50%);bottom: -20px;border-top: 10px solid #fff;border-bottom: 10px solid transparent;border-left: 10px solid transparent;border-right: 10px solid transparent;
}

创建overlay实例 ,offset是偏移量,根据写的元素大小调节。

 const element = document.getElementById("popup");const popup = new Overlay({element: element,stopEvent: false,offset:[-80,-120],});map.addOverlay(popup);

 点击地图的时候,获取图形的信息并给 this.message赋值。

 map.on("click", (event) => {if (popover) {popover.dispose();popover = undefined;}const feature = map.getFeaturesAtPixel(event.pixel)[0];if (!feature) {popup.setPosition(undefined);return;}const coordinate = feature.getGeometry().getCoordinates();popup.setPosition(coordinate);this.message.name = feature.get("name");this.message.color = feature.get("color");});

 完整代码:

<template><div class="box"><h1>Geographic Coordinates</h1><div id="map" class="map"><div id="popup"><div>name:{{ message.name }}</div><div>color:{{ message.color }}</div><div class="triangle"></div></div></div><div id="info"></div></div>
</template><script>
import { Feature, Map, Overlay, View } from "ol/index.js";
import { OSM, Vector as VectorSource } from "ol/source.js";
import { Point } from "ol/geom.js";
import { Tile as TileLayer, Vector as VectorLayer } from "ol/layer.js";
import { useGeographic } from "ol/proj.js";
import StadiaMaps from "ol/source/StadiaMaps.js";
export default {name: "",components: {},data() {return {map: null,extentData: "",message: {name: "",color: "",},};},computed: {},created() {},mounted() {useGeographic();const place = [-110, 45];const point = new Point(place);const map = new Map({target: "map",view: new View({center: place,zoom: 8,}),layers: [new TileLayer({source: new StadiaMaps({layer: "outdoors",}),}),new VectorLayer({source: new VectorSource({features: [new Feature({ geometry: point, name: "sss", color: "red" }),],}),style: {"circle-radius": 20,"circle-fill-color": "red",},}),],});const element = document.getElementById("popup");const popup = new Overlay({element: element,stopEvent: false,offset:[-80,-120],});map.addOverlay(popup);function formatCoordinate(coordinate) {return `<table><tbody><tr><th>lon</th><td>${coordinate[0].toFixed(2)}</td></tr><tr><th>lat</th><td>${coordinate[1].toFixed(2)}</td></tr></tbody></table>`;}const info = document.getElementById("info");map.on("moveend", function () {const view = map.getView();const center = view.getCenter();info.innerHTML = formatCoordinate(center);});let popover;map.on("click", (event) => {if (popover) {popover.dispose();popover = undefined;}const feature = map.getFeaturesAtPixel(event.pixel)[0];if (!feature) {popup.setPosition(undefined);return;}const coordinate = feature.getGeometry().getCoordinates();popup.setPosition(coordinate);this.message.name = feature.get("name");this.message.color = feature.get("color");});map.on("pointermove", function (event) {const type = map.hasFeatureAtPixel(event.pixel) ? "pointer" : "inherit";map.getViewport().style.cursor = type;});},methods: {},
};
</script><style lang="scss" scoped>
#map {width: 100%;height: 500px;
}
.box {height: 100%;
}
#popup {width: 160px;height: 80px;border-radius: 10px;background: #fff;position: absolute;padding: 10px;box-sizing: border-box;
}
.triangle {position: absolute;left: 50%;transform: translateX(-50%);bottom: -20px;border-top: 10px solid #fff;border-bottom: 10px solid transparent;border-left: 10px solid transparent;border-right: 10px solid transparent;
}
</style>

这篇关于四十四、openlayers官网示例Geographic Coordinates解析——在地图上添加弹窗,点击显示图形信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1043324

相关文章

Java中Redisson 的原理深度解析

《Java中Redisson的原理深度解析》Redisson是一个高性能的Redis客户端,它通过将Redis数据结构映射为Java对象和分布式对象,实现了在Java应用中方便地使用Redis,本文... 目录前言一、核心设计理念二、核心架构与通信层1. 基于 Netty 的异步非阻塞通信2. 编解码器三、

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

一文解析C#中的StringSplitOptions枚举

《一文解析C#中的StringSplitOptions枚举》StringSplitOptions是C#中的一个枚举类型,用于控制string.Split()方法分割字符串时的行为,核心作用是处理分割后... 目录C#的StringSplitOptions枚举1.StringSplitOptions枚举的常用

Python函数作用域与闭包举例深度解析

《Python函数作用域与闭包举例深度解析》Python函数的作用域规则和闭包是编程中的关键概念,它们决定了变量的访问和生命周期,:本文主要介绍Python函数作用域与闭包的相关资料,文中通过代码... 目录1. 基础作用域访问示例1:访问全局变量示例2:访问外层函数变量2. 闭包基础示例3:简单闭包示例4

MyBatis延迟加载与多级缓存全解析

《MyBatis延迟加载与多级缓存全解析》文章介绍MyBatis的延迟加载与多级缓存机制,延迟加载按需加载关联数据提升性能,一级缓存会话级默认开启,二级缓存工厂级支持跨会话共享,增删改操作会清空对应缓... 目录MyBATis延迟加载策略一对多示例一对多示例MyBatis框架的缓存一级缓存二级缓存MyBat

前端缓存策略的自解方案全解析

《前端缓存策略的自解方案全解析》缓存从来都是前端的一个痛点,很多前端搞不清楚缓存到底是何物,:本文主要介绍前端缓存的自解方案,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、为什么“清缓存”成了技术圈的梗二、先给缓存“把个脉”:浏览器到底缓存了谁?三、设计思路:把“发版”做成“自愈”四、代码

Java高效实现PowerPoint转PDF的示例详解

《Java高效实现PowerPoint转PDF的示例详解》在日常开发或办公场景中,经常需要将PowerPoint演示文稿(PPT/PPTX)转换为PDF,本文将介绍从基础转换到高级设置的多种用法,大家... 目录为什么要将 PowerPoint 转换为 PDF安装 Spire.Presentation fo

Java集合之Iterator迭代器实现代码解析

《Java集合之Iterator迭代器实现代码解析》迭代器Iterator是Java集合框架中的一个核心接口,位于java.util包下,它定义了一种标准的元素访问机制,为各种集合类型提供了一种统一的... 目录一、什么是Iterator二、Iterator的核心方法三、基本使用示例四、Iterator的工