050:vue+openlayers使用Popup组件显示经纬度坐标

本文由高级前端工程师大剑师兰特分享,介绍如何在Vue应用中结合OpenLayers通过Popup组件展示经纬度坐标。示例提供源代码,并提供配置方式,包括基础设置链接和npm命令。适合学习openlayers的开发者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作者: 还是大剑师兰特 ,曾为美国某知名大学计算机专业研究生,现为国内GIS领域高级前端工程师,优快云知名博主,深耕openlayers、leaflet、mapbox、cesium,canvas,echarts等技术开发,欢迎加微信(gis-dajianshi),一起交流。

查看本专栏目录 - 本文是第 050个示例

一、示例效果图

二、示例简介

本示例演示如何在vue+openlayers中使用popup的形式来显示出经纬度坐标,本示例是采用引用扩展的方式,相比以往的添加方式,减少了不少的代码量。

直接复制下面的 vue+openlayers源代码,操作2分钟即可运行实现效果; 注意如果OpenStreetMap无法加载,请加载其他来练习

Vue OpenLayers气泡窗是一种在Vue.js应用程序中使用OpenLayers地图库的弹出窗口。您可以使用它来在地图上显示信息,例如位置名称,地址,图像等。以下是创建Vue OpenLayers气泡窗的一些步骤: 1. 安装OpenLayersVue.js 首先,您需要安装OpenLayersVue.js。您可以使用以下命令: ``` npm install ol vue ``` 2. 创建Vue组件 接下来,您需要创建一个Vue组件来包含OpenLayers地图和气泡窗。您可以使用以下代码: ``` <template> <div> <div ref="map" class="map"></div> <div ref="popup" class="ol-popup"> <a href="#" class="ol-popup-closer" @click.prevent="closePopup"></a> <div ref="popupContent"></div> </div> </div> </template> <script> import 'ol/ol.css'; import Map from 'ol/Map'; import View from 'ol/View'; import Overlay from 'ol/Overlay'; import TileLayer from 'ol/layer/Tile'; import OSM from 'ol/source/OSM'; export default { name: 'MapWithPopup', data() { return { map: null, popup: null, popupContent: null } }, mounted() { this.initMap(); }, methods: { initMap() { // create map this.map = new Map({ target: this.$refs.map, layers: [ new TileLayer({ source: new OSM() }) ], view: new View({ center: [0, 0], zoom: 2 }) }); // create popup this.popup = new Overlay({ element: this.$refs.popup, autoPan: true, autoPanAnimation: { duration: 250 } }); this.map.addOverlay(this.popup); // add click event to map this.map.on('click', this.onClick); }, onClick(event) { // get feature at clicked position const feature = this.map.forEachFeatureAtPixel(event.pixel, (feature) => feature); // check if feature exists if (feature) { // get geometry of feature const geometry = feature.getGeometry(); // get coordinate of geometry const coordinate = geometry.getCoordinates(); // set content of popup this.popupContent = 'Feature selected at ' + coordinate; // set position of popup this.popup.setPosition(coordinate); } else { // close popup this.popup.setPosition(undefined); } }, closePopup() { // close popup this.popup.setPosition(undefined); } } } </script> <style> .map { height: 400px; } .ol-popup { position: absolute; background-color: white; padding: 15px; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.5); font-size: 14px; line-height: 1.5; display: none; } .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: white; border-width: 10px; left: 48%; margin-left: -10px; } .ol-popup:before { border-top-color: rgba(0, 0, 0, 0.75); border-width: 11px; left: 48%; margin-left: -11px; } .ol-popup-closer { position: absolute; top: 5px; right: 5px; font-size: 12px; font-weight: bold; text-decoration: none; color: #bababa; } .ol-popup-closer:hover { color: #ffcccc; } </style> ``` 3. 使用Vue组件 最后,您可以在Vue.js应用程序中使用组件。您可以将其包含在另一个Vue组件中,也可以在Vue路由中使用它。以下是一个示例: ``` <template> <div> <h1>My Map</h1> <MapWithPopup /> </div> </template> <script> import MapWithPopup from './MapWithPopup'; export default { name: 'MyApp', components: { MapWithPopup } } </script> ``` 当您在地图上单击时,将显示一个气泡窗口。您可以将此组件扩展为显示更多信息,例如图像,链接等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

还是大剑师兰特

打赏一杯可口可乐

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值