【问题标题】:MKPolyline polylineWithPoints bug?MKPolyline polylineWithPoints 错误?
【发布时间】:2012-02-21 22:41:07
【问题描述】:

我想显示从我的位置到目的地的路线。 我使用了此代码http://code.google.com/p/octomapkit,并添加了一些日志消息。 Go 正确获取了路线坐标(大约 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;
}

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

有什么建议吗?

【问题讨论】:

    标签: ios mkmapview mkoverlay


    【解决方案1】:

    viewForOverlay 委托方法将由地图视图为它需要显示视图的每个叠加层调用。它还可以为每个叠加层多次调用它。

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

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

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

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


    此外,该 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 数组一起使用。

    【讨论】:

    • // 应该取 objectAtIndex:0 ,但为了测试我会检查它是否有更多
    • 无论如何我已经测试了建议的代码,但没有任何改进:结果相同
    • 完成了这项工作:numberOfPoints = [steps count]*2; MKMapPoint *mapPointCArray = malloc(sizeof(CLLocationCoordinate2D) * numberOfPoints);
    猜你喜欢
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多