MKPolyline polylineWithPoints错误?

时间:2012-02-20 19:11:03

标签: ios mkmapview mkoverlay

我想显示从我的位置到目的地的路线。 我使用了这段代码http://code.google.com/p/octomapkit,并添加了一些日志消息。 去正确得到路线坐标(大约103)。他们正在路上并填写从我的位置到目的地的路线,因此谷歌调用和解析元素是好的。 但是,当我想在MKMapView中显示时,它只显示起始折线。像15或20不多。

我会尝试从上到下发布代码:

原始代码仅采用第一个元素,我想也许如果我得到最后一个我会看到别的东西,如果是的话,我将添加所有叠加层 - 这就是for循环的原因。

否则:MKPolyline *polyLine = [self.mapView.overlays objectAtIndex:0];

#pragma mark MKMapViewDelegate
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKPolylineView *routeLineView = nil;
    // should take the objectAtIndex:0 , but for test I will check if it has more
    for(int i=0;  i <self.mapView.overlays.count; i++ ){

        MKPolyline *polyLine = [self.mapView.overlays objectAtIndex:i];       

        routeLineView = [[[MKPolylineView alloc] initWithPolyline:polyLine] autorelease];
        routeLineView.fillColor = [UIColor redColor];
        routeLineView.strokeColor = [UIColor redColor];
        routeLineView.lineWidth = 3;   
    }   

    return routeLineView;
}


-(void) routeLoadSucceededWithRoutePoints:(MKMapPoint*)routePoints count:(int)pointNumber {
    //NSLog(@"MKMapVew+OctoRoute.routeLoadSucceededWithRoutePoints count: %d", pointNumber);

    MKPolyline* routeLine = nil;
    routeLine = [MKPolyline polylineWithPoints:routePoints count:pointNumber];

    // add the overlay to the map
    if (nil != routeLine) {

        // added zoom support:
        if(shouldZoom){
            MKCoordinateRegion region = [self coordinateRegion];
            [self setRegion:region animated:YES];
        }


        //[self removeOverlays:self.overlays];
        [self addOverlay:routeLine];
    }
}

//[self removeOverlays:self.overlays];如果被评论没有效果,我希望它会创造更多:)但不是。

mapPointCArrayFromJSONString内,我正确地看到了坐标:

-(void) jsonLoadSucceededWithData:(NSData*)loadedData {
    self.routeParser.jsonStr = [[[NSString alloc] initWithData:loadedData encoding:NSUTF8StringEncoding] autorelease];
    MKMapPoint *mapPointCArray = [self.routeParser mapPointCArrayFromJSONString];

    //NSLog(@"OctoRouteService.jsonLoadSucceededWithData : %d"+ mapPointCArray.);
    [delegate routeLoadSucceededWithRoutePoints:mapPointCArray count:[self.routeParser numberOfPoints]];
}

steps有协调肯定。多次检查。

-(MKMapPoint*) mapPointCArrayFromJSONString {   
    NSArray *steps = [self routeStepsArrayFromJSONString];

    //NSLog(@"OctoRouteParser.mapPointCArrayFromJSONString steps:%d ", steps.count);
    if(steps.count == 0){
        return nil;
    }

    MKMapPoint *mapPointCArray = malloc(sizeof(CLLocationCoordinate2D) * [steps count]*2 -1);
    numberOfPoints = [steps count]-1;

    int index=0;
    for (NSDictionary *stepDict in steps) {
        [self addRouteStepDict:stepDict toMapPointCArray:mapPointCArray atIndex:index];
        index = index+2;        
    }

    return mapPointCArray;
}

我看不出为什么只是我地图上路线的第一部分,有红线的原因。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

地图视图将为显示视图所需的每个叠加层调用viewForOverlay委托方法。对于每个叠加层,它也可以多次调用它。

您的代码只需要担心创建并返回作为参数传递给该方法的单个overlay 的视图。

那里的代码应该是这样的:

MKPolylineView *routeLineView = [[[MKPolylineView alloc] initWithPolyline:overlay] autorelease];
routeLineView.fillColor = [UIColor redColor];
routeLineView.strokeColor = [UIColor redColor];
routeLineView.lineWidth = 3;   
return routeLineView;

您问题中的现有代码会返回与地图视图调用overlays的每个叠加层的viewForOverlay数组中最后一个叠加层相对应的视图。

<小时/> 此外,octomapkit在mapPointCArrayFromJSONString方法中存在错误。这些行:

MKMapPoint *mapPointCArray = malloc(sizeof(CLLocationCoordinate2D) 
                                        * [steps count]*2 -1);
numberOfPoints = [steps count]-1;

应该是:

MKMapPoint *mapPointCArray = malloc(sizeof(CLLocationCoordinate2D) 
                                        * [steps count]*2);
numberOfPoints = [steps count]*2;

原始第一行是错误的,因为它排除了最后一个线段的终点。

原始的第二行非常错误,因为numberOfPoints应该反映mapPointCArray中的点数(而不是steps数组中的最后一个索引)。它的方式,叠加只会显示一半的路线。

更改代码会更简洁,因此计算只进行一次:

numberOfPoints = [steps count]*2;
MKMapPoint *mapPointCArray = malloc(sizeof(CLLocationCoordinate2D) 
                                        * numberOfPoints);


仍应如前所述对viewForOverlay方法进行编码。它应仅适用于传递给它的overlay参数,而不能直接使用地图视图的overlays数组。