4

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

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

我有一个清楚地显示问题的屏幕截图:
折线 http://www.builtlight.org/pub/MKPolylineView.png

4

1 回答 1

2

这似乎是 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;


如果旅行不是环游世界(完成!= 开始),则必须使用两条折线。
这个例子从日本到旧金山:

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;
于 2011-04-30T14:07:27.420 回答