Openlayers API运用相关功能记录

本文介绍使用OpenLayers库进行地图操作的各种方法,包括事件监听、默认展示配置、坐标转换、多边形中心点获取等。此外还介绍了如何加载GeoJSON数据、设置填充透明度及实现图层高亮等功能。

1.事件

#鼠标更改视野事件
map.getView().on('change:resolution', function(){})

#鼠标拖动事件,地图拖动结束后执行
map.on("moveend", function (evt) {});

#鼠标单击事件
map.on('singleclick', function(evt) {
    // 获取当前鼠标点击的feature
    var feature = map.forEachFeatureAtPixel(evt.pixel, function(
            feature, layer) {
        return feature;
    });
})

#鼠标双击事件
map.on("dblclick", function (evt) {});

#鼠标悬浮图层高亮并添加阴影
## 添加阴影的方式
lightLayer.on('precompose', (evt) => {
  evt.context.shadowBlur = 25;
  evt.context.shadowColor = 'black';
});
lightLayer.on('postcompose', (evt) => {
  evt.context.shadowBlur = 0;
  evt.context.shadowColor = 'black';
});

##鼠标悬浮地图事件
map.on('pointermove', ()=>{});

鼠标悬浮的时候,将当前悬浮的feature添加到lightLayer中并修改样式,就能实现高亮效果

2.默认展示属性

controls: ol.control.defaults({ attribution: false, zoom: false, rotate: false })

3.获取多边形中心点

var extent = feature.getGeometry().getExtent();
var center = ol.extent.getCenter(extent);
			

4.移动到指定经纬度及视野等级

view.setZoom(16);
view.animate({
    center: center,
    duration: 0
});

5.填充设置透明度

let color = ol.color.asArray('#000');
color= color.slice();
color[3] = 0.4; // 透明度

6.加载Geojson数据对象

let features = new GeoJSON().readFeatures(copyWenzhou_border, {
    dataProjection: 'EPSG:4326',
    featureProjection: 'EPSG:3857',
  });
  let vectorLayer = new layerVector({
    source: new sourceVector({
      features: features,
    }),
    zIndex: 100,
    style() {
      return wenZhouPolygonLayerStyle;
    },
  });


var format_wenzhou1_0 = new ol.format.GeoJSON();
var features_wenzhou1_0 = format_wenzhou1_0.readFeatures(json_wenzhou1_0, 
            {dataProjection: 'EPSG:4326', featureProjection: 'EPSG:3857'});

7.Geojson数据对象Polygon转StringLine

copyWenzhou_border.features[0].geometry.type = 'LineString';
copyWenzhou_border.features[0].geometry.coordinates =
    copyWenzhou_border.features[0].geometry.coordinates[0];

8.默认经纬度

// web地图默认是3857, 我们用的坐标系是4326, 需要转一下

console.log(ol.proj.transform([12957588.728337044, 4851421.175183355],"EPSG:3857","EPSG:4326"));
console.log(ol.proj.transform([116.4, 39.9],"EPSG:4326","EPSG:3857"));

9.fit偏移

默认情况下,会根据展示内容居中展示

const vectorSource = new ol.source.Vector({features: AllMarks});
map.getView().fit(vectorSource.getExtent());

但有时候地图上会展示一些内容,如table 、echarts 等等,导致地图内容被遮住,这时候就需要将展示内容进行偏移,方式如下:

const vectorSource = new ol.source.Vector({features: AllMarks});
const extent = vectorSource.getExtent();
const width = extent[2] - extent[0]
const height = extent[3] - extent[1]

// 经纬度距离
// 获取一半屏幕宽度 和 屏幕高度
const bWidth = width / 2
const bHeight = height / 2

// 自定义偏移量				
extent[0] = extent[0] - bWidth + bWidth
extent[2] = extent[2] + bWidth + bWidth
extent[1] = extent[1] - bHeight
extent[3] = extent[3] + bHeight

map.getView().fit(extent);

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值