ol地图小方法

起因是ol原生的位置转换使用起来有误差,自己手搓一个

   // 定义一个函数来计算两点之间的直线距离
        calculateDistance(point1, point2) {
            const deltaX = point2[0] - point1[0]; // 计算横向距离差
            const deltaY = point2[1] - point1[1]; // 计算纵向距离差
            const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY); // 应用欧几里得距离公式
            return distance;
        },

数据编码格式转换

import { transform, get } from "ol/proj";
//坐标,源投影,目标投影
transform(coordinate, 'EPSG:4326', 'EPSG:3857')
//坐标,源投影,目标投影
transform(coordinate, 'EPSG:4326', 'EPSG:3857')
//如果是 geoJSON数据格式转换,则MultiPolygon和Polygon方法可以直接点出transform方法直接转换。
geometry: new MultiPolygon(
    lineData.geometry.coordinates
).transform("EPSG:4326", "EPSG:3857")
//或者
geometry: new Polygon(
	lineData.geometry.coordinates
).transform("EPSG:4326", "EPSG:3857")



//使用 new GeoJSON().readFeatures方法时,默认的是4326编码,如果想转换编码格式的话加一个参数
 const fts = new GeoJSON().readFeatures(data, {
                dataProjection: 'EPSG:4326',
                featureProjection: 'EPSG:3857',
            });

设置弹窗偏移,默认的弹窗是左上角出现在我们的点击位置,不太美观需要相对调整一下 

  import { Overlay} from 'ol';

const popupOverlay = new Overlay({
                element: document.getElementById("contextmenu"),
                positioning: 'bottom-center',
                stopEvent: false,
                visible: false,
                offset: [-150, -200], // 弹窗位置的偏移量,向上偏移 200 像素,向右150
            });

持续更新

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值