Openlayer系列:点击地图上的点位,弹出信息窗在点位上方显示;

7 篇文章 0 订阅
6 篇文章 0 订阅
文章描述了一种在OpenLayer6集成于Vue2和Element的应用中,当用户点击地图点位时,如何创建并定位一个弹出框在点位上方显示的方法。涉及到的关键技术包括Overlay和Select,以及处理单击事件来更新地图中心和缩放级别。
摘要由CSDN通过智能技术生成

遇到问题

有没有精通openlayer web开发呀
需要点地图的上点位的时候,弹出框在点位上方显示(UI效果图如下,使用的vue2和element、openlayer6)

在这里插入图片描述

解决思路

我自己找的方法,openlayer的Overlay、Select都使用到了,

// 创建一个弹窗 Overlay 对象
      var overlay = new Overlay({
        element: container, //绑定 Overlay 对象和 DOM 对象的
        autoPan: true, // 定义弹出窗口在边缘点击时候可能不完整 设置自动平移效果
        offset: [-110, -180],//偏移量,使得弹出框显示在坐标点正上方
        autoPanAnimation: {
          duration: 250 //自动平移效果的动画时间 9毫秒
        }
      });

      window.map.addOverlay(overlay);

      window.map.on('singleclick', function (evt) {
        
        let coordinate = evt.coordinate
        // 点击尺 (这里是尺(米),并不是经纬度);
        var feature = window.map.forEachFeatureAtPixel(evt.pixel, (feature) => {
          return feature;
        });

        console.log('singleclick feature1', feature);

        if (JSON.stringify(feature) == 'undefined' || !feature) {

          console.log('closed le');
          _that.isShowDialog = false;
          return;
        } else if (!feature.values_.data) {
          _that.isShowDialog = false;
          return;
        }
        if (feature.values_.data) { //捕捉到要素
          let Coordinates = [feature.values_.data.longitude, feature.values_.data.latitude];
          // window.map.getView().setCenter(fromLonLat(Coordinates, 'EPSG:3857'))
          window.map.getView().animate({ // 只设置需要的属性即可
            target: 'map',
            center: fromLonLat(Coordinates, 'EPSG:3857'),
            zoom: 12.6, // 缩放级别
            rotation: undefined, // 缩放完成view视图旋转弧度
            duration: 1000 // 缩放持续时间,默认不需要设置
          })
        }
    }

    overlay.setPosition(coordinate); //把 overlay 显示到指定的 x,y坐标
          if (_that.mapInfo.projectName) {
            console.log('_that.mapInfo.projectName 44', _that.mapInfo.projectName);
            setTimeout(() => {
              _that.isShowDialog = true;
            }, 600);
          }
    


未完待续

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在OpenLayers地图点击位置后弹出提示框,可以使用Overlay和Popup来实现。以下是一个简单的例子: ```javascript // 创建地图 var map = new ol.Map({ target: 'map', layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: ol.proj.fromLonLat([116.3975, 39.9086]), zoom: 10 }) }); // 创建一个VectorLayer用于添加点 var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector() }); // 创建一个Overlay用于显示提示框 var popupOverlay = new ol.Overlay({ element: document.getElementById('popup'), autoPan: true, autoPanAnimation: { duration: 250 } }); // 创建一个点击事件处理函数 function handleMapClick(event) { // 获取点击位置的坐标 var coordinate = event.coordinate; // 创建一个点Feature并添加到VectorLayer上 var feature = new ol.Feature({ geometry: new ol.geom.Point(coordinate) }); vectorLayer.getSource().addFeature(feature); // 设置提示框内容为坐标 var content = '<p>坐标:' + coordinate + '</p>'; document.getElementById('popup-content').innerHTML = content; // 将提示框显示点击位置上 popupOverlay.setPosition(coordinate); map.addOverlay(popupOverlay); } // 监听Map对象的"click"事件,并在回调函数中处理点击事件 map.on('click', handleMapClick); // 将VectorLayer添加到地图上 map.addLayer(vectorLayer); ``` 在HTML中还需要添加一个Popup的容器,如下所示: ```html <div id="map"></div> <div id="popup" class="ol-popup"> <a href="#" id="popup-closer" class="ol-popup-closer"></a> <div id="popup-content"></div> </div> ``` 这样就可以在地图点击位置后弹出提示框了,提示框会显示点击位置的坐标。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

浪潮行舟

你的鼓励是我创作的最大动力。

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

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

打赏作者

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

抵扣说明:

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

余额充值