gpt4 book ai didi

ios - MKPolyline polylineWithPoints 错误?

转载 作者:行者123 更新时间:2023-11-29 13:40:20 26 4
gpt4 key购买 nike

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

步骤肯定是协调的。检查了很多次。

-(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;
}

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

有什么建议吗?

最佳答案

viewForOverlay委托(delegate)方法将由 map View 为它需要显示 View 的每个叠加层调用。它也可能为每个覆盖层多次调用它。

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

代码应该是这样的:

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

您问题中的现有代码返回对应于恰好在 overlays 中的最后一个叠加层的 View 。 map View 调用的每个叠加层的数组 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应该反射(reflect) mapPointCArray 中的点数(不是 steps 数组中的最后一个索引)。原来,叠加层只会显示一半的路线。

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

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


viewForOverlay方法仍应如前所述进行编码。它应该只适用于 overlay参数传递给它而不是直接与 map View 的 overlays 一起传递数组。

关于ios - MKPolyline polylineWithPoints 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9366972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com