2012-11-22 40 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: