我正在为 iPhone 构建基于 MapKit 的应用程序。
我在 map 中添加了许多 MKPolylines。
但是,我不想使用 MKPolyline,而是希望将符合 MKOverlay 协议(protocol)的自己的模型类添加到 map 中,以便在 mapView:viewForOverlay 中创建相应 View 时可以访问模型属性。
问题是我找不到从 MKPolyline 继承的方法,因为它没有任何我可以从子类的 init 调用的 init 方法。您只能使用便捷方法创建它们。
如何将模型属性和 MKPolyline 行为结合在一起?
最佳答案
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
实现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
希望对某人有所帮助。
关于ios - 是否可以从 MKPolyline 继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4949069/