1//
2// Created by Leland Richardson on 12/27/15.
3// Copyright (c) 2015 Facebook. All rights reserved.
4//
5
6#import "AIRMapPolygon.h"
7#import <React/UIView+React.h>
8
9
10@implementation AIRMapPolygon {
11
12}
13
14- (void)setFillColor:(UIColor *)fillColor {
15    _fillColor = fillColor;
16    [self update];
17}
18
19- (void)setStrokeColor:(UIColor *)strokeColor {
20    _strokeColor = strokeColor;
21    [self update];
22}
23
24- (void)setStrokeWidth:(CGFloat)strokeWidth {
25    _strokeWidth = strokeWidth;
26    [self update];
27}
28
29- (void)setLineJoin:(CGLineJoin)lineJoin {
30    _lineJoin = lineJoin;
31    [self update];
32}
33
34- (void)setLineCap:(CGLineCap)lineCap {
35    _lineCap = lineCap;
36    [self update];
37}
38
39- (void)setMiterLimit:(CGFloat)miterLimit {
40    _miterLimit = miterLimit;
41    [self update];
42}
43
44- (void)setLineDashPhase:(CGFloat)lineDashPhase {
45    _lineDashPhase = lineDashPhase;
46    [self update];
47}
48
49- (void)setLineDashPattern:(NSArray <NSNumber *> *)lineDashPattern {
50    _lineDashPattern = lineDashPattern;
51    [self update];
52}
53
54- (void)setCoordinates:(NSArray<AIRMapCoordinate *> *)coordinates {
55    _coordinates = coordinates;
56    CLLocationCoordinate2D coords[coordinates.count];
57    for(int i = 0; i < coordinates.count; i++)
58    {
59        coords[i] = coordinates[i].coordinate;
60    }
61    self.polygon = [MKPolygon polygonWithCoordinates:coords count:coordinates.count interiorPolygons:_interiorPolygons];
62    // TODO: we could lazy-initialize the polygon, since we don't need it until the
63    // polygon is in view.
64    self.renderer = [[MKPolygonRenderer alloc] initWithPolygon:self.polygon];
65    [self update];
66}
67
68- (void)setHoles:(NSArray<NSArray<AIRMapCoordinate *> *> *)holes {
69    _holes = holes;
70    if (holes.count)
71    {
72        NSMutableArray<MKPolygon *> *polygons = [NSMutableArray array];
73        for(int h = 0; h < holes.count; h++)
74        {
75            CLLocationCoordinate2D coords[holes[h].count];
76            for(int i = 0; i < holes[h].count; i++)
77            {
78                coords[i] = holes[h][i].coordinate;
79            }
80            [polygons addObject:[MKPolygon polygonWithCoordinates:coords count:holes[h].count]];
81        }
82        _interiorPolygons = polygons;
83    }
84}
85
86- (void) update
87{
88    if (!_renderer) return;
89    _renderer.fillColor = _fillColor;
90    _renderer.strokeColor = _strokeColor;
91    _renderer.lineWidth = _strokeWidth;
92    _renderer.lineCap = _lineCap;
93    _renderer.lineJoin = _lineJoin;
94    _renderer.miterLimit = _miterLimit;
95    _renderer.lineDashPhase = _lineDashPhase;
96    _renderer.lineDashPattern = _lineDashPattern;
97
98    if (_map == nil) return;
99    [_map removeOverlay:self];
100    [_map addOverlay:self];
101}
102
103#pragma mark MKOverlay implementation
104
105- (CLLocationCoordinate2D) coordinate
106{
107    return self.polygon.coordinate;
108}
109
110- (MKMapRect) boundingMapRect
111{
112    return self.polygon.boundingMapRect;
113}
114
115- (BOOL)intersectsMapRect:(MKMapRect)mapRect
116{
117    BOOL answer = [self.polygon intersectsMapRect:mapRect];
118    return answer;
119}
120
121- (BOOL)canReplaceMapContent
122{
123    return NO;
124}
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178@end
179