八、OpenLayer一些常用的方法总结

1、获取地图的视角范围

this.map.getView().calculateExtent(this.map.getSize());

2、获取地图分辨率

// 获取地图视图的分辨率
var resolution = this.map.getView().getResolution();

3、定位

 //定位视图
this.map.getView().fit( [extentArr[0], extentArr[1], extentArr[2], extentArr[3]],  {
  padding: [0, 0, 0, 0],  //上, 右,下,左
  duration: 500,
});

//定位中心点
this.map.getView().fit(center, {
  duration: 1000, //动画的持续时间,
  maxZoom: 18,
  callback: null,
});

4、获取所有图层

this.map.getLayers().getArray()

5、地图移动监听

this.map.on("moveend",(e)=>{
   //方法
})

6、缩放监听事件

this.map.getView().on('change:resolution', (event) => {
   //方法
});

7、鼠标点击

    this.map.on("click", (ev) => {
      const mouse = ev.coordinate; // 鼠标点击的坐标位置
    })

8、移除双击放大

 //禁用双击放大事件
 let doubleClickZoom = new ol.interaction.DoubleClickZoom();
 let doubleClickZoomArr = this.map
    .getInteractions()
    .array_.filter((item) => {
        return item.constructor.name === doubleClickZoom.constructor.name;
    });
 this.map.removeInteraction(doubleClickZoomArr[0]);

9、渲染监听,适用于监听图层绘制,以及实时改变自己定义的一些元素的位置

//监听地图 去实时计算一些东西,比如位置
this.map.on("postrender", function (ev) {

})

//监听图层  去改变栅格图层的一些样式
myLayer.on("postrender", (e) => {
  const vectorContext = getVectorContext(e);
  e.context.globalCompositeOperation = "destination-in";
  clipLayer.getSource().forEachFeature(function (feature) {
    vectorContext.drawFeature(feature, style);
  });
  e.context.globalCompositeOperation = "source-over";
});
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值