2011-04-30 71 views
4

我在繪製MKMapView上的MKPolylineView時出現問題。該線代表世界各地的旅行,始於紐約附近,始終向東行駛。此行從日本到舊金山的一段路程穿過太平洋,因此經度爲+/- 180。 MKPolylineView確實連接了這兩個點,但是它沿着錯誤的方向行進。也就是說,該線路從日本西部返回舊金山,而不是東部穿越太平洋。我沒有看到任何讓我指定線段連接兩點的方向的選項。無法繪製MKPolylineView交叉經度+/- 180

簡而言之,MKMapView似乎並不瞭解世界是圓形的。有沒有一種方法可以按照預期從日本東部行駛到舊金山?

我必須清楚顯示問題的屏幕截圖:
The Polyline http://www.builtlight.org/pub/MKPolylineView.png

回答

1

這似乎是的MKMapView的限制。

解決方法是將+/- 180交叉分爲「東」和「西」部分。

對於世界之旅(finish == start),您可以將折線的「西」部分放置在折線的前部。
例如:(!=完成啓動)

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; 
+0

謝謝。我正在考慮類似的解決方法,但希望可能有一些我錯過的MapKit功能。 – 2011-05-02 21:33:23