使用API​​中的坐标绘制路线

时间:2014-12-16 22:28:28

标签: ios iphone mkmapview

我正在使用transportAPI来获取从一个点到另一个点的路由。我打算在mapview中选择一次路线。选择一个首选路径,迭代到它的数组以获得所有坐标。

问题是我无法使用MKPolyline绘制路线。我的代码如下;

- (void)viewDidLoad
{
    [super viewDidLoad];

  //  NSLog(@"selected route: %@",selectedRoute.routeParts);
    NSMutableArray * cordArray = [[NSMutableArray alloc]init];
    for (int i = 0;i < selectedRoute.routeParts.count; i++)
    {
        NSMutableArray * cords =[[selectedRoute.routeParts objectAtIndex:i]objectForKey:@"coordinates"];

         for (int c = 0;c < cords.count; c++)
         {

             double latitude = [[[cords objectAtIndex:c]objectAtIndex:1]doubleValue];
             double longitude = [[[cords objectAtIndex:c]objectAtIndex:0]doubleValue];
             CLLocation * location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
             [cordArray addObject:location];
             NSLog(@"coordiantes %@",cordArray);
         }
    }

            CLLocationCoordinate2D coordinates[cordArray.count];
            //int i = 0;
            int numPoints = [cordArray count];
            for (int i = 0; i<numPoints;i++)
            {
                CLLocation * current = [cordArray objectAtIndex:i];
                coordinates[i] = current.coordinate;
            }

            // create a polyline with all cooridnates
            self.polyline = [MKPolyline polylineWithCoordinates:coordinates count:numPoints];
            [self.mapView addOverlay:self.polyline];
            [mapView setDelegate:self];
}


- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolyline class]])
    {
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];

        renderer.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
        renderer.lineWidth   = 3;

        return renderer;
    }

    return nil;
} 

坐标数组的片段;

oordiantes (
    "<+51.51756000,-0.12033000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 12/16/14, 10:04:19 PM Greenwich Mean Time"
)
2014-12-16 22:04:19.545 MeetUp[16434:1366070] coordiantes (
    "<+51.51756000,-0.12033000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 12/16/14, 10:04:19 PM Greenwich Mean Time",
    "<+51.51865000,-0.12089000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 12/16/14, 10:04:19 PM Greenwich Mean Time"
)
2014-12-16 22:04:19.547 MeetUp[16434:1366070] coordiantes (
    "<+51.51756000,-0.12033000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 12/16/14, 10:04:19 PM Greenwich Mean Time",
    "<+51.51865000,-0.12089000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 12/16/14, 10:04:19 PM Greenwich Mean Time",
    "<+51.51912000,-0.12129000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 12/16/14, 10:04:19 PM Greenwich Mean Time"
)

我认为返回的坐标应该只是如此:

[+51.51756000,-0.12033000]
[51.51865000,-0.12089000]
[+51.51912000,-0.12129000]

我做错了什么?

1 个答案:

答案 0 :(得分:2)

尝试在添加叠加层之前设置地图的委托,例如:

// create a polyline with all cooridnates
self.polyline = [MKPolyline polylineWithCoordinates:coordinates count:numPoints];
[mapView setDelegate:self]; // <-- move this line to before addOverlay
[self.mapView addOverlay:self.polyline];

至于你的coordiantes数组日志,它是正确的。