2015-03-06 4 views
1

В принципе, я хочу нарисовать полилинию с помощью этого кода. Как вы можете видеть, у меня есть метод 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 
+0

Посмотрите на 'MKPolyline'? – Larme

+0

Я знаю, как применять его, когда у меня фиксированные координаты в моем приложении, у меня даже есть метод Overlay. – J320

+0

Итак, отправьте код для вашего метода Overlay и расскажите, что он делает, чтобы он работал с данными из файла CSV, что является проблемой для вас. –

ответ

0

An overlay is being added качестве each coordinate is added к в array C (before all остальным в coordinates в массив). Это приводит к тому, что линии идут на северо-запад. Создайте и добавьте наложение после цикла.

Вот краткое изложение текущего кода только с соответствующими частями отметили:

MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * allLinedStrings.count); 

for(int idx = 0; idx < allLinedStrings.count; idx++) 
{ 
    //some code that gets "infos" (current coordinate's data)... 

    if ([infos count] > 1) 
    { 
     //some code that creates and adds an annotation... 

     MKMapPoint point = MKMapPointForCoordinate(coordinate); 

     //some code that calculates the "bounding box" (irrelevant)... 

     pointArr[idx] = point;  
    } 

    self.routeLine = [MKPolyline polylineWithPoints:pointArr count:allLinedStrings.count]; 

    //_routeRect = ... (irrelevant) 

    [self.mapview addOverlay:self.routeLine]; 

} //end of for-loop 

Обратите внимание, что addOverlay вызова внутри for -loop что означает полилинию наложение добавляется для каждый координаты.

Но на каждой итерации pointArr массив еще полностью не заселен:

  • Когда pointArr[0] устанавливается на место 1, pointArr[1] к pointArr[4] еще не установлены и содержат ноль или случайные значения.
  • Когда pointArr[1] установлен на место 2, pointArr[2] к pointArr[4] еще не установлены и содержат либо ноль или случайные значения, и т.д. ...

С pointArr частично установлено, как это в каждой координате/итераций добавляется полилиния, в результате чего случайные линии попадают на северо-запад.


Вместо этого можно создать и добавить полилинию накладку раз и послеfor -loop и после pointArr полностью заполнен:

MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * allLinedStrings.count); 

for(int idx = 0; idx < allLinedStrings.count; idx++) 
{ 
    //some code that gets "infos" (current coordinate's data)... 

    if ([infos count] > 1) 
    { 
     //some code that creates and adds an annotation... 

     MKMapPoint point = MKMapPointForCoordinate(coordinate); 

     //some code that calculates the "bounding box" (irrelevant)... 

     pointArr[idx] = point;  
    } 

    //_routeRect = ... (irrelevant) 

} //end of for-loop 

self.routeLine = [MKPolyline polylineWithPoints:pointArr count:allLinedStrings.count]; 

[self.mapview addOverlay:self.routeLine]; 


Несколько моментов:

  1. malloc технически неправильно:

    MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * allLinedStrings.count); 
    

    Это определение массива MKMapPoint структур, но используя размер CLLocationCoordinate2D структур.Так как вы намерены добавить MKMapPoint структур, следует использовать размер этой структуры вместо:

    MKMapPoint* pointArr = malloc(sizeof(MKMapPoint) * allLinedStrings.count); 
    

    Причина, по-прежнему «работает» неправильный путь, потому что MKMapPoint и CLLocationCoordinate2D случается быть того же размера.

  2. Вы назначаете массив C malloc, но не освобождаете память. После pointArr больше не нужен, вы не должны вызывать free:

    self.routeLine = [MKPolyline polylineWithPoints:pointArr count:allLinedStrings.count]; 
    [self.mapview addOverlay:self.routeLine]; 
    free(pointArr); 
    
  3. Просто FYI: Поскольку у вас есть CLLocationCoordinate2D значения координат для начала, вы можете просто создать полилинию с помощью polylineWithCoordinates и избавляют преобразования их в MKMapPoint s.

  4. Расчет ограничивающей коробки кажется более сложным, чем необходимо. Проще инициализировать MKMapRect до MKMapRectNull, а затем добавить к нему каждую аннотацию MKMapPoint с использованием MKMapRectUnion. См. Пример this answer. Еще проще, чем просто позвонить [self.mapview showAnnotations:self.mapview.annotations animated:YES]; после добавления всех аннотаций.

+0

Спасибо, Анна! Работает отлично :) – J320

Смежные вопросы