在Mapbox iOS SDK 3.3.1中继承MGLPolygon

时间:2016-08-01 08:36:21

标签: ios objective-c swift mapbox-gl

我试图定义一个继承MGLPolygon的类Building。 MGLPolygon定义为:

public class MGLPolygon : MGLMultiPoint, MGLOverlay {

    public var interiorPolygons: [MGLPolygon]? { get }

    public convenience init(coordinates coords: UnsafeMutablePointer<CLLocationCoordinate2D>, count: UInt)

    public convenience init(coordinates coords: UnsafeMutablePointer<CLLocationCoordinate2D>, count: UInt, interiorPolygons: [MGLPolygon]?)
}

MGLPolygon的指定初始化程序隐藏在快速版本的SDK中。以下将失败:

class Building: MGLPolygon {

    let name: String

    init(name: String, coordinates: [CLLocationCoordinate2D]){
        self.name = name
        super.init(coordinates: &coordinates, count: UInt(coordinates.count))
        // Must call a designated initializer of the superclass 'MGLPolygon'
    }
}

我检查了Objective-C中的original SDK code

@implementation MGLPolygon

@dynamic overlayBounds;

+ (instancetype)polygonWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUInteger)count {
    return [self polygonWithCoordinates:coords count:count interiorPolygons:nil];
}

+ (instancetype)polygonWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUInteger)count interiorPolygons:(NSArray<MGLPolygon *> *)interiorPolygons {
    return [[self alloc] initWithCoordinates:coords count:count interiorPolygons:interiorPolygons];
}

- (instancetype)initWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUInteger)count interiorPolygons:(NSArray<MGLPolygon *> *)interiorPolygons {
    if (self = [super initWithCoordinates:coords count:count]) {
        if (interiorPolygons.count) {
            _interiorPolygons = interiorPolygons;
        }
    }
    return self;
}

- (mbgl::LinearRing<double>)ring {
    NSUInteger count = self.pointCount;
    CLLocationCoordinate2D *coordinates = self.coordinates;

    mbgl::LinearRing<double> result;
    result.reserve(self.pointCount);
    for (NSUInteger i = 0; i < count; i++) {
        result.push_back(mbgl::Point<double>(coordinates[i].longitude, coordinates[i].latitude));
    }
    return result;
}

- (mbgl::Annotation)annotationObjectWithDelegate:(id <MGLMultiPointDelegate>)delegate {
    mbgl::Polygon<double> geometry;
    geometry.push_back(self.ring);
    for (MGLPolygon *polygon in self.interiorPolygons) {
        geometry.push_back(polygon.ring);
    }

    mbgl::FillAnnotation annotation { geometry };
    annotation.opacity = [delegate alphaForShapeAnnotation:self];
    annotation.outlineColor = [delegate strokeColorForShapeAnnotation:self];
    annotation.color = [delegate fillColorForPolygonAnnotation:self];

    return annotation;
}

@end

然而,不幸的是,我对Objective-C并不擅长,而且我不了解代码。

我在问什么?

Swift中MGLPolygon的指定初始化程序是什么?作为参数需要什么?

额外的问题

为什么隐藏指定的初始值设定项?

2 个答案:

答案 0 :(得分:0)

我认为你需要在子类中创建一个类方法(例如+buildingWithCoordinates:count:),它调用super的实现来处理这个问题。

答案 1 :(得分:0)

我的解决方案根据@ incanus的建议:

class Building: MGLPolygon {

    var name: String?

    afterInit(name: String){
        self.name = name
    }
}

func initBuilding(name: String, coordinates: [CLLocationCoordinate2D]) -> Building {
    let building = Building(coordinates: &coordinates, count: UInt(coordinates.count))
    building.afterInit(name)
    return building
}     

它不优雅,但它有效。