1//
2//  AIRGoogleMapPolyline.m
3//
4//  Created by Nick Italiano on 10/22/16.
5//
6
7#ifdef HAVE_GOOGLE_MAPS
8#import <UIKit/UIKit.h>
9#import "AIRGoogleMapPolyline.h"
10#import "AIRGMSPolyline.h"
11#import "AIRMapCoordinate.h"
12#import "AIRGoogleMapMarker.h"
13#import "AIRGoogleMapMarkerManager.h"
14#import <GoogleMaps/GoogleMaps.h>
15#import <React/RCTUtils.h>
16
17@implementation AIRGoogleMapPolyline
18
19- (instancetype)init
20{
21  if (self = [super init]) {
22    _polyline = [[AIRGMSPolyline alloc] init];
23  }
24  return self;
25}
26
27-(void)setCoordinates:(NSArray<AIRMapCoordinate *> *)coordinates
28{
29  _coordinates = coordinates;
30
31  GMSMutablePath *path = [GMSMutablePath path];
32  for(int i = 0; i < coordinates.count; i++)
33  {
34    [path addCoordinate:coordinates[i].coordinate];
35  }
36
37  _polyline.path = path;
38
39  [self configureStyleSpansIfNeeded];
40}
41
42-(void)setStrokeColor:(UIColor *)strokeColor
43{
44  _strokeColor = strokeColor;
45  _polyline.strokeColor = strokeColor;
46  [self configureStyleSpansIfNeeded];
47}
48
49-(void)setStrokeColors:(NSArray<UIColor *> *)strokeColors
50{
51  NSMutableArray *spans = [NSMutableArray arrayWithCapacity:[strokeColors count]];
52  for (int i = 0; i < [strokeColors count]; i++)
53  {
54    GMSStrokeStyle *stroke;
55
56     if (i == 0) {
57      stroke = [GMSStrokeStyle solidColor:strokeColors[i]];
58    } else {
59      stroke = [GMSStrokeStyle gradientFromColor:strokeColors[i-1] toColor:strokeColors[i]];
60    }
61
62     [spans addObject:[GMSStyleSpan spanWithStyle:stroke]];
63  }
64
65  _strokeColors = strokeColors;
66  _polyline.spans = spans;
67}
68
69-(void)setStrokeWidth:(double)strokeWidth
70{
71  _strokeWidth = strokeWidth;
72  _polyline.strokeWidth = strokeWidth;
73}
74
75-(void)setFillColor:(UIColor *)fillColor
76{
77  _fillColor = fillColor;
78  _polyline.spans = @[[GMSStyleSpan spanWithColor:fillColor]];
79}
80
81- (void)setLineDashPattern:(NSArray<NSNumber *> *)lineDashPattern {
82  _lineDashPattern = lineDashPattern;
83  [self configureStyleSpansIfNeeded];
84}
85
86-(void)setGeodesic:(BOOL)geodesic
87{
88  _geodesic = geodesic;
89  _polyline.geodesic = geodesic;
90}
91
92-(void)setTitle:(NSString *)title
93{
94  _title = title;
95  _polyline.title = _title;
96}
97
98-(void) setZIndex:(int)zIndex
99{
100  _zIndex = zIndex;
101  _polyline.zIndex = zIndex;
102}
103
104-(void)setTappable:(BOOL)tappable
105{
106  _tappable = tappable;
107  _polyline.tappable = tappable;
108}
109
110- (void)setOnPress:(RCTBubblingEventBlock)onPress {
111  _polyline.onPress = onPress;
112}
113
114- (void)configureStyleSpansIfNeeded {
115  if (!_strokeColor || !_lineDashPattern || !_polyline.path) {
116      return;
117  }
118
119  BOOL isLine = YES;
120  NSMutableArray *styles = [[NSMutableArray alloc] init];
121  for (NSInteger i = 0; i < _lineDashPattern.count; i++) {
122    if (isLine) {
123      [styles addObject:[GMSStrokeStyle solidColor:_strokeColor]];
124    } else {
125      [styles addObject:[GMSStrokeStyle solidColor:[UIColor clearColor]]];
126    }
127    isLine = !isLine;
128  }
129
130  _polyline.spans = GMSStyleSpans(_polyline.path, styles, _lineDashPattern, kGMSLengthRhumb);
131}
132
133@end
134
135#endif
136