是否有可能从MKPolyline继承

tat*_*ato 9 mapkit ios

我正在为iPhone构建一个基于MapKit的应用程序.

我在地图上添加了许多MKPolylines.

但是,我想让我自己的Model类符合添加到地图中的MKOverlay协议,而不是拥有MKPolyline,这样我就可以在mapView:viewForOverlay中创建相应的视图时访问模型属性.

问题是我无法找到从MKPolyline继承的方法,因为它没有我可以从子类'init调用的任何init方法.您只能使用便捷方法创建它们.

如何将模型属性和MKPolyline行为结合在一起?

goe*_*ric 7

MANIAK_dobrii的代码是要走的路,但我发现我必须实现一些额外的MKMultiPoint方法才能使它工作,这里是我使用的AnchorLine类的完整头文件和实现文件: -

标题AnchorLine.h

#import <MapKit/MapKit.h>

@interface AnchorLine : NSObject <MKOverlay> {
    MKPolyline* polyline;
}

@property (nonatomic, retain) MKPolyline* polyline;

+ (AnchorLine*)initWithPolyline: (MKPolyline*) line;
@end
Run Code Online (Sandbox Code Playgroud)

实施AnchorLine.m

#import "AnchorLine.h"

@implementation AnchorLine

@synthesize polyline;


+ (AnchorLine*)initWithPolyline: (MKPolyline*) line {
    AnchorLine* anchorLine = [[AnchorLine alloc] init];
    anchorLine.polyline = line;
    return [anchorLine autorelease];
}

- (void) dealloc {
    [polyline release];
    polyline = nil;
    [super dealloc];
}

#pragma mark MKOverlay
//@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (CLLocationCoordinate2D) coordinate {
    return [polyline coordinate];
}

//@property (nonatomic, readonly) MKMapRect boundingMapRect;
- (MKMapRect) boundingMapRect {
    return [polyline boundingMapRect];
}

- (BOOL)intersectsMapRect:(MKMapRect)mapRect {
    return [polyline intersectsMapRect:mapRect];
}

- (MKMapPoint *) points {
    return [polyline points];
}


-(NSUInteger) pointCount {
    return [polyline pointCount];
}

- (void)getCoordinates:(CLLocationCoordinate2D *)coords range:(NSRange)range {
    return [polyline getCoordinates:coords range:range];
}

@end
Run Code Online (Sandbox Code Playgroud)

希望能帮助别人.


Way*_*man 4

您可以设置该类的关联对象属性。这允许您将实例变量绑定到现有的类。确保自己妥善清理干净。