首页 > 解决方案 > OpenLayers 计算两个坐标之间的距离给出了错误的答案

问题描述

我有一个 GPX 文件,我正在尝试使用以下函数计算两点之间的总距离

import { LineString } from 'ol/geom'

getDistance(previous, next){
   const coords = [ previous, next ]
   const line = new LineString(coords);
   return line.getLength();
},

我知道getLength()应该以米为单位返回,但我得到了一些非常小的数字,当我添加所有距离时,我离 gpx 文件的 2.6 公里还很远。

我的数据看起来像这样:[[0.194655, 38.730435], [0.194435, 38.730522]]我为此得到的距离是0.0002365776828023007

标签: openlayers

解决方案


输出对我来说似乎是正确的,它是以度为单位的线串长度。

要获得以米为单位的距离,请getDistance尝试ol.Sphere

https://openlayers.org/en/latest/apidoc/module-ol_sphere.html#.getDistance

编辑:您将getLengthLineString 的getLength方法与 ol.sphere的方法混淆了


推荐阅读