我正在使用 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);
}
}
这是(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
我的 MKMapViewDelegate 方法中的代码:
- (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 上呈现的线索。
非常感谢