我想要显示从我所在位置到目的地的路线。我采用了这段代码http://code.google.com/p/octomapkit,并添加了一些日志消息。Go正确地获得了路线坐标(大约103)。他们正在从我的位置到目的地的途中,所以google调用和解析元素是很好的。但是当我想在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];都不起作用,我希望它能创建更多:) -but not。
在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;
}我不明白为什么我的地图上只有路线的第一部分,有红线。
有什么建议吗?
发布于 2012-02-21 03:38:51
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数组。
https://stackoverflow.com/questions/9366972
复制相似问题