Ios MKPolyline解析csv?

Ios MKPolyline解析csv?,ios,objective-c,csv,Ios,Objective C,Csv,基本上,我想用这段代码画一条多段线。正如您所看到的,我已经准备好了Overlay方法,我只需要知道如何在voidviewDidLoad中调用MKPolyline [更新]好的,我画了多段线。然而,这些线没有意义,它们没有顺序地连接一些管脚,然后其中4个管脚释放出一条指向地图西北部的线,我需要知道如何以有序的方式将一个管脚连接到另一个管脚 - (void)viewDidLoad { [super viewDidLoad]; NSString *csvFilePath = [[NSBundle

基本上,我想用这段代码画一条多段线。正如您所看到的,我已经准备好了Overlay方法,我只需要知道如何在voidviewDidLoad中调用MKPolyline

[更新]好的,我画了多段线。然而,这些线没有意义,它们没有顺序地连接一些管脚,然后其中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;

}

在设置数组中的所有其余坐标之前,每个坐标都添加到C数组中,此时将添加一个覆盖。这导致线路向西北方向延伸。在循环后创建并添加覆盖

以下是当前代码的摘要,仅指出了相关部分:

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
其他几点:

这个malloc在技术上是错误的:

MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * allLinedStrings.count);
它定义了一个MKMapPoint结构数组,但使用了CLLocationCoordinate2D结构的大小。由于您打算添加MKMapPoint结构,因此应改用该结构的大小:

MKMapPoint* pointArr = malloc(sizeof(MKMapPoint) * allLinedStrings.count);
它仍然以错误的方式工作的原因是,MKMapPoint和CLLocationCoordinate2D恰好大小相同

您正在使用malloc分配C数组,但没有释放内存。不再需要pointArr后,您应拨打免费电话:

仅供参考:由于您有CLLocationCoordinate2D坐标值,因此可以使用polylineWithCoordinates创建多段线,省去将其转换为MKMapPoints的麻烦

边界框的计算似乎比必要的更复杂。更简单的方法是将MKMapRect初始化为MKMapRectNull,然后使用MKMapRectUnion将每个注释的MKMapPoint添加到其中。有关示例,请参见。比这更简单的是只调用[self.mapview showAnnotations:self.mapview.annotations动画:YES];添加所有注释之后


看看MKPolyline?我知道如何应用它,当我的应用程序中有固定的坐标时,我甚至已经准备好了覆盖方法。因此,请发布覆盖方法的代码,并告诉我们如何使它与CSV文件中的数据一起工作,这对你来说是一个问题。这与CSV格式无关。您已经能够解析坐标并添加注释。你在Google或StackOverflow上搜索过MKPolyline吗?你看过MKPolyline的文档了吗?有很多例子。尝试一些东西,然后用更新的代码编辑你的问题。我的意思是,我不知道如何在CSV格式中应用它,而不是CSV格式本身就是问题所在。是的,我做了并且一直在做大量的研究,正如我所说的,你可以看到叠加方法在那里,我可以完美地用固定坐标应用它,这是在提取坐标时,没有固定数量的位置,我有麻烦。非常感谢Anna!工作完美:
MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * allLinedStrings.count);
MKMapPoint* pointArr = malloc(sizeof(MKMapPoint) * allLinedStrings.count);
self.routeLine = [MKPolyline polylineWithPoints:pointArr count:allLinedStrings.count];
[self.mapview addOverlay:self.routeLine];
free(pointArr);