ArcGIS JS 4X MapImageLayer的graphic鼠标点击交互

2 篇文章 0 订阅
博客介绍了如何在ArcGIS中使用MapImageLayer替代DynamicMapServiceLayer,并通过FeatureLayer实现图层的鼠标交互。文章详细阐述了如何添加图层,以及封装获取点击和悬停图形对象的方法,提供了示例代码供参考。
摘要由CSDN通过智能技术生成

4X去掉了之前的ArcGISDynamicMapServiceLayer,替换的MapImageLayer更能更强大一些,但是MapImageLayer没有图形的鼠标交互,对需要添加图形交互的MapServer图层需要用FeatureLayer加到map对象上。

    // 包含图层url等信息的对象数组
    layerList.reverse().forEach((ele: any) => {
      let layer;
      if (ele.type === 'feature') {
        // arcgisFuns是自己封装的arcGIS组件对象 等同于new FeatureLayer
        layer = arcgisFuns.FeatureLayer({
          // 自己封装的一个拼接图层url的方法 
          // 示例:http://ip:6080/arcgis/rest/services/XXX/${ele.name}/MapServer
          url: layerConfig.getServerPath(ele.name, 'MapServer') + '/0',
          id: ele.name,
          visible: false,
          outFields: ['*'], // 这里需要自行设置输出字段,默认不全
        });
      } else {
        layer = arcgisFuns.MapImageLayer({
          url: layerConfig.getServerPath(ele.name, 'MapServer'),
          id: ele.name,
          visible: false,
        });
      }
      _t.view.map.add(layer);
    });

然后是封装的一个获取鼠标点击图层内graphic的方法。GraphicsLayer和FeatureLayer可以直接获取,MapImageLayer因为上边说的原因需要用FeatureLayer替换。

  /**
   * 获取图层点击的图形对象的简要方法 一次调用终生有效,下次调用会先移除上次的
   * @param view arcgis的mapview
   * @param layer 目标图层
   * @param onClickCallBack 回调
   */
  arcgisFuns.getClickGraphic = (view: any, layer: any, onClickCallBack: (graphic: any) => void) => {
    if (view.customEventHandle) {
      view.customEventHandle.remove();
      view.customEventHandle = null;
    }
    view.customEventHandle = view.on('click', (event: any) => {
      view.hitTest(event).then((response: any) => {
        if (response.results.length) {
          const graphics = response.results.filter((result: any) => {
            return result.graphic.layer === layer;
          });

          if (graphics.length > 0) {
            // 这里graphics[0]包含了graphic和mapPoint两个对象
            // 这里只输出graphic,都需要的话可以自行修改
            onClickCallBack(graphics[0].graphic);
          }
        }
      });
    });
  };

调用示例

  private startClickListen(id: string) {
    // 通过id筛出待监听点击的图层
    const aimLayer = this.view.map.findLayerById(id);
    // this.view是arcgis的mapview对象
    arcgisFuns.getClickGraphic(this.view, aimLayer, (graphic: any) => {
      // todo ...
    });
  }

附赠一个光标经过图层graphic的函数

  /**
   * 获取光标经过的graphic
   * @param view arcgis的mapview
   * @param layer 目标图层
   * @param onHitGraphic 经过时的回调
   * @param onMoveOut 移出时的回调
   */
  arcgisFuns.getHoverGraphic = (view: any, layer: any, onHitGraphic: any, onMoveOut?: any) => {
    if (view.hoverHandle) {
      view.hoverHandle.remove();
      view.hoverHandle = null; // handler
    }
    view.hoverHandle = view.on('pointer-move', (event: any) => {
      view.hitTest(event, { exclude: view.graphics }).then((response: any) => {
        if (response.results.length) {
          const graphics = response.results.filter((result: any) => {
            return result.graphic.layer === layer;
          });
          if (graphics.length > 0) {
            onHitGraphic(graphics[0]);
          }
        } else {
          onMoveOut();
        }
      });
    });
  };

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值