openlayer6 getFeatureInfoUrl +map.on+map.un使用

该博客介绍了如何通过JavaScript实现地图点击事件,根据不同图层(如设备图层、井图层)查询数据,并利用axios发送请求获取JSON格式信息,进一步更新store状态,用于设备详情、井口信息等不同类型的展示。
摘要由CSDN通过智能技术生成

代码:

import {
  layers
} from "./layersJson"
import axios from "axios";
import store from '../../store'
export const clickMap = {}
clickMap.queryFun = {}
clickMap.queryFunOne = function (e, map, layer, value, type) {
  console.log('这是e', e);
  let url = layer.getSource().getFeatureInfoUrl(
    e.coordinate,
    map.getView().getResolution(),
    "EPSG:3857", {
      INFO_FORMAT: "application/json",
      FEATURE_COUNT: 1,
      QUERY_LAYERS: value
    },
  )
  axios.get(url).then(res => {
    if (res.data.numberReturned) {
      console.log('这是liyao 查询到的事件 res', res);
      console.log('这是刷新了store', res.data.features);
      console.log('这是type', type);
      if (type == "通道图层") {
        store.dispatch("tagging/setCableDetails", res.data.features);
      } else if (type == "井图层") {
        console.log('这是123456', res.data);
        store.dispatch("tagging/setEqmtOrWell", res.data.features);
        store.dispatch("tagging/setWellDetails", res.data.features[0]);
      } else if (type == "设备图层") {
        store.dispatch("tagging/setEqmtOrWell", res.data.features);
      }
    }
  })
}
clickMap.openClickAndQueryWithType = function (map, type) {
  console.log('这是this', this);
  let arr = map.getLayers().array_
  let layer = {}
  arr.forEach(item => {
    if (item.get("label") == type) {
      layer = item
    }
  })
  let value = ""
  layers.forEach(items => {
    if (items.label == type) {
      value = items.value
    }
  })
  //找到图层对应的那个layer
  console.log('这是layer', layer);
  clickMap.queryFun = function(e){
    clickMap.queryFunOne(e, map, layer, value, type)
  }
  map.on("click", clickMap.queryFun)
}
clickMap.openMutiClickAndQueryWithType = function (map, arr) {
  // console.log('这是queryFun', queryFun);
  clickMap.clickMapClose(map)
  arr.forEach(item => {
    clickMap.openClickAndQueryWithType(map, item)
  })
}
clickMap.clickMapClose = function (map) {
  console.log('这是map', map);
  if (map.listeners_.click.length) {
    console.log('这是123',);
    map.listeners_.click.forEach(item=>{
      console.log('=====这是清除了一次=====',);
      map.un("click", item)
    })
  }
}

导入:

import {clickMap}from "@/mapComponent/editFeatures/clickMapSearch"

使用: 其他同理可得

clickMap.openMutiClickAndQueryWithType(this.mapObj.map, ["设备图层"])

其中layers

在这里插入图片描述
效果:点击对应图层内某图斑查出的结果
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值