Openlayer add mark及添加hover效果实例

Crq
Crq
管理员
1871
文章
0
粉丝
Linux教程评论5字数 527阅读1分45秒阅读模式
摘要这篇文章主要为大家介绍了Openlayer add mark及添加hover效果实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
add mark
方法一

如果有多个点的话,可以生成多个 feature(循环调用 addFeature)

const iconStyle = () =>
  new Style({ image: new Icon({ scale: 0.2, src: image }) });
const addFeature = (point: Coordinate) =>
  new Feature({
    geometry: new Point(Proj.fromLonLat(point)),
    properties,
    name: "当前位置",
    population: 4000,
    rainfall: 500,
  });
const pointSource = new VectorSource({
  features: [addFeature(point)],
});
const clusterSourceForLayer = new Cluster({
  source: pointSource,
  distance: 50,
});
const pointLayer = new VectorLayer({
  source: clusterSourceForLayer,
  zIndex: 3,
  style: iconStyle,
});
map.addLayer(pointLayer);
pointLayer.set("baseMap", "iconLayer");
方法二

用 geojson 去渲染 mark

const iconStyle = () =>
  new Style({ image: new Icon({ scale: 0.2, src: image }) });
const pointSource = new VectorSource({
  features: new GeoJSON().readFeatures(geojson, {
    dataProjection: "EPSG:4326",
    featureProjection: "EPSG:3857",
  }),
});
const clusterSourceForLayer = new Cluster({
  source: pointSource,
  distance: 50,
});
const pointLayer = new VectorLayer({
  source: clusterSourceForLayer,
  zIndex: 3,
  style: iconStyle,
});
map?.addLayer(pointLayer);
pointLayer.set("baseMap", "iconLayer");
geojson 格式

生成 geojson 的方式:

  • 自己手动构建
  • 使用 @turf/helpers,它提供了 point、featureCollection 等方法
  • {
      "type": "FeatureCollection",
      "features": [
        {
          "type": "Feature",
          "properties": {
            "id": "customer002",
            "name": "c2"
          },
          "geometry": {
            "type": "Point",
            "coordinates": [119.777738303153, 32.91324329434815]
          }
        },
        {
          "type": "Feature",
          "properties": {
            "id": "customerId007",
            "name": "张三"
          },
          "geometry": {
            "type": "Point",
            "coordinates": [109.54393448864997, 35.7427088696462]
          }
        }
      ]
    }
    hover mark
    popover

    overlay 需要一个 dom 元素,这里是用过 ref 获取的

    const o = new Overlay({ element: ref.current });
    map?.addOverlay(o);
    setOverlay(o);
    方法一

    用 select 去做,它有个 select 事件

    它事件参数中,有个 selected,如果不是空数组,说明你鼠标正在 hover mark,就可以弹出 popover,显示你想要显示的内容

    const select = new Select({
      condition: pointerMove,
      hitTolerance: 1,
      layers: [iconLayer],
      style: iconStyle,
    });
    select.on("select", (e) => {
      const { selected } = e;
      if (selected.length) {
        const [feature] = selected;
        const _feature = feature.get("features")[0];
        const id = _feature.get("id");
        const name = _feature.get("name");
        setContent({ name, id });
        const coordinate = feature.get("geometry").flatCoordinates;
        overlay.setPosition(coordinate);
      } else {
        overlay.setPosition(undefined);
      }
    });
    map?.addInteraction(select);
    方法二

    用 select 去做,本质也是通过 pointerMove 事件,所以可以直接在 map 上监听 pointerMove 事件

    具体的实现方式我没有去尝试,通过上面的推断,我觉得是可行的

    map.on("pointerMove", (e) => {});
    clear mark

    通过 useEffect 返回的函数清理地图中的 mark

    useEffect(() => {
      return () => {
        // 卸载页面中的 mark
        iconSource?.getFeatures().forEach((feature) => {
          iconSource?.removeFeature(feature);
        });
      };
    }, [points, iconSource]);
    方法 addInteraction、addOverlay、addLayer 区别

    我没有搞清楚他们之间的区别,目前了解的是:

  • addLayer:是添加图层,图层分为:
    1. 栅格:Tile(图片)
      矢量:Vector(geojson、lerc)
      矢量栅格:VectorTile(pbf)
  • addInteraction:添加 Select 和 Modify
  • addOverlay:添加 Overlay 和 Control
    1. Popover 可以用 Overlay 去做

    以上就是Openlayer add mark及添加hover效果实例详解的详细内容

    weinxin
    我的微信
    微信号已复制
    我的微信
    这是我的微信扫一扫
     
    Crq
    • 本文由 Crq 发表于2025年3月13日 21:37:28
    • 转载请注明:https://www.cncrq.com/13435.html
    【技术快报】9.12-9.18 Linux教程

    【技术快报】9.12-9.18

    本期《linux就该这么学》的技术周报中,将为您推出Nginx源码安装及调优配置、国内三大云数据库测试对比、12个Linux进程管理命令介绍、怎样在Ubuntu中修改默认程序、在a...
    最牛X的GCC 内联汇编 Linux教程

    最牛X的GCC 内联汇编

    正如大家知道的,在C语言中插入汇编语言,其是Linux中使用的基本汇编程序语法。本文将讲解 GCC 提供的内联汇编特性的用途和用法。对于阅读这篇文章,这里只有两个前提要求,很明显,...
    10款优秀Vim插件帮你打造完美IDE Linux教程

    10款优秀Vim插件帮你打造完美IDE

    如果你稍微写过一点代码,就能知道“集成开发环境”(IDE)是多么的便利。不管是Java、C还是Python,当IDE会帮你检查语法、后台编译,或者自动导入你需要的库时,写代码就变得...
    匿名

    发表评论

    匿名网友
    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
    确定

    拖动滑块以完成验证