summaryrefslogtreecommitdiff
path: root/platform/darwin/MGLMultiPoint.mm
blob: fd27cf781985f3d857360187bc364ae9d3a3d905 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#import "MGLMultiPoint_Private.h"
#import "MGLGeometry_Private.h"

#import <mbgl/util/geo.hpp>

mbgl::Color MGLColorObjectFromCGColorRef(CGColorRef cgColor) {
    if (!cgColor) {
        return {{ 0, 0, 0, 0 }};
    }
    NSCAssert(CGColorGetNumberOfComponents(cgColor) >= 4, @"Color must have at least 4 components");
    const CGFloat *components = CGColorGetComponents(cgColor);
    return {{ (float)components[0], (float)components[1], (float)components[2], (float)components[3] }};
}

@implementation MGLMultiPoint
{
    CLLocationCoordinate2D *_coords;
    size_t _count;
    mbgl::LatLngBounds _bounds;
}

- (instancetype)initWithCoordinates:(CLLocationCoordinate2D *)coords
                              count:(NSUInteger)count
{
    self = [super init];

    if (self)
    {
        _count = count;
        _coords = (CLLocationCoordinate2D *)malloc(_count * sizeof(CLLocationCoordinate2D));
        _bounds = mbgl::LatLngBounds::getExtendable();

        for (NSUInteger i = 0; i < _count; i++)
        {
            _coords[i] = coords[i];
            _bounds.extend(mbgl::LatLng(coords[i].latitude, coords[i].longitude));
        }
    }

    return self;
}

- (void)dealloc
{
    free(_coords);
}

- (CLLocationCoordinate2D)coordinate
{
    if ([self isMemberOfClass:[MGLMultiPoint class]])
    {
        [[NSException exceptionWithName:@"MGLAbstractClassException"
                                 reason:@"MGLMultiPoint is an abstract class"
                               userInfo:nil] raise];
    }

    assert(_count > 0);

    return CLLocationCoordinate2DMake(_coords[0].latitude, _coords[0].longitude);
}

- (NSUInteger)pointCount
{
    if ([self isMemberOfClass:[MGLMultiPoint class]])
    {
        [[NSException exceptionWithName:@"MGLAbstractClassException"
                                 reason:@"MGLMultiPoint is an abstract class"
                               userInfo:nil] raise];
    }

    return _count;
}

- (void)getCoordinates:(CLLocationCoordinate2D *)coords range:(NSRange)range
{
    if ([self isMemberOfClass:[MGLMultiPoint class]])
    {
        [[NSException exceptionWithName:@"MGLAbstractClassException"
                                 reason:@"MGLMultiPoint is an abstract class"
                               userInfo:nil] raise];
    }

    assert(range.location + range.length <= _count);

    NSUInteger index = 0;

    for (NSUInteger i = range.location; i < range.location + range.length; i++)
    {
        coords[index] = _coords[i];
        index++;
    }
}

- (MGLCoordinateBounds)overlayBounds
{
    return {
        CLLocationCoordinate2DMake(_bounds.sw.latitude,  _bounds.sw.longitude),
        CLLocationCoordinate2DMake(_bounds.ne.latitude, _bounds.ne.longitude)
    };
}

- (BOOL)intersectsOverlayBounds:(MGLCoordinateBounds)overlayBounds
{
    mbgl::LatLngBounds area(
        mbgl::LatLng(overlayBounds.sw.latitude, overlayBounds.sw.longitude),
        mbgl::LatLng(overlayBounds.ne.latitude, overlayBounds.ne.longitude)
    );

    return _bounds.intersects(area);
}

- (void)addShapeAnnotationObjectToCollection:(std::vector<mbgl::ShapeAnnotation> &)shapes withDelegate:(id <MGLMultiPointDelegate>)delegate {
    NSUInteger count = self.pointCount;
    if (count == 0) {
        return;
    }
    
    CLLocationCoordinate2D *coordinates = (CLLocationCoordinate2D *)malloc(count * sizeof(CLLocationCoordinate2D));
    NSAssert(coordinates, @"Unable to allocate annotation with %lu points", (unsigned long)count);
    [self getCoordinates:coordinates range:NSMakeRange(0, count)];
    
    mbgl::AnnotationSegment segment;
    segment.reserve(count);
    for (NSUInteger i = 0; i < count; i++) {
        segment.push_back(MGLLatLngFromLocationCoordinate2D(coordinates[i]));
    }
    free(coordinates);
    shapes.emplace_back(mbgl::AnnotationSegments {{ segment }},
                        [self shapeAnnotationPropertiesObjectWithDelegate:delegate]);
}

- (mbgl::ShapeAnnotation::Properties)shapeAnnotationPropertiesObjectWithDelegate:(__unused id <MGLMultiPointDelegate>)delegate {
    return mbgl::ShapeAnnotation::Properties();
}

@end