我正在使用iOS 7并扩展了MKOverlayPathRenderer,以便在MKMap上绘制自定义叠加层。
我已经实现了MKOverlayPathRenderer类的以下方法,并且NSLog输出显示在我期望的时候调用代码来绘制上下文。但是,自定义叠加层根本不会显示在地图上。
---CustomPathRenderer extends MKOverlayPathRenderer----
-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)ctx
{
CGContextSetAlpha(ctx, 0.9);
if([self.overlay isKindOfClass:[CustomPathOverlay class]]){
CustomPathOverlay *path = (CustomPathOverlay *)self.overlay;
for(int idx = 1; idx < path.selected.positions.count; idx++)
{
CGContextBeginPath(ctx);
Position* firstPosition = (Position *)[path.selected.positions objectAtIndex:idx-1];
CGPoint firstpoint = [self.mapView convertCoordinate:firstPosition.location toPointToView:self.mapView];
CGContextMoveToPoint(ctx, firstpoint.x , firstpoint.y);
Position* pos = (Position *)[flightPath.selectedAircraft.positions objectAtIndex:idx];
CGPoint point = [self.mapView convertCoordinate:pos.location toPointToView:self.mapView];
NSLog(@"Drawing X:%f Y:%f",point.x,point.y);
CGContextAddLineToPoint(ctx, point.x, point.y);
CGContextSetStrokeColorWithColor(ctx,[UIColor redColor].CGColor);<-- THIS WILL BECOME A FUNCTION OF A POSITION PROPERTY RATHER THAN JUST RED.
CGContextSetLineWidth(ctx, 0.5 * MKRoadWidthAtZoomScale(zoomScale));
CGContextStrokePath(ctx);
}
// Last segment to the last known position.
CGContextBeginPath(ctx);
Position* lastPosition = (Position *)[path.selected.positions lastObject];
CGPoint lastPoint = [_mapView convertCoordinate:lastPosition.location toPointToView:self.mapView];
CGContextMoveToPoint(ctx, lastPoint.x , lastPoint.y);
Position *estimatedPosition = [path.selected.position estimatePosition];
CGPoint endPoint = [self.mapView convertCoordinate:estimatedPosition.location toPointToView:self.mapView];
CGContextAddLineToPoint(ctx, endPoint.x, endPoint.y);
CGContextSetStrokeColorWithColor(ctx,[UIColor redColor].CGColor);
CGContextSetLineWidth(ctx, 0.5 * MKRoadWidthAtZoomScale(zoomScale));
CGContextStrokePath(ctx);
}
}
以下是我的MKMapViewDelegate的(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
方法的代码:
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
{
if([overlay isKindOfClass:[CustomPathOverlay class]]){
CustomPathRenderer *renderer = [[CustomPathRenderer alloc]initWithOverlay:overlay andMapView:mapView];
return renderer;
}
return nil;
}
我真的很感激为什么这可能不会在MKMapView上呈现的一些线索。
非常感谢