Skip to content

Commit 016471e

Browse files
committed
fix(ios): cluster layer in pair with android
1 parent 5730170 commit 016471e

File tree

10 files changed

+194
-9
lines changed

10 files changed

+194
-9
lines changed

plugin/platforms/android/java/com/akylas/carto/additions/AKClusterElementBuilder.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.content.Context;
55
import android.graphics.BitmapFactory;
66
import android.graphics.Paint;
7+
import android.graphics.Color;
78
import android.util.Log;
89

910
// import com.carto.layers.VectorLayer;
@@ -41,7 +42,9 @@ public void setInterface(Interface inter) {
4142
private Map<Integer, Style> markerStyles = new HashMap<>();
4243
private android.graphics.Bitmap markerBitmap = null;
4344
private com.carto.graphics.Color markerColor = null;
45+
private com.carto.graphics.Color textColor = null;
4446
private int markerSize = 20;
47+
private float textSize = 12;
4548

4649
private String shape = "marker";
4750
private final String TAG = "AKClusterElementBuilder";
@@ -62,10 +65,16 @@ public void setSize(int value) {
6265
markerSize = value;
6366
}
6467
public void setShape(String value) {
65-
Log.d(TAG, "setShape " + value);
6668
shape = value;
6769
}
6870

71+
public void setTextSize(float value) {
72+
textSize = value;
73+
}
74+
public void setTextColor(Color value) {
75+
textColor = value;
76+
}
77+
6978
@Override
7079
public VectorElement buildClusterElement(final MapPos pos, final VectorElementVector elements) {
7180
if (useNativeBuilder) {
@@ -159,11 +168,15 @@ public VectorElement nativeBuildClusterElement(MapPos pos, VectorElementVector e
159168
android.graphics.Paint paint = new android.graphics.Paint(android.graphics.Paint.ANTI_ALIAS_FLAG);
160169

161170
paint.setTextAlign(Paint.Align.CENTER);
162-
paint.setTextSize(12);
163-
paint.setColor(android.graphics.Color.argb(255, 0, 0, 0));
171+
paint.setTextSize(textSize);
172+
if (textColor != null) {
173+
paint.setColor(textColor.getARGB());
174+
} else {
175+
paint.setColor(Color.WHITE);
176+
}
164177

165178
float x = markerBitmap.getWidth() / 2;
166-
float y = markerBitmap.getHeight() / 2 - 5;
179+
float y = markerBitmap.getHeight() / 2 - textSize/2;
167180

168181
canvas.drawText(Integer.toString((int) elements.size()), x, y, paint);
169182
cBitmap = BitmapUtils.createBitmapFromAndroidBitmap(canvasBitmap);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <CartoMobileSDK/CartoMobileSDK.h>
2+
@interface AkClusterElementBuilder : NTClusterElementBuilder
3+
4+
- (void) setBitmap: (UIImage *)value;
5+
- (void) setColor: (UIColor *)value;
6+
- (void) setTextColor: (UIColor *)value;
7+
- (void) setSize: (NSUInteger)value;
8+
- (void) setTextSize: (NSUInteger)value;
9+
- (void) setShape: (NSString *)value;
10+
11+
12+
@end
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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

plugin/platforms/ios/src/CartoAdditions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
#import <UIKit/UIKit.h>
44
#import <Foundation/Foundation.h>
55

6+
#import "AkMarkerClusterElementBuilder.h"
67
#import "Utils.h"

plugin/platforms/ios/src/Utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#pragma mark Public
77

8+
+(NTColor*) toNTColor: (UIColor*)color;
9+
810
+ (long)isLocationOn:(NTMapPos *)point
911
poly:(NTMapPosVector *)poly;
1012

plugin/platforms/ios/src/Utils.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ @interface CartoAdditionsUtils ()
1010

1111
@implementation CartoAdditionsUtils
1212

13+
+(NTColor*) toNTColor: (UIColor*)color {
14+
CGFloat fRed = 0;
15+
CGFloat fGreen = 0;
16+
CGFloat fBlue = 0;
17+
CGFloat fAlpha = 0;
18+
19+
[color getRed:&fRed green:&fGreen blue:&fBlue alpha:&fAlpha];
20+
21+
return [[NTColor alloc] initWithR:(int)(fRed * 255.0) g:(int)(fRed * 255.0) b:(int)(fRed * 255.0) a:(int)(fRed * 255.0)];
22+
}
23+
1324
+ (long)isLocationOn:(NTMapPos *)point
1425
poly:(NTMapPosVector *)poly {
1526
return [CartoAdditionsUtils isLocationOn:point poly:poly closed:false];

plugin/typings/ak.carto.ios.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ declare class CartoAdditionsUtils extends NSObject {
2121

2222
static new(): CartoAdditionsUtils; // inherited from NSObject
2323
}
24+
25+
declare class AkMarkerClusterElementBuilder extends NTClusterElementBuilder {}

src/layers/cluster.android.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export class ClusterElementBuilder extends BaseNative<com.akylas.carto.additions
1010
@nativeColorProperty color: string | Color;
1111
@nativeProperty size: number;
1212
@nativeProperty shape: string;
13+
@nativeProperty textSize: number;
14+
@nativeColorProperty textColor: string | Color;
1315
buildClusterElement?: (position: MapPos, elements: VectorElementVector) => BaseVectorElement<any, any> | com.carto.vectorelements.VectorElement;
1416
createNative(options: ClusterElementBuilderOptions) {
1517
// initClusterElementBuilderNative();

src/layers/cluster.ios.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@ import { BaseNative } from '../index.common';
22
import { MapPos, fromNativeMapPos } from '../core';
33
import { BaseVectorElement, VectorElementVector } from '../vectorelements';
44
import { ClusterElementBuilderOptions } from './cluster';
5+
import { nativeColorProperty, nativeImageProperty, nativeProperty } from '..';
6+
import { Color } from '@nativescript/core';
57

68
@NativeClass
7-
export class ClusterElementBuilderImpl extends NTClusterElementBuilder {
9+
export class ClusterElementBuilderImpl extends AkMarkerClusterElementBuilder {
810
private _owner: WeakRef<ClusterElementBuilder>;
11+
912
public static initWithOwner(owner: WeakRef<ClusterElementBuilder>): ClusterElementBuilderImpl {
1013
const delegate = ClusterElementBuilderImpl.new() as ClusterElementBuilderImpl;
1114
delegate._owner = owner;
1215
return delegate;
1316
}
14-
buildClusterElementSwigExplicitNTClusterElementBuilderElements(position: NTMapPos, nElements: NTVectorElementVector | number) {
17+
buildClusterElementElements(position: NTMapPos, nElements: NTVectorElementVector | number) {
1518
const owner = this._owner.get();
1619
if (owner.buildClusterElement) {
1720
const result = owner.buildClusterElement(fromNativeMapPos(position), new VectorElementVector(undefined, nElements));
@@ -21,13 +24,23 @@ export class ClusterElementBuilderImpl extends NTClusterElementBuilder {
2124
return result;
2225
}
2326
}
24-
return super.buildClusterElementSwigExplicitNTClusterElementBuilderElements(position, nElements as NTVectorElementVector);
27+
return super.buildClusterElementElements(position, nElements as NTVectorElementVector);
2528
}
2629
}
2730

2831
export class ClusterElementBuilder extends BaseNative<NTClusterElementBuilder, ClusterElementBuilderOptions> {
29-
createNative() {
30-
return ClusterElementBuilderImpl.initWithOwner(new WeakRef(this));
32+
@nativeImageProperty bitmap: string;
33+
@nativeColorProperty color: string | Color;
34+
@nativeProperty size: number;
35+
@nativeProperty shape: string;
36+
@nativeProperty textSize: number;
37+
@nativeColorProperty textColor: string | Color;
38+
createNative(options) {
39+
if (!!options.buildClusterElement) {
40+
return ClusterElementBuilderImpl.initWithOwner(new WeakRef(this));
41+
} else {
42+
return AkMarkerClusterElementBuilder.new();
43+
}
3144
}
3245
buildClusterElement?: (position: MapPos, elements: VectorElementVector) => BaseVectorElement<any, any> | NTVectorElement;
3346
}

src/typings/ak.carto.ios.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ declare class CartoAdditionsUtils extends NSObject {
2121

2222
static new(): CartoAdditionsUtils; // inherited from NSObject
2323
}
24+
25+
declare class AkMarkerClusterElementBuilder extends NTClusterElementBuilder {}

0 commit comments

Comments
 (0)