简体   繁体   English

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

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

I'm trying to draw geodesic line with arc.js . 我正在尝试用arc.js绘制测地线。 It works fine when difference between points' longitudes is less than 180. Otherwise, eg longitudes are 179 and -179, OpenLayers draws line with greatest distance. 当点的经度之差小于180时,它可以正常工作。否则,例如,经度为179和-179,OpenLayers会绘制最大距离的线。

So I found a solution for straight line: 所以我找到了直线的解决方案:

  1. Checking if difference between longitudes is more than 180 检查经度之间的差异是否大于180
  2. Calculating intersection of needed line and map edge (finding temporary point with longitude 180 or -180) 计算所需线与地图边缘的交点(找到经度为180或-180的临时点)
  3. Creating ol.geom.MultiLineString with array [[firstPoint, temporaryPoint], [temporaryPoint, secondPoint]] 使用数组[[firstPoint, temporaryPoint], [temporaryPoint, secondPoint]]创建ol.geom.MultiLineString
  4. Creating feature with line 用线创建特征

And it works fine. 而且效果很好。 But it's pretty complicated to make this trick for geodesic line with arc.js . 但是使用arc.js为测地线制作此技巧非常复杂。 The main problem is in calculation of intersection. 主要问题在于交点的计算。

在此处输入图片说明

I supposed to find solution in OpenLayers documentation or examples but there is no any example with map edge intersection. 我应该在OpenLayers文档或示例中找到解决方案,但没有地图边缘相交的任何示例。

After hours of investigation I found a solution. 经过数小时的调查,我找到了解决方案。 That looks like a trick but it works fine. 这看起来像个把戏,但效果很好。

First of all here is an original code from arc.js documentation : 首先,这是arc.js文档中的原始代码:

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});

The main issue is arc.js can't calculate coordinates for line that intersects International Date Line . 主要问题是arc.js无法计算与国际日期线相交的线的坐标。 So I decided to move points into one tile before calculating Great Circle . 因此,我决定在计算Great Circle之前将点移动到一个图块中。

I had to find longitudes offset: 我必须找到经度偏移量:

var dateLineOffset = 180 - currentPoint.longitude;

It also could be nextPoint.longitude . 也可以是nextPoint.longitude Depends on what point is placed left. 取决于放置在哪一点。

After that you can use arc.js to generate coordinates: 之后,您可以使用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});

Then you need to iterate generated coordinates and fix offset. 然后,您需要迭代生成的坐标并修复偏移量。 In my case I used map . 就我而言,我使用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);

That's it. 而已。 geodesicLine will contain proper coordinates. geodesicLine将包含适当的坐标。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM