Skip to content

Feature/multi tree #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,36 @@
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

#import "TSDemoClusteredAnnotationView.h"
#import "CDMapViewController.h"

@implementation TSDemoClusteredAnnotationView

- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
- (id)initWithAnnotation:(ADClusterAnnotation *)annotation reuseIdentifier:(NSString *)reuseIdentifier {

self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code

self.image = [UIImage imageNamed:@"ClusterAnnotation"];
self.frame = CGRectMake(0, 0, self.image.size.width, self.image.size.height);

self.label = [[UILabel alloc] initWithFrame:self.frame];

if ([annotation.cluster.groupID isEqualToString:CDStreetLightJsonFile]) {
self.image = [UIImage imageNamed:@"ClusterAnnotationYellow"];
self.label.textColor = UIColorFromRGB(0xf6d262);
}
else if ([annotation.cluster.groupID isEqualToString:CDToiletJsonFile]) {
self.image = [UIImage imageNamed:@"ClusterAnnotationGreen"];
self.label.textColor = UIColorFromRGB(0x6fc99d);
}
else {
NSLog(@"Error Grouping: %@, %@, %@", annotation, annotation.cluster, annotation.cluster.groupID);
self.image = [UIImage imageNamed:@"ClusterAnnotation"];
self.label.textColor = UIColorFromRGB(0x009fd6);
}

self.frame = CGRectMake(0, 0, self.image.size.width, self.image.size.height);
self.label.frame = self.frame;
self.label.textAlignment = NSTextAlignmentCenter;
self.label.font = [UIFont systemFontOfSize:10];
self.label.textColor = UIColorFromRGB(0x009fd6);
self.label.center = CGPointMake(self.image.size.width/2, self.image.size.height*.43);
self.centerOffset = CGPointMake(0, -self.frame.size.height/2);

Expand All @@ -46,6 +60,20 @@ - (void)clusteringAnimation {

NSUInteger count = clusterAnnotation.clusterCount;
self.label.text = [self numberLabelText:count];

if ([clusterAnnotation.cluster.groupID isEqualToString:CDStreetLightJsonFile]) {
self.image = [UIImage imageNamed:@"ClusterAnnotationYellow"];
self.label.textColor = UIColorFromRGB(0xf6d262);
}
else if ([clusterAnnotation.cluster.groupID isEqualToString:CDToiletJsonFile]) {
self.image = [UIImage imageNamed:@"ClusterAnnotationGreen"];
self.label.textColor = UIColorFromRGB(0x6fc99d);
}
else {
NSLog(@"Error Grouping: %@, %@, %@", clusterAnnotation, clusterAnnotation.cluster, clusterAnnotation.cluster.groupID);
self.image = [UIImage imageNamed:@"ClusterAnnotation"];
self.label.textColor = UIColorFromRGB(0x009fd6);
}
}

- (NSString *)numberLabelText:(float)count {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
#import <UIKit/UIKit.h>
#import "TSClusterMapView.h"

static NSString * const CDStreetLightJsonFile = @"CDStreetlights";
static NSString * const kStreetLightAnnotationImage = @"StreetLightAnnotationYellow";

static NSString * const CDToiletJsonFile = @"CDToilets";
static NSString * const kBathroomAnnotationImage = @"BathroomAnnotationGreen";

@interface CDMapViewController : UIViewController <TSClusterMapViewDelegate, UITabBarDelegate>

@property (strong, nonatomic) IBOutlet TSClusterMapView * mapView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@
#import "TSStreetLightAnnotation.h"
#import "TSDemoClusteredAnnotationView.h"

static NSString * const CDStreetLightJsonFile = @"CDStreetlights";
static NSString * const kStreetLightAnnotationImage = @"StreetLightAnnotation";

static NSString * const CDToiletJsonFile = @"CDToilets";
static NSString * const kBathroomAnnotationImage = @"BathroomAnnotation";

@interface CDMapViewController ()

Expand Down Expand Up @@ -82,7 +78,7 @@ - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnot

#pragma mark - ADClusterMapView Delegate

- (MKAnnotationView *)mapView:(TSClusterMapView *)mapView viewForClusterAnnotation:(id<MKAnnotation>)annotation {
- (MKAnnotationView *)mapView:(TSClusterMapView *)mapView viewForClusterAnnotation:(ADClusterAnnotation *)annotation {

TSDemoClusteredAnnotationView * view = (TSDemoClusteredAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:NSStringFromClass([TSDemoClusteredAnnotationView class])];
if (!view) {
Expand Down Expand Up @@ -156,6 +152,11 @@ - (BOOL)mapView:(TSClusterMapView *)mapView shouldRepositionAnnotations:(NSArray

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {

[self resetsStepperValues];
}

- (void)resetsStepperValues {

if (_tabBar.selectedItem == _bathroomTabBarItem) {
_stepper.value = _bathroomAnnotationsAdded.count;
_stepper.minimumValue = 0;
Expand All @@ -176,31 +177,31 @@ - (IBAction)addAll:(id)sender {
if (_tabBar.selectedItem == _bathroomTabBarItem) {
NSLog(@"Adding All %@", CDToiletJsonFile);

[_mapView addClusteredAnnotations:_bathroomAnnotations];
[_mapView addClusteredAnnotations:_bathroomAnnotations toGroup:CDToiletJsonFile];
_bathroomAnnotationsAdded = [NSMutableArray arrayWithArray:_bathroomAnnotations];
_stepper.value = _bathroomAnnotationsAdded.count;
}
else if (_tabBar.selectedItem == _streetLightsTabBarItem) {
NSLog(@"Adding All %@", CDStreetLightJsonFile);

[_mapView addClusteredAnnotations:_streetLightAnnotations];
[_mapView addClusteredAnnotations:_streetLightAnnotations toGroup:CDStreetLightJsonFile];
_streetLightAnnotationsAdded = [NSMutableArray arrayWithArray:_streetLightAnnotations];
_stepper.value = _streetLightAnnotationsAdded.count;
}

[self refreshBadges];

[self resetsStepperValues];
}

- (IBAction)removeAll:(id)sender {

if (_tabBar.selectedItem == _bathroomTabBarItem) {
[_mapView removeAnnotations:_bathroomAnnotationsAdded];
[_mapView removeAnnotations:_bathroomAnnotationsAdded fromGroup:CDToiletJsonFile];
[_bathroomAnnotationsAdded removeAllObjects];

NSLog(@"Removing All %@", CDToiletJsonFile);
}
else if (_tabBar.selectedItem == _streetLightsTabBarItem) {
[_mapView removeAnnotations:_streetLightAnnotationsAdded];
[_mapView removeAnnotations:_streetLightAnnotationsAdded fromGroup:CDStreetLightJsonFile];
[_streetLightAnnotationsAdded removeAllObjects];

NSLog(@"Removing All %@", CDStreetLightJsonFile);
Expand Down Expand Up @@ -253,7 +254,7 @@ - (void)addNewBathroom {
TSBathroomAnnotation *annotation = [_bathroomAnnotations objectAtIndex:_bathroomAnnotationsAdded.count];
[_bathroomAnnotationsAdded addObject:annotation];

[_mapView addClusteredAnnotation:annotation];
[_mapView addClusteredAnnotation:annotation toGroup:CDToiletJsonFile];
}

- (void)addNewStreetLight {
Expand All @@ -267,7 +268,7 @@ - (void)addNewStreetLight {
TSStreetLightAnnotation *annotation = [_streetLightAnnotations objectAtIndex:_streetLightAnnotationsAdded.count];
[_streetLightAnnotationsAdded addObject:annotation];

[_mapView addClusteredAnnotation:annotation];
[_mapView addClusteredAnnotation:annotation toGroup:CDStreetLightJsonFile];
}

- (void)removeLastBathroom {
Expand All @@ -276,7 +277,7 @@ - (void)removeLastBathroom {

TSBathroomAnnotation *annotation = [_bathroomAnnotationsAdded lastObject];
[_bathroomAnnotationsAdded removeObject:annotation];
[_mapView removeAnnotation:annotation];
[_mapView removeAnnotation:annotation fromGroup:CDToiletJsonFile];
}

- (void)removeLastStreetLight {
Expand All @@ -285,7 +286,7 @@ - (void)removeLastStreetLight {

TSStreetLightAnnotation *annotation = [_streetLightAnnotationsAdded lastObject];
[_streetLightAnnotationsAdded removeObject:annotation];
[_mapView removeAnnotation:annotation];
[_mapView removeAnnotation:annotation fromGroup:CDStreetLightJsonFile];
}

- (IBAction)segmentedControlValueChanged:(id)sender {
Expand Down
4 changes: 2 additions & 2 deletions Pod/Classes/ADClusterAnnotation.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ typedef NS_ENUM(NSUInteger, ADClusterAnnotationType) {
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

@property (readonly, nonatomic) BOOL offscreen;
@property (readonly, nonatomic) BOOL offMap;

/*!
* @discussion Type of annotation, cluster or single.
Expand All @@ -53,7 +53,7 @@ typedef NS_ENUM(NSUInteger, ADClusterAnnotationType) {
/*!
* @discussion This array contains the MKAnnotation objects represented by this annotation
*/
@property (weak, nonatomic, readonly) NSArray <id<MKAnnotation>> * originalAnnotations;
@property (weak, nonatomic, readonly) NSSet <id<MKAnnotation>> * originalAnnotations;

/*!
* @discussion Number of annotations represented by the annotation
Expand Down
22 changes: 11 additions & 11 deletions Pod/Classes/ADClusterAnnotation.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ - (id)init {
self = [super init];
if (self) {
_cluster = nil;
self.coordinate = [self offscreenCoordinate];
self.coordinate = [self offMapCoordinate];
_shouldBeRemovedAfterAnimation = NO;
_title = @"Title";
}
Expand Down Expand Up @@ -52,32 +52,32 @@ - (void)setCoordinate:(CLLocationCoordinate2D)coordinate {

- (void)reset {
self.cluster = nil;
self.coordinate = [self offscreenCoordinate];
self.coordinate = [self offMapCoordinate];
}

- (void)shouldReset {
self.cluster = nil;
self.coordinatePreAnimation = [self offscreenCoordinate];
self.coordinatePreAnimation = [self offMapCoordinate];
}

- (CLLocationCoordinate2D)offscreenCoordinate {
- (CLLocationCoordinate2D)offMapCoordinate {

CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(MAXFLOAT, MAXFLOAT);
CLLocationCoordinate2D coordinate;// = CLLocationCoordinate2DMake(MAXFLOAT, MAXFLOAT);

if (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
// if (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
coordinate = CLLocationCoordinate2DMake(85.0, 179.0);
// this coordinate puts the annotation on the top right corner of the map. We use this instead of kCLLocationCoordinate2DInvalid so that we don't mess with MapKit's KVO weird behaviour that removes from the map the annotations whose coordinate was set to kCLLocationCoordinate2DInvalid.
}
// }

return coordinate;
}

- (BOOL)offscreen {
CLLocationCoordinate2D offscreen = [self offscreenCoordinate];
return (self.coordinate.latitude == offscreen.latitude && self.coordinate.longitude == offscreen.longitude);
- (BOOL)offMap {
CLLocationCoordinate2D offMapCoordinate = [self offMapCoordinate];
return (self.coordinate.latitude == offMapCoordinate.latitude && self.coordinate.longitude == offMapCoordinate.longitude);
}

- (NSArray <id<MKAnnotation>> *)originalAnnotations {
- (NSSet <id<MKAnnotation>> *)originalAnnotations {
NSAssert(self.cluster != nil, @"This annotation should have a cluster assigned!");
return self.cluster.originalAnnotations;
}
Expand Down
22 changes: 18 additions & 4 deletions Pod/Classes/ADMapCluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ typedef void(^KdtreeCompletionBlock)(ADMapCluster *mapCluster);

@property (nonatomic, readonly) NSInteger depth;

@property (readonly) NSMutableArray <id<MKAnnotation>> *originalAnnotations;
@property (readonly) NSSet <id<MKAnnotation>> *originalAnnotations;

@property (readonly) NSMutableArray <ADMapPointAnnotation *> *originalMapPointAnnotations;
@property (readonly, strong) NSSet <ADMapPointAnnotation *> *originalMapPointAnnotations;

@property (readonly) NSString *title;

Expand All @@ -46,24 +46,38 @@ typedef void(^KdtreeCompletionBlock)(ADMapCluster *mapCluster);

@property (readonly) NSSet <ADMapCluster *> *clustersWithAnnotations;

@property (strong, nonatomic) NSString *groupID;

@property (strong, nonatomic) NSArray <ADMapCluster *>*rootClusters;

- (id)initWithRootClusters:(NSArray <ADMapCluster *>*)clusters;

- (ADMapCluster *)rootClusterForID:(NSString *)groupID;

- (instancetype)rebuildWithAnnotations:(NSSet<ADMapPointAnnotation *> *)annotations mapView:(TSClusterMapView *)mapView completion:(KdtreeCompletionBlock)completion;

- (BOOL)overlapsClusterOnMap:(ADMapCluster *)cluster annotationViewMapRectSize:(MKMapRect)annotationViewRect;

/*!
* @discussion Creates a KD-tree of clusters http://en.wikipedia.org/wiki/K-d_tree
* @param annotations Set of ADMapPointAnnotation objects
* @param mapView The ADClusterMapView that will send the delegate callback
* @param groupID The key associated with the group of annotations
* @param completion A new ADMapCluster object.
*/
+ (void)rootClusterForAnnotations:(NSSet <ADMapPointAnnotation *> *)annotations mapView:(TSClusterMapView *)mapView completion:(KdtreeCompletionBlock)completion ;
+ (ADMapCluster *)rootClusterForAnnotations:(NSSet <ADMapPointAnnotation *> *)annotations mapView:(TSClusterMapView *)mapView groupID:(NSString *)groupID completion:(KdtreeCompletionBlock)completion ;


/*!
* @discussion Creates a KD-tree of clusters http://en.wikipedia.org/wiki/K-d_tree
* @param annotations Set of ADMapPointAnnotation objects
* @param groupID The key associated with the group of annotations
* @param gamma Descrimination power
* @param clusterTitle Title of cluster
* @param showSubtitle A Boolean to show subtitle from titles of children
* @param completion A new ADMapCluster object.
*/
+ (void)rootClusterForAnnotations:(NSSet <ADMapPointAnnotation *> *)annotations centerWeight:(double)gamma title:(NSString *)clusterTitle showSubtitle:(BOOL)showSubtitle completion:(KdtreeCompletionBlock)completion ;
+ (ADMapCluster *)rootClusterForAnnotations:(NSSet <ADMapPointAnnotation *> *)annotations groupID:(NSString *)groupID centerWeight:(double)gamma title:(NSString *)clusterTitle showSubtitle:(BOOL)showSubtitle completion:(KdtreeCompletionBlock)completion ;

/*!
* @discussion Adds a single map point annotation to an existing KD-tree map cluster root
Expand Down
Loading