我在 MKMapView 上绘制 MKPolylineView 时遇到问题。这条线代表了一次环游世界的旅程,从纽约附近开始和结束,总是向东行驶。旅程的一站,从日本到旧金山,跨越太平洋,因此经度为 +/-180。 MKPolylineView 确实连接了这两个点,但它以错误的方向行进。也就是说,该线路从日本向西返回旧金山,而不是向东穿过太平洋。我没有看到任何选项可以让我指定一条线段应该行进的方向来连接两个点。

简单来说,MKMapView 似乎并不理解世界是圆的。有没有办法可以按预期划定界限,从日本向东行驶到旧金山?

我有一个屏幕截图,清楚地显示了问题:
The Polyline http://www.builtlight.org/pub/MKPolylineView.png

最佳答案

这似乎是 MKMapView 的限制。

一种解决方法是将 +/-180 度交叉口分成“东”和“西”部分。

对于环游世界(完成 == 开始),您可以将十字路口的“西”部分放在折线的前面。
例如:

CLLocationCoordinate2D newYorkCoord = CLLocationCoordinate2DMake(40.7141667, -74.0063889);
CLLocationCoordinate2D londonCoord = CLLocationCoordinate2DMake(51.5, -0.116667);
CLLocationCoordinate2D japanCoord = CLLocationCoordinate2DMake(36, 138);
CLLocationCoordinate2D sanFranciscoCoord = CLLocationCoordinate2DMake(37.775, -122.4183333);

CLLocationCoordinate2D japanToSFMidpointEast;
//use average calc as crude way to find midpoint...
japanToSFMidpointEast.latitude = (japanCoord.latitude + sanFranciscoCoord.latitude)/2.0;
japanToSFMidpointEast.longitude = 180;

CLLocationCoordinate2D japanToSFMidpointWest;
japanToSFMidpointWest.latitude = japanToSFMidpointEast.latitude;
japanToSFMidpointWest.longitude = -180;

int pointsCount = 6;
CLLocationCoordinate2D *points = malloc(pointsCount * sizeof(CLLocationCoordinate2D));
points[0] = japanToSFMidpointWest;
points[1] = sanFranciscoCoord;
points[2] = newYorkCoord;
points[3] = londonCoord;
points[4] = japanCoord;
points[5] = japanToSFMidpointEast;
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:points count:pointsCount];
[mapView addOverlay:polyline];
free(points);
points = NULL;

如果旅行不是环游世界(完成!= 开始),则必须使用两条折线。
这个例子从日本到SF:
CLLocationCoordinate2D japanCoord = CLLocationCoordinate2DMake(36, 138);
CLLocationCoordinate2D sanFranciscoCoord = CLLocationCoordinate2DMake(37.775, -122.4183333);

CLLocationCoordinate2D japanToSFMidpointEast;
japanToSFMidpointEast.latitude = (japanCoord.latitude + sanFranciscoCoord.latitude)/2.0;
japanToSFMidpointEast.longitude = 180;

CLLocationCoordinate2D japanToSFMidpointWest;
japanToSFMidpointWest.latitude = japanToSFMidpointEast.latitude;
japanToSFMidpointWest.longitude = -180;

int eastPointsCount = 2;
CLLocationCoordinate2D *eastPoints = malloc(eastPointsCount * sizeof(CLLocationCoordinate2D));
eastPoints[0] = japanCoord;
eastPoints[1] = japanToSFMidpointEast;
MKPolyline *eastPolyline = [MKPolyline polylineWithCoordinates:eastPoints count:eastPointsCount];
[mapView addOverlay:eastPolyline];
free(eastPoints);
eastPoints = NULL;

int westPointsCount = 2;
CLLocationCoordinate2D *westPoints = malloc(westPointsCount * sizeof(CLLocationCoordinate2D));
westPoints[0] = japanToSFMidpointWest;
westPoints[1] = sanFranciscoCoord;
MKPolyline *westPolyline = [MKPolyline polylineWithCoordinates:westPoints count:westPointsCount];
[mapView addOverlay:westPolyline];
free(westPoints);
westPoints = NULL;

关于ios - 无法绘制跨越经度 +/-180 的 MKPolylineView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5840548/

10-12 04:20