|
| 1 | +#include "AkClusterElementBuilder.h" |
| 2 | +#include "Utils.h" |
| 3 | + |
| 4 | +@interface AkClusterElementBuilder () |
| 5 | + |
| 6 | +@property NSMutableDictionary* markerStyles; |
| 7 | +@property (nonatomic) UIImage *markerImage; |
| 8 | +@property (nonatomic) UIColor *markerColor; |
| 9 | +@property (nonatomic) UIColor *textColor; |
| 10 | +@property (nonatomic) NSUInteger markerSize; |
| 11 | +@property (nonatomic) NSUInteger textSize; |
| 12 | +@property (nonatomic) NSString* shape; |
| 13 | + |
| 14 | +@end |
| 15 | +@implementation AkClusterElementBuilder : NTClusterElementBuilder |
| 16 | + |
| 17 | +-(id)init { |
| 18 | + if (self = [super init]) { |
| 19 | + self.markerSize = 20; |
| 20 | + self.textSize = 12; |
| 21 | + } |
| 22 | + return self; |
| 23 | +} |
| 24 | + |
| 25 | +- (void) setBitmap: (UIImage *)value { |
| 26 | + self.markerImage = value; |
| 27 | +} |
| 28 | +- (void) setColor: (UIColor *)value{ |
| 29 | + self.markerColor = value; |
| 30 | + } |
| 31 | +- (void) setTextColor: (UIColor *)value{ |
| 32 | + self.textColor = value; |
| 33 | +} |
| 34 | +- (void) setSize: (NSUInteger)value{ |
| 35 | + self.markerSize = value; |
| 36 | +} |
| 37 | +- (void) setTextSize: (NSUInteger)value{ |
| 38 | + self.textSize = value; |
| 39 | +} |
| 40 | +- (void) setShape: (NSString *)value { |
| 41 | + self.shape = value; |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +-(NTVectorElement*)buildClusterElement:(NTMapPos *)mapPos elements:(NTVectorElementVector *)elements |
| 47 | +{ |
| 48 | + if (!self.markerStyles) { |
| 49 | + self.markerStyles = [NSMutableDictionary new]; |
| 50 | + } |
| 51 | + |
| 52 | + NSString* styleKey = [NSString stringWithFormat:@"%d",(int)[elements size]]; |
| 53 | + |
| 54 | + NTStyle* markerStyle = [self.markerStyles valueForKey:styleKey]; |
| 55 | + |
| 56 | + if ([elements size] == 1) { |
| 57 | + markerStyle = [(NTMarker*)[elements get:0] getStyle]; |
| 58 | + } |
| 59 | + |
| 60 | + if (!markerStyle) { |
| 61 | + |
| 62 | + if (!self.markerImage) { |
| 63 | + self.markerImage = [UIImage imageNamed:@"marker_black.png"]; |
| 64 | + } |
| 65 | + |
| 66 | + UIGraphicsBeginImageContext(self.markerImage.size); |
| 67 | + [self.markerImage drawAtPoint:CGPointMake(0, 0)]; |
| 68 | + |
| 69 | + CGRect rect = CGRectMake(0, 15, self.markerImage.size.width, self.markerImage.size.height); |
| 70 | + if(self.textColor) { |
| 71 | + [self.textColor set]; |
| 72 | + } else { |
| 73 | + [[UIColor whiteColor] set]; |
| 74 | + } |
| 75 | + |
| 76 | + NSMutableParagraphStyle* style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; |
| 77 | + [style setAlignment:NSTextAlignmentCenter]; |
| 78 | + |
| 79 | + NSDictionary* attr = [NSDictionary dictionaryWithObject:style forKey:NSParagraphStyleAttributeName]; |
| 80 | + [styleKey drawInRect:CGRectIntegral(rect) withAttributes:attr]; |
| 81 | + |
| 82 | + UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); |
| 83 | + UIGraphicsEndImageContext(); |
| 84 | + |
| 85 | + NTBitmap* markerBitmap = [NTBitmapUtils createBitmapFromUIImage:newImage]; |
| 86 | + |
| 87 | + |
| 88 | + if ([self.shape isEqualToString:@"point"]) { |
| 89 | + NTPointStyleBuilder* styleBuilder = [[NTPointStyleBuilder alloc] init]; |
| 90 | + |
| 91 | + [styleBuilder setBitmap:markerBitmap]; |
| 92 | + [styleBuilder setSize:self.markerSize]; |
| 93 | + |
| 94 | + if (self.markerColor != nil) { |
| 95 | + [styleBuilder setColor:[CartoAdditionsUtils toNTColor:self.markerColor]]; |
| 96 | + } |
| 97 | + markerStyle = [styleBuilder buildStyle]; |
| 98 | + } else { |
| 99 | + NTMarkerStyleBuilder* styleBuilder = [[NTMarkerStyleBuilder alloc] init]; |
| 100 | + |
| 101 | + [styleBuilder setBitmap:markerBitmap]; |
| 102 | + [styleBuilder setSize:self.markerSize]; |
| 103 | + [styleBuilder setHideIfOverlapped:NO]; |
| 104 | + [styleBuilder setPlacementPriority:(int)[elements size]]; |
| 105 | + |
| 106 | + if (self.markerColor != nil) { |
| 107 | + [styleBuilder setColor:[CartoAdditionsUtils toNTColor:self.markerColor]]; |
| 108 | + } |
| 109 | + markerStyle = [styleBuilder buildStyle]; |
| 110 | + } |
| 111 | + [self.markerStyles setValue:markerStyle forKey:styleKey]; |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + if( [markerStyle isKindOfClass:[NTPointStyle class]]) { |
| 116 | + NTPoint* marker = [[NTPoint alloc] initWithPos:mapPos style:(NTPointStyle*)markerStyle]; |
| 117 | + [marker setMetaDataElement:@"elements" element:[[NTVariant alloc] initWithLongVal:[elements size]]]; |
| 118 | + return marker; |
| 119 | + } |
| 120 | + if( [markerStyle isKindOfClass:[NTMarkerStyle class]]) { |
| 121 | + NTMarker* marker = [[NTMarker alloc] initWithPos:mapPos style:(NTMarkerStyle*)markerStyle]; |
| 122 | + [marker setMetaDataElement:@"elements" element:[[NTVariant alloc] initWithLongVal:[elements size]]]; |
| 123 | + return marker; |
| 124 | + } |
| 125 | + return nil; |
| 126 | +} |
| 127 | +@end |
0 commit comments