summaryrefslogtreecommitdiff
path: root/platform/darwin/src/NSArray+MGLAdditions.mm
blob: 3cab7ff4277b32427f18114fec49191dd0ca78dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#import "NSArray+MGLAdditions.h"

#import "NSDictionary+MGLAdditions.h"
#import "NSExpression+MGLAdditions.mm"

@implementation NSArray (MGLAdditions)

- (std::vector<mbgl::Value>)mgl_vector {
    std::vector<mbgl::Value> vector;
    vector.reserve(self.count);
    for (id value in self) {
        if ([value isKindOfClass:[NSArray class]]) {
            std::vector<mbgl::Value> innerVector = [value mgl_vector];
            vector.push_back(innerVector);
        } else if ([value isKindOfClass:[NSDictionary class]]) {
            mbgl::PropertyMap propertyMap = [value mgl_propertyMap];
            vector.push_back(propertyMap);
        } else {
            NSExpression *expression = [NSExpression expressionForConstantValue:value];
            vector.push_back(expression.mgl_constantMBGLValue);
        }
    }
    return vector;
}

- (NSAttributedString *)mgl_attributedComponentsJoinedByString:(NSString *)separator {
    NSAttributedString *attributedSeparator = [[NSAttributedString alloc] initWithString:separator];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
    BOOL isSubsequentItem = NO;
    for (NSAttributedString *component in self) {
        NSAssert([component isKindOfClass:[NSAttributedString class]], @"Items in array must be attributed strings.");
        if (isSubsequentItem) {
            [attributedString appendAttributedString:attributedSeparator];
        }
        isSubsequentItem = YES;
        [attributedString appendAttributedString:component];
    }
    return attributedString;
}

+ (NSArray *)mgl_coordinatesFromCoordinates:(std::vector<CLLocationCoordinate2D>)coords {
    NSMutableArray *coordinates = [NSMutableArray array];
    for (auto coord : coords) {
        [coordinates addObject:@{@"latitude": @(coord.latitude),
                                 @"longitude": @(coord.longitude)}];
    }
    return coordinates;
}

- (std::vector<CLLocationCoordinate2D>)mgl_coordinates {
    NSUInteger numberOfCoordinates = [self count];
    CLLocationCoordinate2D *coords = (CLLocationCoordinate2D *)malloc(numberOfCoordinates * sizeof(CLLocationCoordinate2D));

    for (NSUInteger i = 0; i < numberOfCoordinates; i++) {
        coords[i] = CLLocationCoordinate2DMake([self[i][@"latitude"] doubleValue],
                                               [self[i][@"longitude"] doubleValue]);
    }

    std::vector<CLLocationCoordinate2D> coordinates = { coords, coords + numberOfCoordinates };
    free(coords);

    return coordinates;
}

@end