我有一个带有两个叠加层的 MKMapView:一个 MKPolyline 和一个 MKOverlay 的子类,它被简单地定义为 CLLocationCoordinate2D 为 0,0 和一个 MKMapRectWorld 边界矩形的叠加层。
我正在尝试最初添加 MKPolyline 叠加层和之后的自定义叠加层,以响应用户操作。自定义叠加层需要位于 MKPolyline 叠加层之下。
我添加了自定义覆盖:
[map insertOverlay:customOverlay atIndex:0];
但是它不起作用。cusomOverlay 被添加到现有的之上。来自 MKMapView 的覆盖数组声称它们的顺序是正确的。
我能够强制执行正确顺序的唯一方法是删除 MKPolyline,添加自定义叠加层,然后将 MKPolyline 放回原处。
我错过了一些明显的东西吗?
编辑代码片段:
自定义覆盖类:
@interface TransparencyOverlay : NSObject <MKOverlay>
{}
@end
@implementation TransparencyOverlay
- (CLLocationCoordinate2D)coordinate
{
return CLLocationCoordinate2DMake(0, 0);
}
- (MKMapRect)boundingMapRect
{
return MKMapRectWorld;
}
@end
添加自定义叠加层:
- (void)onUserAction
{
transparencyOverlay_ = [TransparencyOverlay new];
[context_.map insertOverlay:transparencyOverlay_ atIndex:0];
}
覆盖视图回调:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
// if overlay is MKOverlay return a MKPolylineView
// the construction of that view is kind of complex and can't fit in here
// else:
CLLocationCoordinate2D coords[4] = {CLLocationCoordinate2DMake(89, 180),
CLLocationCoordinate2DMake(89, -180),
CLLocationCoordinate2DMake(-89, -180),
CLLocationCoordinate2DMake(-89, 180)};
MKPolygon *poly = [MKPolygon polygonWithCoordinates:coords count:4];
MKPolygonView *view = [[MKPolygonView alloc] initWithPolygon:poly];
view.fillColor = some color
return [view autorelease];
}
MKPolyline 被添加为 viewDidLoad 上的叠加层(同样,这里列出的代码太多)。