我正在尝试定义一个继承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的指定初始化程序是什么?它作为参数是什么?

额外的问题

为什么隐藏了指定的初始化程序?

最佳答案

我认为您需要在子类(例如+buildingWithCoordinates:count:)中创建一个类方法,该方法将调用super的实现来进行处理。

关于ios - 在Mapbox iOS SDK 3.3.1中继承MGLPolygon,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38694334/

10-10 15:01