ios - 是否可以从 MKPolyline 继承

标签 ios mapkit

我正在为 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/

相关文章:

iphone - MKPolylineView lineWidth 每次在 MKMapview 上放大或缩小时都需要相同

swift - MapKit addAnnotation 与 addAnnotations

ios - MKMapCamera 俯仰高度函数

ios - 如何从内存中完全删除 UIImageView 动画?

ios - UICollectionView for Springboard 像文件夹

ios - 在 iOS 上学习编码——swift 与 Objective C 的困境

ios - MK坐标跨度制作: why specify longitude AND latitude delta?

ios - 如何在 Swift 中动态生成注释图像

ios - 使用 SLComposeViewController 在 Facebook 中分享图片

ios - Storyboard和自动布局 : how to set UIView to use same aspect ratio as device?