openlayers点击地图获取与坐标相交的要素feature、输入坐标获取与其相交的要素

15 篇文章 1 订阅
8 篇文章 0 订阅
该代码段主要展示了如何在地图应用中实现用户点击获取与坐标相交的要素,并通过输入坐标判断所属田块。它使用了图层交互、样式设置以及异步请求来确保图层完全加载后进行操作。同时,存在错误处理,如坐标不在田块内的情况。
摘要由CSDN通过智能技术生成

1.点击地图获取与坐标相交的要素feature

//判断点击的坐标是否在田块内
  var selected = new Style({
    fill: new Fill({
      color: 'yellow'
    }),
    stroke: new Stroke({
      color: 'rgba(255, 255, 255, 0.7)',
      width: 2
    })
  });
  var select: SELECT;
  const isinField = () => {
    //点击的图层
    let layerarr = hxMap.mapInstance.getLayers().getArray();
    //select
    select = new SELECT({
      condition: click,
      style: selected,
      layers: [layerarr[1]]
    });
    hxMap.mapInstance.addInteraction(select);
    select.on('select', (e: any) => {
      let features = e.target.getFeatures().getArray();
      if (features.length > 0) {
        setisinfield(true);
        //删除上一个田块样式
        clearselectstyle();
        //重新设置样式
        features[0].setStyle(selectFieldStyle(features[0]));
        features[0].setId('selectfeature');
        formRef.setFieldsValue({ landCode: features[0].code });
      } else {
        setisinfield(false);
        message.error('设备点不位于田块内!');
      }
    });
  };
map.on('click', function (e) {
  const viewResolution = map.getView().getResolution();
  const url = dFarmlandLayer.getSource().getFeatureInfoUrl(
    e.coordinate, viewResolution, 'EPSG:4326',
    {
      'INFO_FORMAT': 'application/json',
    });
  fetch(url, {
    method: 'GET',
    mode: 'cors'
  }).then(function (response) {
    return response.json();
  }).then(function (json) {
    if(json.features.length==0)return;
    const features = new GeoJSON().readFeatures(json);
    console.log(features[0].getGeometry().getCoordinates().join(' '));
    vectorSource.clear();
    vectorSource.addFeatures(features);
    //map.getView().fit(vectorSource.getExtent());//缩放到查询出的feature
  });
});

2.输入坐标获取与其相交的要素
通过getFeaturesAtCoordinate(coordinate)方法
此处运用 getCountByFarmCode接口获取田块总数量,原因在于页面刚加载时,图层的要素不能马上渲染完毕,通过异步请求方式“延长时间”,当实际要素数量与结果一致时代表要素全部加载完毕,这时getFeaturesAtCoordinate就不会为空。

//输入的坐标,判断其所属田块并让其高亮
  const coorinField=(coordinate:any)=>{
    let layerarr = hxMap.mapInstance.getLayers().getArray();
    const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}');
    const { code } = userInfo.ffarmRespVO;
    getCountByFarmCode({farmCode:code}).then((res)=>{
       let features_=layerarr[1].getSource().getFeatures()
       if(features_.length=res.dcount){
        let features=layerarr[1].getSource().getFeaturesAtCoordinate(coordinate)
        if(features.length>0){
          //删除上一个田块样式
          clearselectstyle()
          //重新设置样式
          features[0].setStyle(selectFieldStyle(features[0]));
          features[0].setId('readfeature');
          formRef.setFieldsValue({ landCode: features[0].code });
        }else{
          message.error("坐标不位于田块内!")
        }
       }     
    })
   }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

定位算法工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值