Permalink
Browse files

Initial commit

  • Loading branch information...
1 parent cb6cf52 commit af1e3d89b034804052b88337471e8e030b224638 @romaonthego committed Jul 11, 2012
View
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2011-2012 Roman Efimov (http://github.com/romaonthego).
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
@@ -0,0 +1,68 @@
+# REMarkerClusterer
+###REMarkerClusterer creates and manages per-zoom-level clusters for large amounts of markers.
+
+As seen in [Pinsnap](http://itunes.apple.com/us/app/pinsnap/id457407067?mt=8) iPhone app. `REMarkerClusterer` was inspired by the Apple Photo app on the iPhone, `REMarkerClusterer` mimics it's behaviour providing animations for grouping and ungrouping clusters.
+
+![Screenshot of REMarkerClusterer](https://github.com/romaonthego/REMarkerClusterer/raw/master/Screenshot.png "REMarkerClusterer Screenshot")
+
+## How it works
+The `REMarkerClusterer` will group markers into clusters according to their distance from a cluster's center. When a marker is added, the marker cluster will find a position in all the clusters, and if it fails to find one, it will create a new cluster with the marker. The number of markers in a cluster will be displayed on the cluster marker. When the map viewport changes, `REMarkerClusterer` will destroy the clusters in the viewport and regroup them into new clusters.
+
+## Demo
+
+Build and run the `REMarkerClustererExample` project in Xcode to see `REMarkerClusterer` in action.
+
+## Installation
+
+`REMarkerClusterer` requires the `MapKit` and `CoreLocation` frameworks, so the first thing you'll need to do is include the frameworks into your project.
+
+Now that the framework has been linked, all you need to do is drop files from `REMarkerClusterer` folder into your project, and add `#include "REMarkerClusterer.h"` to the top of classes that will use it.
+
+## Example Usage
+
+``` objective-c
+REMarkerClusterer *clusterer = [[REMarkerClusterer alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
+clusterer.delegate = self;
+[clusterer setLatitude:37.786996 longitude:-97.440100 delta:30.03863];
+
+// Set smaller grid size for an iPad
+clusterer.gridSize = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ? 25 : 20;
+[self.view addSubview:clusterer];
+
+clusterer.oneItemCaption = @"One item";
+clusterer.manyItemsCaption = @"%i items";
+
+NSDictionary *data = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Points" ofType:@"plist"]];
+for (NSDictionary *dict in [data objectForKey:@"Points"]) {
+ REMarker *marker = [[REMarker alloc] init];
+ marker.markerId = [[dict objectForKey:@"id"] intValue];
+ marker.coordinate = CLLocationCoordinate2DMake([[dict objectForKey:@"latitude"] floatValue],
+ [[dict objectForKey:@"longitude"] floatValue]);
+ [clusterer addMarker:marker];
+}
+[clusterer zoomToAnnotationsBounds:clusterer.markers];
+[clusterer clusterize];
+```
+
+## Contact
+
+Roman Efimov
+
+- http://github.com/romaonthego
+- http://twitter.com/romaonthego
+
+## Credits
+
+Partially based on MarkerClusterer Javascript library by Xiaoxi Wu (http://http://gmaps-utility-library-dev.googlecode.com)
+
+## License
+
+REMarkerClusterer is available under the MIT license.
+
+Copyright © 2012 Roman Efimov.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,40 +1,47 @@
//
-// RECluster.h
-// REMarkerClusterer
+// RECluster.h
+// REMarkerClusterer
//
-// Created by Roman Efimov on 3/9/11.
-// Copyright 2011 Roman Efimov. All rights reserved.
+// Copyright (c) 2011-2012 Roman Efimov (http://github.com/romaonthego)
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
//
#import <Foundation/Foundation.h>
+#import <CoreLocation/CoreLocation.h>
#import "REMarkerClusterer.h"
-#import <CoreLocation/CLLocation.h>
#import "RELatLngBounds.h"
-@interface RECluster : NSObject <MKAnnotation> {
- REMarkerClusterer *__weak markerClusterer;
- NSString *title;
- int gridSize;
- CLLocationCoordinate2D coordinate;
- bool averageCenter;
- NSMutableArray *markers;
- bool hasCenter;
- RELatLngBounds *bounds;
-}
+@interface RECluster : NSObject <MKAnnotation>
@property (nonatomic) RELatLngBounds *bounds;
-@property (nonatomic, weak) REMarkerClusterer *markerClusterer;
-@property (nonatomic, readwrite) int gridSize;
-@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
-@property (nonatomic, readwrite) bool averageCenter;
-@property (nonatomic, readwrite) bool hasCenter;
-@property (nonatomic) NSMutableArray *markers;
+@property (nonatomic, assign) REMarkerClusterer *markerClusterer;
+@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;
+@property (nonatomic, readwrite) BOOL averageCenter;
+@property (nonatomic, readwrite) BOOL hasCenter;
@property (nonatomic, copy) NSString *title;
+@property (nonatomic) NSMutableArray *markers;
-- (bool)isMarkerAlreadyAdded:(REMarker *)marker;
-- (bool)addMarker:(REMarker *)marker;
- (id)initWithClusterer:(REMarkerClusterer *)clusterer;
-- (bool)isMarkerInClusterBounds:(REMarker *)marker;
+- (BOOL)isMarkerAlreadyAdded:(REMarker *)marker;
+- (BOOL)addMarker:(REMarker *)marker;
+- (BOOL)isMarkerInClusterBounds:(REMarker *)marker;
- (void)setAverageCenter;
@end
@@ -1,120 +1,94 @@
//
-// RECluster.m
-// REMarkerClusterer
+// RECluster.m
+// REMarkerClusterer
//
-// Created by Roman Efimov on 3/9/11.
-// Copyright 2011 Roman Efimov. All rights reserved.
+// Copyright (c) 2011-2012 Roman Efimov (http://github.com/romaonthego)
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
//
#import "RECluster.h"
-
@implementation RECluster
-@synthesize markerClusterer, gridSize, coordinate, markers, averageCenter, hasCenter, title, bounds;
+@synthesize markerClusterer, coordinate, markers, averageCenter, hasCenter, title, bounds;
-- (id)initWithClusterer:(REMarkerClusterer *)clusterer {
- /*markerClusterer = clusterer;
- gridSize = [clusterer getGridSize];
- averageCenter = [clusterer isAverageCenter];
- markers = [[NSMutableArray alloc] initWithCapacity:0];
- hasCenter = NO;
-
- return [super init]; */
-
- self = [super init];
- if (self) {
- self.markerClusterer = clusterer;
- self.gridSize = [clusterer getGridSize];
- self.averageCenter = [clusterer isAverageCenter];
+- (id)initWithClusterer:(REMarkerClusterer *)clusterer
+{
+ if ((self = [super init])) {
+ markerClusterer = clusterer;
+ averageCenter = [clusterer isAverageCenter];
markers = [[NSMutableArray alloc] initWithCapacity:0];
- self.hasCenter = NO;
+ hasCenter = NO;
bounds = [[RELatLngBounds alloc] initWithMapView:markerClusterer.mapView];
}
return self;
}
-- (void)calculateBounds {
+- (void)calculateBounds
+{
[bounds setSouthWest:coordinate northEast:coordinate];
[bounds setExtendedBounds:markerClusterer.gridSize];
- //RELatLngBounds *_bounds = [RELatLngBounds boundsWithSouthWest:coordinate northEast:coordinate mapView:markerClusterer.mapView];
- //self.bounds = [markerClusterer getExtendedBounds:_bounds];
- //[_bounds release];
}
-- (bool)isMarkerInClusterBounds:(REMarker *)marker {
+- (BOOL)isMarkerInClusterBounds:(REMarker *)marker
+{
return [bounds contains:marker.coordinate];
}
-- (bool)isMarkerAlreadyAdded:(REMarker *)marker {
- for (int i=0; i < [markers count]; i++) {
- REMarker *m = (REMarker *)[markers objectAtIndex:i];
- if (m.ID == marker.ID)
+- (BOOL)isMarkerAlreadyAdded:(REMarker *)marker
+{
+ for (REMarker *m in markers) {
+ if ([m isEqual:marker])
return YES;
}
return NO;
}
-- (void)setAverageCenter {
+- (void)setAverageCenter
+{
double x = 0;
double y = 0;
double z = 0;
for (REMarker *marker in markers) {
- //REMarker *marker = [markers objectAtIndex:[markers count] - 1];
-
- double lat = marker.coordinate.latitude * M_PI/180;
- double lon = marker.coordinate.longitude * M_PI/180;
- //NSLog(@"lat = %f, lng = %f", marker.coordinate.latitude, marker.coordinate.longitude);
-
+ double lat = marker.coordinate.latitude * M_PI / 180;
+ double lon = marker.coordinate.longitude * M_PI / 180;
+
x += cos(lat) * cos(lon);
y += cos(lat) * sin(lon);
z += sin(lat);
-
}
- // NSLog(@"x = %f, y = %f, z = %f", x, y, z);
- double count = [markers count];
- x = x / count;
- y = y / count;
- z = z / count;
+ x = x / [markers count];
+ y = y / [markers count];
+ z = z / [markers count];
- double r = sqrt(x*x + y*y + z*z);
- double lat1 = asin(z/r) / (M_PI/180);
- double lon1 = atan2(y, x) / (M_PI/180);
-
- //NSLog(@"lat1 = %f, lng1 = %f", coordinate.latitude, coordinate.longitude);
- //NSLog(@"lat2 = %f, lng2 = %f", lat1, lon1);
+ double r = sqrt(x * x + y * y + z * z);
+ double lat1 = asin(z / r) / (M_PI / 180);
+ double lon1 = atan2(y, x) / (M_PI / 180);
coordinate = CLLocationCoordinate2DMake(lat1, lon1);
-
- //double avgX =
-
- /*double s1, c1;
- double s2, c2;
- for (REMarker *marker in markers) {
- s1 += sin(marker.coordinate.latitude);
- c1 += cos(marker.coordinate.latitude);
-
- s2 += sin(marker.coordinate.longitude);
- c2 += cos(marker.coordinate.longitude);
- }
-
- s1 = s1 / [markers count];
- c1 = c1 / [markers count];
-
- s2 = s2 / [markers count];
- c2 = c2 / [markers count];
-
- double o1 = atan(c1/s1);
-
- double o2 = atan(c2/s2);
- //avgLng = avgLng / [markers count];
-
- coordinate = CLLocationCoordinate2DMake(o1, o2);*/
}
-- (bool)addMarker:(REMarker *)marker {
+- (BOOL)addMarker:(REMarker *)marker
+{
if ([self isMarkerAlreadyAdded:marker]) {
return NO;
}
@@ -126,8 +100,8 @@ - (bool)addMarker:(REMarker *)marker {
} else {
if (averageCenter && [markers count] >= 10) {
double l = [markers count] + 1;
- double lat = (coordinate.latitude * (l-1) + marker.coordinate.latitude) / l;
- double lng = (coordinate.longitude * (l-1) + marker.coordinate.longitude) / l;
+ double lat = (coordinate.latitude * (l - 1) + marker.coordinate.latitude) / l;
+ double lng = (coordinate.longitude * (l - 1) + marker.coordinate.longitude) / l;
coordinate = CLLocationCoordinate2DMake(lat, lng);
hasCenter = YES;
[self calculateBounds];
@@ -1,28 +1,38 @@
//
-// RELatLngBounds.h
-// REMarkerClusterer
+// RELatLngBounds.h
+// REMarkerClusterer
//
-// Created by Roman Efimov on 3/9/11.
-// Copyright 2011 Roman Efimov. All rights reserved.
+// Copyright (c) 2011-2012 Roman Efimov (http://github.com/romaonthego)
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
//
-#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CLLocation.h>
-@interface RELatLngBounds : NSObject {
- CLLocationCoordinate2D northEast;
- CLLocationCoordinate2D northWest;
- CLLocationCoordinate2D southWest;
- CLLocationCoordinate2D southEast;
- MKMapView *__weak _mapView;
-}
+@interface RELatLngBounds : NSObject
@property (nonatomic, readwrite) CLLocationCoordinate2D northEast;
@property (nonatomic, readwrite) CLLocationCoordinate2D northWest;
@property (nonatomic, readwrite) CLLocationCoordinate2D southWest;
@property (nonatomic, readwrite) CLLocationCoordinate2D southEast;
-@property (nonatomic, weak) MKMapView *_mapView;
+@property (nonatomic, copy) MKMapView *mapView;
- (id)initWithMapView:(MKMapView *)mapView;
- (void)setSouthWest:(CLLocationCoordinate2D)sw northEast:(CLLocationCoordinate2D)ne;
Oops, something went wrong.

0 comments on commit af1e3d8

Please sign in to comment.