В принципе, я хочу нарисовать полилинию с помощью этого кода. Как вы можете видеть, у меня есть метод Overlay, мне нужно только знать, как вызвать MKPolyline в (void) viewDidLoadMKPolyline синтаксический анализ csv?
[UPDATE] ОК, поэтому мне удалось нарисовать полилинии. Тем не менее, линии не имеют смысла, они соединяют некоторые контакты без заказа, а затем из 4 контактов выводят линию, направленную к северо-западу от карты, мне нужно знать, как сделать ее одной штырькой на другую в упорядоченный способ.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *csvFilePath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"csv"];
NSString *dataStr = [NSString stringWithContentsOfFile:csvFilePath encoding:NSUTF8StringEncoding error:nil];
NSArray* allLinedStrings = [dataStr componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
MKMapPoint northEastPoint;
MKMapPoint southWestPoint;
MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * allLinedStrings.count);
for(int idx = 0; idx < allLinedStrings.count; idx++)
{
NSString* currentPointString = [allLinedStrings objectAtIndex:idx];
NSArray* infos = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
if ([infos count] > 1)
{
NSString * latitude = [infos objectAtIndex:1];
NSString * longitude = [infos objectAtIndex:2];
NSString * Description =[infos objectAtIndex:3];
NSString * address = [infos objectAtIndex:4];
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude.doubleValue;
coordinate.longitude = longitude.doubleValue;
Location *annotation = [[Location alloc] initWithName:Description address:address coordinate:coordinate] ;
[mapview addAnnotation:annotation];
MKMapPoint point = MKMapPointForCoordinate(coordinate);
//
// adjust the bounding box
//
// if it is the first point, just use them, since we have nothing to compare to yet.
if (idx == 0) {
northEastPoint = point;
southWestPoint = point;
}
else
{
if (point.x > northEastPoint.x)
northEastPoint.x = point.x;
if(point.y > northEastPoint.y)
northEastPoint.y = point.y;
if (point.x < southWestPoint.x)
southWestPoint.x = point.x;
if (point.y < southWestPoint.y)
southWestPoint.y = point.y;
}
pointArr[idx] = point;
}
self.routeLine = [MKPolyline polylineWithPoints:pointArr count:allLinedStrings.count];
_routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);
[self.mapview addOverlay:self.routeLine];
}
}
}
}
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView
rendererForOverlay:(id <MKOverlay>)overlay
{
MKPolylineRenderer *renderer =
[[MKPolylineRenderer alloc] initWithPolyline:overlay];
renderer.strokeColor = [[UIColor orangeColor] colorWithAlphaComponent:1];
renderer.lineWidth = 6.0;
renderer.lineDashPattern = @[@2, @10];
renderer.alpha = 0.5;
return renderer;
}
Also, my file csv если its of any help
01,51.751782,-0.238992, Location 1, 1st Stop
02,51.815020,-0.200418, Location 2, 2nd Stop
03,51.755462,-0.340392, Location 3, 3rd Stop
04,51.660507,-0.389374, Location 4, 4th Stop
05,51.798323,-0.081622, Location 5, 5th Stop
Посмотрите на 'MKPolyline'? – Larme
Я знаю, как применять его, когда у меня фиксированные координаты в моем приложении, у меня даже есть метод Overlay. – J320
Итак, отправьте код для вашего метода Overlay и расскажите, что он делает, чтобы он работал с данными из файла CSV, что является проблемой для вас. –