在geojson图层上增加点击弹窗,并添加删除功能

本文介绍了如何在GIS应用中创建一个带有自定义标题和删除功能的弹窗模板,包括设置popupTemplate、使用event.graphic.getAttribute()区分图形、监听popup的删除动作并调用deleteGraphic方法删除选定图形的过程。
摘要由CSDN通过智能技术生成

显示效果

完整代码

// 创建弹窗模板
      const popupTemplate = {
        title: function(event) {
          return popupTile[action] + '-------' + event.graphic.getAttribute('OBJECTID')
        },
        actions:[
          {
            title: '删除',
            id: 'delete-graphic',
            className: 'esri-icon-trash'
          }
        ]
      }
// 把模板设置到geojsonLayer上
     const GeojsonLayer = new esri.GeoJSONLayer({
        url: url,
        copyright: 'RHgis',
        id: layerID,
        renderer: customRenderer || renderer,
        popupTemplate
      })
// 监听popup上的按钮事件
      this.mapView.popup.on("trigger-action", event => {
        console.log(event)
        const { id } = event.action
        if(id === 'delete-graphic')
          this.deleteGraphic()
      })
//     删除
    deleteGraphic() {
      const selectedFeature = this.mapView.popup.selectedFeature
      const { layer } = selectedFeature
      layer.applyEdits({
        deleteFeatures: [selectedFeature]
      }).then(res => {
        console.log(res)
      })
    },

详细过程

1. 实例时添加属性popupTemplate

popupTemplate: {
          title: action,
          content: '点击了'
        }

2. 设置title用于查询到多个graphic时能够区分

title: function(event) {
            return popupTile[action] + event.graphic.getAttribute('OBJECTID')
          }

3. 设置actions属性来创建删除按钮

actions:[
    {
        title: '删除',
        id: 'deleteGraphic',
        className: 'esri-icon-trash'
    }
]

4. 通过action属性创建删除按钮

actions:[
    {
        title: '删除',
        id: 'deleteGraphic',
        className: 'esri-icon-trash'
    }
]

5. 在map上监听popup上的操作,然后调用对应的方法

this.mapView.popup.on("trigger-action", event => {
        const { id } = event.action
        if(id === 'delete-graphic')
          this.deleteGraphic()
      })

6. 删除指定位置的graphic的方法

deleteGraphic() {
      const selectedFeature = this.mapView.popup.selectedFeature
      const { layer } = selectedFeature
      layer.applyEdits({
        deleteFeatures: [selectedFeature]
      }).then(res => {
        console.log(res)
      })
    },

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值