Jinuss's blog Jinuss's blog
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript高级程序设计》
    • 《Vue》
    • 《React》
    • 《Git》
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • 学习
  • 实用技巧
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

东流

前端可视化
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript高级程序设计》
    • 《Vue》
    • 《React》
    • 《Git》
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • 学习
  • 实用技巧
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • React

  • Vue

  • JavaScript文章

  • 学习笔记

  • openlayers

    • Openlayers用图片填充面
    • Openlayers用图片填充线
    • openlayers处理大量Overlay渲染问题
    • openlayers的比例尺
    • openlayers水印
    • openlayer实现矢量图层点击高亮效果
      • openlayers实现自定义路径
      • Openlayers加载渲染矢量切片
      • openlayers实现角度测量
      • openlayers实现长度测量
      • openlayers实现面积测量
      • Openlayers实现方位角测量
      • Openlayers中的动画
      • Openlayers的多边形高级交互
      • Openlayers地图底图换色
      • Openlayers种的默认交互事件
      • Openlayers默认键盘交互实现
      • 源码分析之Openlayers中默认键盘事件触发机制
      • 源码分析之Openlayers中的Collection类
    • threejs

    • MapboxGL

    • 工具

    • 源码合集

    • 前端
    • openlayers
    东流
    2024-10-16
    目录

    openlayer实现矢量图层点击高亮效果

    # 概述

    本文主要介绍如何在 Openlayers 矢量图层上实现点击高亮效果。

    # 效果演示

    # 具体实现

    数据准备

    矢量数据可点击下载

    加载矢量图层

    矢量数据是通过调用Openlayers的new GeoJSON()实例去加载读取。

    let style = new Style({
      fill: new Fill({
        color: "rgba(255, 255, 255, 0.6)",
      }),
      stroke: new Stroke({
        color: "#319FD3",
        width: 1,
      }),
      text: new Text({
        font: "12px Calibri,sans-serif",
        fill: new Fill({
          color: "#000",
        }),
        stroke: new Stroke({
          color: "#fff",
          width: 3,
        }),
      }),
    });
    
    let vectorLayer = new LayerVector({
      source: new SourceVector({
        features: new GeoJSON({
          featureProjection: "EPSG:3857",
        }).readFeatures(lands),
        format: new GeoJSON(),
        wrapX: true,
      }),
      style: (feature) => {
        style.getText().setText(feature.get("name"));
        return style;
      },
    });
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33

    绑定事件

    监听地图click和pointermove事件,获取点击位置的像素点信息,就是点在整个浏览器屏幕的位置

    map.on("pointermove", (evt) => {
      if (evt.dragging) {
        return;
      }
      let pixel = map.getEventPixel(evt.originalEvent);
      displayFeatureInfo(pixel);
    });
    
    map.on("click", (evt) => {
      displayFeatureInfo(evt.pixel);
    });
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    定义displayFeatureInfo方法

    该方法主要就是通过 Openlayers 的map.forEachFeatureAtPixel方法和点击位置获取点击的feature,再对feature进行添加到高亮图层中显示。

    具体实现如下:

     displayFeatureInfo(pixel) {
                // 创建高亮图层
                if (!featureOverlay) {
                    featureOverlay = new LayerVector({
                        source: new SourceVector(),
                        map: map,
                        style: (feature) => {
                            highlightStyle
                                .getText()
                                .setText(feature.get("name"));
                            return highlightStyle;
                        },
                    });
                }
                // 获取点击的要素
                let feature = map.forEachFeatureAtPixel(
                    pixel,
                    (feature) => feature
                );
    
                // 设置高亮要素
                if (feature !== highlight) {
                    if (highlight) {
                        featureOverlay
                            .getSource()
                            .removeFeature(highlight);
                    }
                    if (feature) {
                        featureOverlay.getSource().addFeature(feature);
                    }
                    highlight = feature;
                }
            }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    编辑 (opens new window)
    上次更新: 2024/11/12, 10:01:20
    openlayers水印
    openlayers实现自定义路径

    ← openlayers水印 openlayers实现自定义路径→

    最近更新
    01
    GeoJSON
    05-08
    02
    Circle
    04-15
    03
    CircleMarker
    04-15
    更多文章>
    Theme by Vdoing | Copyright © 2024-2025 东流 | MIT License
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式