openlayers 给数据点(散点)添加点击事件 和hover事件

前言:由于本人也是最近才开始学习openlayers,如说明的有所不对,请在评论区指出。

1、点击事件

/**
     * 捕捉点击事件
     */
    featureClick(callback) {
      let _this = this;
      let selectPointerClick = new PointerInteraction({
        handleDownEvent: function(ev){
          let pixel = ev.pixel;
          let feature = _this.map2d.map.forEachFeatureAtPixel(
          pixel,
          function (feature) {
            return feature;
          }
        );
        if (feature) {
          callback({ flag: true }); // do something
        } else {
          callback({ flag: false }); //do something
        }
        }
      });
      _this.map2d.map.addInteraction(selectPointerClick);
    },

代码中不写注释,我讲解一下流程,能让你有个大概的认识应该就能符合你业务去做自己的逻辑。

首先你最好对交互事件有一定的认识,这里推荐大佬写的博客,有一个初步的认识。
https://blog.csdn.net/weitaming1/article/details/87805287
这里为了保证文章的逻辑完善,我会把相关的给截图下来。
在这里插入图片描述
在这里插入图片描述
文档只有英文的,我粗略的解释一下,我们自定义的鼠标交互事件只需要点击,而实际上我实现的点击的方法是按下。但某种程度上而言,他们的效果是等同的。他的描述是如果handleDownEvent的这个函数返回了true说明一个拖拽队列已经开始,但我们并不需要拖拽,我们只是借着这个按下实现点击。

我们生成一个PointerInteraction对象,在这个回调参数中接受一个事件ev,通过map.forEachFeatureAtPixel,用当前位置的位置pixel去判断每一个feature的位置pixel,是否能够找到点击的那个feature,如果找到了,我们做某些事,如果没找到做某些事。接着把这个交互事件往地图实例上添加。

2、hover事件

/**
     * 捕捉滑过事件
     */
    featureHover(callback) {
      const _this = this;
      _this.map2d.map.on('pointermove',function(ev){
        let pixel = ev.pixel;
        let feature = _this.map2d.map.forEachFeatureAtPixel(pixel,function (feature) {
          return feature;
        });
        if (feature) {
          callback({flag: true }); //do something
        } else {
          callback({ flag: false });// do something
        }
      })
    },

在地图中监听pointermove 事件,这个是原生支持的。接受一个回调参数,如法炮制。找到点,做什么,找不到点做什么。

杂谈

对了。这个实例由于我是直接复制编辑器中的,所以map 实例 你们得自己搞定。照着这个思路,我不敢保证你能复制就用。思路是最主要的,预祝你顺利解决问题。

新增修改 来自2021年6月15日

视频内容

https://www.bilibili.com/video/BV1DM4y1M7aY

点击feature事件比较标准的写法
// 给地图添加点击事件
    featureClick: function (callback) {
      let _this = this;
      _this.map2d.on("singleclick", function (ev) {
        let pixel = ev.pixel;
        let feature = _this.map2d.forEachFeatureAtPixel(
          pixel,
          function (feature) {
            return feature;
          }
        );
        if (feature) {
        // dosth like next line
          _this.correctTypeAndFunction(callback, feature, pixel);
        } else {
        //do sth like next line
          _this.lastClickFactoryFeature = null;
        }
      });
    },
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在 React 中实现 OpenLayers 的悬浮事件点击事件,可以按照以下步骤进行操作: 1.在 React 项目中安装 OpenLayers: ``` npm install ol ``` 2.在 React 组件中引入 OpenLayers: ``` import 'ol/ol.css'; import Map from 'ol/Map'; import View from 'ol/View'; import TileLayer from 'ol/layer/Tile'; import OSM from 'ol/source/OSM'; import VectorLayer from 'ol/layer/Vector'; import VectorSource from 'ol/source/Vector'; import Feature from 'ol/Feature'; import Point from 'ol/geom/Point'; import { fromLonLat } from 'ol/proj'; import { Style, Fill, Stroke, Circle } from 'ol/style'; ``` 3.在 React 组件中创建地图和图层: ``` class MapContainer extends React.Component { constructor(props) { super(props); this.mapRef = React.createRef(); } componentDidMount() { const map = new Map({ target: this.mapRef.current, layers: [ new TileLayer({ source: new OSM(), }), ], view: new View({ center: fromLonLat([120.1551, 30.2741]), zoom: 10, }), }); const vectorSource = new VectorSource({ features: [ new Feature({ geometry: new Point(fromLonLat([120.1551, 30.2741])), }), ], }); const vectorLayer = new VectorLayer({ source: vectorSource, style: new Style({ image: new Circle({ radius: 6, fill: new Fill({ color: '#3399CC', }), stroke: new Stroke({ color: '#fff', width: 2, }), }), }), }); map.addLayer(vectorLayer); } render() { return <div ref={this.mapRef} className="map-container"></div>; } } export default MapContainer; ``` 4.在 React 组件中添加悬浮事件点击事件: ``` class MapContainer extends React.Component { constructor(props) { super(props); this.mapRef = React.createRef(); } componentDidMount() { const map = new Map({ target: this.mapRef.current, layers: [ new TileLayer({ source: new OSM(), }), ], view: new View({ center: fromLonLat([120.1551, 30.2741]), zoom: 10, }), }); const vectorSource = new VectorSource({ features: [ new Feature({ geometry: new Point(fromLonLat([120.1551, 30.2741])), name: 'My Point', }), ], }); const vectorLayer = new VectorLayer({ source: vectorSource, style: new Style({ image: new Circle({ radius: 6, fill: new Fill({ color: '#3399CC', }), stroke: new Stroke({ color: '#fff', width: 2, }), }), }), }); map.addLayer(vectorLayer); // 悬浮事件 const overlay = new Overlay({ element: document.createElement('div'), }); overlay.setMap(map); const pointerMoveHandler = function (evt) { const feature = map.forEachFeatureAtPixel(evt.pixel, function (feature) { return feature; }); if (feature) { const geometry = feature.getGeometry(); const coord = geometry.getCoordinates(); overlay.getElement().innerHTML = feature.get('name'); overlay.setPosition(coord); } else { overlay.setPosition(undefined); } }; map.on('pointermove', pointerMoveHandler); // 点击事件 map.on('click', function (evt) { const feature = map.forEachFeatureAtPixel(evt.pixel, function (feature) { return feature; }); if (feature) { console.log(feature.get('name')); } }); } render() { return <div ref={this.mapRef} className="map-container"></div>; } } export default MapContainer; ``` 在上述代码中,我们首先创建了一个 Overlay 对象并将其添加到地图中,然后在 pointermove 事件处理函数中获取当前鼠标悬浮的要素,并将其名称显示在 Overlay 上。在 click 事件处理函数中,我们可以获取被击的要素,并打印出其名称。 注意:在 React 中,我们需要使用 `ref` 来获取 DOM 元素的引用。在上述代码中,我们使用 `this.mapRef` 来获取地图容器的引用,并将其传递给 `Map` 构造函数的 `target` 参数中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值