首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKPolyline解析csv?

MKPolyline解析csv?
EN

Stack Overflow用户
提问于 2015-03-06 16:20:10
回答 1查看 120关注 0票数 1

基本上,我想用这段代码画一条折线。如您所见,我已经准备好了覆盖方法,我只需要知道如何在(void)viewDidLoad中调用viewDidLoad

更新OK,所以我设法画了多条线。然而,这些线是没有意义的,它们连接一些没有顺序的引脚,然后4个引脚释放出一条指向地图西北的线,我需要知道如何使它以有序的方式将一个引脚连接到另一个引脚上。

代码语言:javascript
复制
- (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;

}

另外,我的csv文件如果有任何帮助

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-09 02:04:36

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

下面是对当前代码的总结,并仅指出相关部分:

代码语言:javascript
复制
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中,这意味着为每个坐标添加了一个polyline覆盖。

但是,在每次迭代时,pointArr数组尚未完全填充:

  • pointArr[0]设置为location 1时,还没有将pointArr[1]设置为pointArr[4],并且包含零值或随机值。
  • pointArr[1]设置为位置2时,还没有将pointArr[2]设置为pointArr[4]并包含零值或随机值等.

在每个坐标/迭代中,pointArr部分设置为这样,增加了折线,导致随机线进入西北方向。

相反,只需在for-loop之后和pointArr完全填充之后创建并添加polyline覆盖:

代码语言:javascript
复制
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); 它仍然“工作”错误的原因是因为MKMapPointCLLocationCoordinate2D恰好是相同的大小。
  2. 您正在使用malloc分配C数组,但不释放内存。在不再需要pointArr之后,应该调用free: self.routeLine = MKPolyline polylineWithPoints:pointArr count:allLinedStrings.count;self.mapview addOverlay:self.routeLine;free(pointArr);
  3. 只是FYI:因为您有CLLocationCoordinate2D坐标值,所以您可以使用polylineWithCoordinates创建polyline,并省去将它们转换为MKMapPoint的麻烦。
  4. 包围盒的计算似乎比必要的要复杂。更简单的方法是将MKMapRect初始化为MKMapRectNull,然后使用MKMapRectUnion将每个注释的MKMapPoint添加到它。有关示例,请参见this answer。更简单的是,在添加了所有注释之后,只需调用[self.mapview showAnnotations:self.mapview.annotations animated:YES];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28903005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档