我有两个包含纬度和经度的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 :(得分:0)
不要使用CSMapRouteLayerView,而是使用mapview本身。您可以在mapView上轻松绘制自定义路径。
参见MKMapKit参考: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MapKit_Framework_Reference/_index.html
使用以下类创建叠加层:
MKOverlayPathView
或
对于行,MKPolylineView
和MKPolyline
(如果你想要MKPolygonView
和`多边形的MKPolygon。)
要将这些类与mapView一起使用,请使用以下方法/ delegate:
MKMapView:向地图添加叠加层
– addOverlay:
MKMapViewDelegate:管理叠加视图
– mapView:viewForOverlay:
– mapView:didAddOverlayViews: