无法绘制MKPolylineView横经度+/- 180

时间:2011-04-30 08:19:05

标签: ios mapkit mkoverlay

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

简而言之,MKMapView似乎并不了解世界是圆的。有没有办法按照预期划线,从日本东行到旧金山?

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

1 个答案:

答案 0 :(得分: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;


如果旅行不在世界各地(完成!=开始),您将不得不使用两条折线 这个例子从日本到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;