如何通过 OpenLayers 3 中的地图边缘绘制测地线?

How do I draw a geodesic line through map edges in OpenLayers 3?

我正在尝试用 arc.js 绘制测地线。当点之间的经度差小于 180 时,它工作正常。否则,例如经度为 179 和 -179,OpenLayers 绘制距离最大的线。

所以我找到了直线的解决方案:

  1. 检查经度之间的差异是否大于 180
  2. 正在计算所需线与地图边缘的交点(寻找经度180或-180的临时点)
  3. 正在用数组 [[firstPoint, temporaryPoint], [temporaryPoint, secondPoint]]
  4. 创建 ol.geom.MultiLineString
  5. 使用行
  6. 创建特征

而且效果很好。但是用 arc.js 制作测地线的这个技巧是相当复杂的。主要问题在交集的计算上。

我应该在 OpenLayers 文档或 examples 中找到解决方案,但没有任何地图边缘相交的示例。

经过几个小时的调查,我找到了解决方案。这看起来像个把戏,但效果很好。

首先这里是来自arc.js documentation的原始代码:

var start = { x: currentPoint.longitude, y: currentPoint.latitude },
    end = { x: nextPoint.longitude, y: nextPoint.latitude },
    generator = new arc.GreatCircle(start, end),
    arcLine = generator.Arc(1000 ,{offset:10});

主要问题是arc.js can't calculate coordinates for line that intersects International Date Line. So I decided to move points into one tile before calculating Great Circle

我必须找到经度偏移量:

var dateLineOffset = 180 - currentPoint.longitude;

也可能是nextPoint.longitude。取决于放在左边的点。

之后可以使用arc.js生成坐标:

var start = { x: -180, y: currentPoint.latitude },
    end = { x: nextPoint.longitude + dateLineOffset, y: nextPoint.latitude },
    generator = new arc.GreatCircle(start, end),
    arcLine = generator.Arc(1000 ,{offset:10});

然后你需要迭代生成的坐标并修复偏移量。在我的例子中,我使用了 map.

var coordinatesWithOffset = arcLine.geometries[0].coords,
    geodesicLineCoordinates = coordinatesWithOffset.map(function(coord) {
        return ol.proj.fromLonLat([coord[0] - dateLineOffset, coord[1]]);
    }),
    geodesicLine = ol.geom.LineString(geodesicLineCoordinates);

就是这样。 geodesicLine 将包含适当的坐标。