openlayers5实战--踩坑总结

1.接口返回圆心坐标和半径,直接通过new Circle(center,radius)添加圆形feature变小问题。

  解决办法:

  new  Feature()的geometry参数不能直接赋值new  Circel()得到的geometry,

  要通过‘ol/geom/Polygon.js’中的fromCircle方法将new  Circel()得到的geometry转化一遍然后赋值给new  Feature()的geometry。

  另:如果接口直接返回的坐标点画圆,则使用‘ol/geom/Polygon.js’中的circular方法。

1
2
3
4
5
6
7
8
9
10
import {circular as circularPolygon, fromCircle as fromCirclePolygon} from CC;eg1:let lng = parseFloat(d[0].lng);
let lat = parseFloat(d[0].lat);
let radius = parseFloat(d[0].radius);
let circle = new Circle(transform([lng, lat], 'EPSG:4326', 'EPSG:3857'), radius);feature = new Feature({
   position: transform([lng, lat], 'EPSG:4326', 'EPSG:3857'),
   radius: radius,
   type: 'circle',
   id: 'N',
   geometry: fromCirclePolygon(circle)
})eg2:let lng = item.coordinateList[0].lng;let lat = item.coordinateList[0].lat;let radius = item.coordinateList[0].radius;let circle4326 = circularPolygon([lng, lat],radius,64);let circle3857 = circle4326.clone().transform('EPSG:4326', 'EPSG:3857');feature = new Feature(circle3857);

  

2.测距不准问题。

  解决办法:

  使用'ol/sphere.js'中的getLength()方法计算。

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*格式化测量长度
       *@params line:  type geometry
       */
      formatLength (line) {
        //定义长度变量
        let length = getLength(line);
        let output;
        if (length > 100) {
          output = `${(Math.round(length / 1000 * 100) / 100)} 公里`;
        else {
          output = `${(Math.round(length * 100) / 100)} 米`;
        }
        return output;
      },

  

posted @   尹言覃少  阅读(6306)  评论(2编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示