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

Javascript 如何通过OpenLayers 3中的地图边绘制测地线?,javascript,maps,coordinates,openlayers,openlayers-3,Javascript,Maps,Coordinates,Openlayers,Openlayers 3,我想用它画测地线。当点之间的经度差小于180时,它可以正常工作。否则,例如经度为179和-179,OpenLayers以最大距离绘制直线 所以我找到了直线的解决方案: 检查经度差是否大于180 计算所需直线和地图边缘的交点(查找经度为180或-180的临时点) 使用数组创建ol.geom.multileString[[firstPoint,temporaryPoint],[temporaryPoint,secondPoint]] 用直线创建要素 而且效果很好。但是,对于测地线来说,这是一个相当复

我想用它画测地线。当点之间的经度差小于180时,它可以正常工作。否则,例如经度为179和-179,OpenLayers以最大距离绘制直线

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

  • 检查经度差是否大于180
  • 计算所需直线和地图边缘的交点(查找经度为180或-180的临时点)
  • 使用数组创建
    ol.geom.multileString
    [[firstPoint,temporaryPoint],[temporaryPoint,secondPoint]]
  • 用直线创建要素
  • 而且效果很好。但是,对于测地线来说,这是一个相当复杂的技巧。主要问题是交叉口的计算


    我本想在OpenLayers文档中找到解决方案,但没有任何关于地图边缘相交的示例。

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

    首先,这里有一个源代码:

    主要问题是无法计算相交直线的坐标。所以我决定在计算之前将点移动到一个瓷砖中

    我必须找到经度偏移:

    var dateLineOffset = 180 - currentPoint.longitude;
    
    它也可以是
    nextPoint.longitude
    。取决于左侧放置的点

    之后,您可以使用生成坐标:

    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});
    
    然后需要迭代生成的坐标并修复偏移量。在我的例子中,我使用了映射

    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);
    
    就这样<代码>测地线将包含正确的坐标


    经过几个小时的调查,我找到了解决办法。这看起来像个骗局,但效果很好

    首先,这里有一个源代码:

    主要问题是无法计算相交直线的坐标。所以我决定在计算之前将点移动到一个瓷砖中

    我必须找到经度偏移:

    var dateLineOffset = 180 - currentPoint.longitude;
    
    它也可以是
    nextPoint.longitude
    。取决于左侧放置的点

    之后,您可以使用生成坐标:

    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});
    
    然后需要迭代生成的坐标并修复偏移量。在我的例子中,我使用了映射

    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);
    
    就这样<代码>测地线将包含正确的坐标


    话题外:为什么你的照片中充满敌意?@JonatasWalker不再:)哈哈哈。。好多了:-)离题:为什么你的照片中有敌意?@JonatasWalker不再:)哈哈哈。。好多了:-)