Skip to content
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

fix for crash on large number set as raised here -> https://github.com/DDRBoxman/google-maps-ios-utils/issues/14 #16

Open
wants to merge 2 commits into
base: master
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
4 changes: 4 additions & 0 deletions Clustering/Algorithms/GClusterAlgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
- (void)addItem:(id <GClusterItem>) item;
- (void)removeItems;
- (void)removeItemsNotInRectangle:(CGRect)rect;
- (void)hideItemsNotInBounds: (GMSCoordinateBounds *)bounds;

- (void)removeItem:(id <GClusterItem>) item;
- (void)removeClusterItemsInSet:(NSSet *)set;
- (BOOL)containsItem:(id <GClusterItem>) item;
- (NSSet*)getClusters:(float)zoom;

@end
1 change: 1 addition & 0 deletions Clustering/Algorithms/GQuadItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@interface GQuadItem : NSObject <GCluster, GQTPointQuadTreeItem, NSCopying>

- (id)initWithItem:(id <GClusterItem>)item;
- (id <GClusterItem>)item;

@property(nonatomic, assign) CLLocationCoordinate2D position;

Expand Down
4 changes: 4 additions & 0 deletions Clustering/Algorithms/GQuadItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ @implementation GQuadItem{
CLLocationCoordinate2D _position;
}

- (id)item{
return _item;
}

- (id)initWithItem:(id <GClusterItem>)clusterItem {
if (self = [super init]) {
SphericalMercatorProjection *projection = [[SphericalMercatorProjection alloc] initWithWorldWidth:1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@interface NonHierarchicalDistanceBasedAlgorithm : NSObject<GClusterAlgorithm>

@property (nonatomic, strong) NSMutableArray *items;

- (void)removeItem:(id <GClusterItem>) item;
- (id)initWithMaxDistanceAtZoom:(NSInteger)maxDistanceAtZoom;

@end
38 changes: 38 additions & 0 deletions Clustering/Algorithms/NonHierarchicalDistanceBasedAlgorithm.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,33 @@ - (void)addItem:(id <GClusterItem>) item {
[_quadTree add:quadItem];
}

- (void)removeItem:(id <GClusterItem>) item{
NSSet *set = [NSSet setWithObject:item];
[self removeClusterItemsInSet:set];
}
- (void)removeClusterItemsInSet:(NSSet *)set {
NSMutableArray *toRemove = [NSMutableArray array];
[_items enumerateObjectsUsingBlock:^(GQuadItem *quadItem, NSUInteger idx, BOOL *stop) {
if ([set containsObject:quadItem.item]) {
[toRemove addObject:quadItem];
}
}];
[_items removeObjectsInArray:toRemove];
[toRemove enumerateObjectsUsingBlock:^(GQuadItem *quadItem, NSUInteger idx, BOOL *stop) {
[_quadTree remove:quadItem];
}];
}
-(BOOL)containsItem:(id <GClusterItem>) item{
__block BOOL bFound = NO;
[_items enumerateObjectsUsingBlock:^(GQuadItem *quadItem, NSUInteger idx, BOOL *stop) {
if(quadItem.item == item){
bFound = YES;
*stop = YES;
}
}];
return bFound;
}

- (void)removeItems
{
[_items removeAllObjects];
Expand All @@ -50,6 +77,17 @@ - (void)removeItemsNotInRectangle:(CGRect)rect
_items = newItems;
}

- (void)hideItemsNotInBounds:(GMSCoordinateBounds*)bounds
{
for (GQuadItem *item in _items){
if (![bounds containsCoordinate:item.position ]) {
item.hidden = YES;
}else{
item.hidden = NO;
}
}
}

- (NSSet*)getClusters:(float)zoom {
int discreteZoom = (int) zoom;

Expand Down
3 changes: 3 additions & 0 deletions Clustering/GClusterManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
- (void)addItem:(id <GClusterItem>) item;
- (void)removeItems;
- (void)removeItemsNotInRectangle:(CGRect)rect;
- (void)hideItemsNotInVisibleBounds;

- (void)removeItem:(id <GClusterItem>) item;
- (BOOL)containsItem:(id <GClusterItem>) item;
- (void)cluster;

//convenience
Expand Down
13 changes: 13 additions & 0 deletions Clustering/GClusterManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,26 @@ - (void)addItem:(id <GClusterItem>) item {
[_clusterAlgorithm addItem:item];
}

- (void)removeItem:(id <GClusterItem>) item {
[_clusterAlgorithm removeItem:item];
}

-(BOOL)containsItem:(id<GClusterItem>)item{
return [_clusterAlgorithm containsItem:item];
}

- (void)removeItems {
[_clusterAlgorithm removeItems];
}

- (void)removeItemsNotInRectangle:(CGRect)rect {
[_clusterAlgorithm removeItemsNotInRectangle:rect];
}
- (void)hideItemsNotInVisibleBounds{
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc]initWithRegion:self.mapView.projection.visibleRegion];
[_clusterAlgorithm hideItemsNotInBounds:bounds];
[self cluster];
}

- (void)cluster {
NSSet *clusters = [_clusterAlgorithm getClusters:_mapView.camera.zoom];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
@implementation ClusterMapViewController {
GMSMapView *mapView_;
GClusterManager *clusterManager_;
NSMutableArray *spots;
NSMutableArray *markers;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
Expand All @@ -39,22 +41,41 @@ - (void)viewDidLoad
mapView_.settings.myLocationButton = YES;
mapView_.settings.compassButton = YES;
self.view = mapView_;
spots = [NSMutableArray array];
markers = [NSMutableArray array];

clusterManager_ = [GClusterManager managerWithMapView:mapView_
algorithm:[[NonHierarchicalDistanceBasedAlgorithm alloc] init]
renderer:[[GDefaultClusterRenderer alloc] initWithMapView:mapView_]];

[mapView_ setDelegate:clusterManager_];

for (int i=0; i<12; i++) {
Spot* spot = [self generateSpot];
for (int i=0; i<12000; i++) {
Spot* spot = [self generateSpot]; //auto add to markers
[spots addObject:spot];
[clusterManager_ addItem:spot];
}

[clusterManager_ cluster];
[clusterManager_ setDelegate:self];
}






- (void)mapView:(GMSMapView *)mapView_ didChangeCameraPosition:(GMSCameraPosition *)position {
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(updateMapResults) object:nil];
[self performSelector:@selector(updateMapResults) withObject:nil afterDelay:0.3];
}
//
- (void)updateMapResults {
[clusterManager_ hideItemsNotInVisibleBounds];
}


- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {
[[[UIAlertView alloc] initWithTitle:@"DidTapMarker" message:marker.title delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
return YES;
Expand All @@ -77,6 +98,7 @@ - (Spot*)generateSpot {
Spot* spot = [[Spot alloc] init];
spot.location = marker.position;
spot.marker = marker;
[markers addObject:marker];
return spot;
}

Expand Down