openlayers上添加點擊事件


有很多場景會有這個需求,就是我繪制了圖標,點擊圖標需要展示一些對應的信息

openlayer的事件主要是通過監聽來完成的,你所有的icon的點擊事件都是可以通過監聽map的點擊事件來處理對應的邏輯的

話不多說直接上代碼

// 監聽singleclick事件,通過點擊事件,獲取對應點的feature圖標
        const that: any = this;
        var overlay: any
        var popupCloser = document.getElementById("popup-closer") as HTMLElement;
        that.map.on('singleclick', function (e: any) {
            var container = document.getElementById("popup") as HTMLElement;
            var content = document.getElementById("popup-content") as HTMLElement;
            overlay = new olOverlay({
                //設置彈出框的容器
                element: container,
                //是否自動平移,即假如標記在屏幕邊緣,彈出時自動平移地圖使彈出框完全可見
                autoPan: true
            });
            console.log(e.coordinate)
            if (that.map.hasFeatureAtPixel(e.pixel)) {
                var feature = that.map.getFeaturesAtPixel(e.pixel)
                console.log(feature)
                var pixel = that.map.getEventPixel(e.originalEvent);
                that.map.forEachFeatureAtPixel(pixel, function (feature: any) {
                    //coodinate存放了點擊時的坐標信息
                    var coodinate = e.coordinate;
                    //設置彈出框內容,可以HTML自定義
                    content.innerHTML = "<p>你點擊的坐標為:" + coodinate + "</p>";
                    //設置overlay的顯示位置
                    overlay.setPosition(coodinate);
                    //顯示overlay
                    that.map.addOverlay(overlay);
                });
            }
        })

        popupCloser.addEventListener('click', function () {
            overlay.setPosition(undefined);
        });

你會發現里面很多dom的操作方式,沒錯,openlayer就是這么強大,就是你所有的渲染等都是對應的一個dom,就是div這種,不想pixijs等是通過canvas去繪制的

在此之前你還需要在你html里添加對應的dom元素,如下

<template>
  <div class="main">
    <div id="map" class="map">
      <div id="mouse-position"></div>
    </div>
    <div id="popup" class="ol-popup">
      <a href="#" id="popup-closer" class="ol-popup-closer"></a>
      <div id="popup-content"></div>
    </div>
  </div>
</template>

順表丟上css樣式,哈哈

.ol-popup {
    position: absolute;
    background-color: #eeeeee;
    -webkit-filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
    filter: drop-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: #eeeeee;
    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: "✖";
}

接下來看看效果

 

 索嘎,點擊事件完工


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM