2012-11-22 41 views
0

可能重複:
Multiple MKPolyline on MKMapView iOS6如何繪製多條路線在iOS 6地圖

我有一個包含經度和緯度2個CSV文件。我想在iOS 6地圖上繪製兩條多段線或路線。我怎樣才能做到這一點??

我試過以下代碼來繪製單折線。

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"route" ofType:@"csv"]; 
NSString* fileContents = [NSString stringWithContentsOfFile:filePath]; 
NSArray* pointStrings = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

NSMutableArray* points = [[NSMutableArray alloc] initWithCapacity:pointStrings.count]; 

for(int idx = 0; idx < pointStrings.count; idx++) 
{ 
    // break the string down even further to latitude and longitude fields. 
    NSString* currentPointString = [pointStrings objectAtIndex:idx]; 
    NSArray* latLonArr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]]; 

    CLLocationDegrees latitude = [[latLonArr objectAtIndex:0] doubleValue]; 
    CLLocationDegrees longitude = [[latLonArr objectAtIndex:1] doubleValue]; 

    CLLocation* currentLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude]; 
    [points addObject:currentLocation]; 
} 

// create our route layer view, and initialize it with the map on which it will be rendered. 
_routeView = [[CSMapRouteLayerView alloc] initWithRoute:points mapView:mapView]; 

但這個代碼的問題是,我不能滾動地圖也雙擊放大將無法正常工作(基本上地圖凍結)。雖然它在Xcode 4.3(谷歌地圖)中工作正常。

回答

0

不要使用CSMapRouteLayerView,而是使用mapview本身。您可以在mapView上輕鬆繪製自定義路徑。

參見MKMapKit參考: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MapKit_Framework_Reference/_index.html

與以下類創建疊加:
MKOverlayPathView
MKPolylineViewMKPolyline爲線條。
(如果你想MKPolygonView和'MKPolygon多邊形。)

要使用的MapView使用這些類,請使用以下方法/委託:

的MKMapView:添加疊加到地圖
– addOverlay:

MKMapViewDelegate:管理覆蓋查看
– mapView:viewForOverlay:
– mapView:didAddOverlayViews: