1

我想显示从我的位置到目的地的路线。我使用了这段代码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;
}

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

有什么建议吗?

4

1 回答 1

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的点数。就这样,叠加层只会显示一半的路线。mapPointCArraysteps

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

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


viewForOverlay方法仍应如前所述进行编码。它应该只与overlay传递给它的参数一起工作,而不是直接与地图视图的overlays数组一起工作。

于 2012-02-20T19:38:51.413 回答