Code Monkey home page Code Monkey logo

ocmapview's Introduction

What is OCMapView?

OpenClusterMapView is a simple and easy to use extension of MKMapView for iOS. If you are displaying a lot of annotations on the map, this class is made for you.

OCMapView automatically creates clusters by combining annotations super fast. It works with any iOS application. No code changes needed.

Screenshots:

Screenshots

Features

  • Fast and easy! Handle over 9000 annotations on the map at once.
  • Very simple usage! No code changes needed, just replace the MapView class
  • Easy to integrate! Keep all MKMapView delegate methods as they are
  • Customize the MKAnnotationViews of generated clusters like any other annotation
  • Customize behavior like the cluster size
  • Two different clustering algorithms
  • Provide an ignore list of annotations, that wont be clustered
  • Create independent clusters by using groups

Setup

Cocoapods

  • Just install the pod using pod 'OCMapView'

Manually

  • Add the OCMapView folder to your project.
  • Link with the MapKit and CoreLocation frameworks

Background:

You may already have encountered the problem: When adding a several hundred annotations to the MKMapView, it will get laggy and everything but user friendly.

Many developers believe that iOS can't handle a huge amount of annotations on a MKMapView due to the low memory capacities of iDevices. So they start to handle the annotation management themselves with pretty dumb filter methods which won't display all annotations and confuse users even more.

The actual problem is not memory related. It rather occurs because annotationViews are UIViews and these are extremely slow. So if you scroll/zoom a MKMapView with many annotations, iOS has to redraw them all at once and that will take time.

OCMapView combines multiple annotations in a specified range and displays them just as a single annotationView for the whole cluster. So you'll never have too many views which make your app slow and laggy. In addition to that forget to handle the annotations yourself! Just add them all to your OCMapView and it will do everything for you. Even the iPhone 3G with iOS 3.1.x can handle a couple thousand annotations without lagging!

Usage

Just replace your MKMapView references with OCMapView. It automatically handles annotations and combines them to clusters for you without any other change of your code.

Add the annotations you want to display as usual and implement the same MKMapViewDelegate methods like before. The handling stays completely the same so you can easily use custom views for your annotations and clusters. The viewForAnnotation delegate method will return OCAnnotation objects for generated clusters. This is how to provide custom views:

- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)annotation{  
    // if it's a cluster  
    if ([annotation isKindOfClass:[OCAnnotation class]]) {  
        // create your custom cluster annotationView here!  
    }  
    // If it's a single annotation  
    else if([annotation isKindOfClass:[Your_Annotation class]]){  
        // create your custom annotationView  as regular here!  
    }  
    return Your_annotationView;  
}

For more informations, have a look at the example project.

Grouping

You can cluster different groups of annotations, as seen on the screenshots above, like this:

Implement the OCGrouping protocol on your annotation class, set its grouping-tag and set the clusterByGroupTag property of your OCMapView to true. Take a look at the sample project to see how it works.

Contribute

Apps that use OCMapView:

  • official Car2Go App
  • cost control for Car2Go aka. aCar2Go
  • AbHof
  • and a lot of more who do not tell me ;)

If you are using OCMapView and want to contribute, please contact me to add your app to the list of supporting apps!

OpenClusterMapView Logo

ocmapview's People

Contributors

calimarkus avatar cniaulin avatar felixlam avatar florentmorin avatar marotan3 avatar mbulli avatar schneider-iosphere avatar thechrisoshow avatar yinkou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ocmapview's Issues

"ERROR: Trying to select an annotation which has not been added"

ERROR: Trying to select an annotation which has not been added

I'm getting this error with OCMapView, but not MKMapView (which currently works perfectly).

I'm getting it on the following line of code:

 [self.mapView selectAnnotation:annotation animated:YES];

annotation is definitely added to self.mapView already, because it is visible there when iterating over self.mapView.annotations (and comparing pointer references - definitely same annotation object).

This is on iOS7.

Any idea what could be wrong?

Podspec not in offical CocoaPods repo

hi @yinkou!
first things first: thank you for your awesome clustering implementation. really really appreciate your efforts!

i just recognized that your podspec is not present via the offical cocoapods spec repository. if you like, i'd publish it for you.

just let me know :)

cheers,
@swissmanu

Async clustering

Hi! Good decision. Do you planning to make clustering asynchronous?

Regarding detail of annotations

OCMapView cluster library is really good and easy to use.
How to get detail of annotation on click on any annotation callout for moving on detail section and differentiate all annotation for both category. and one more thing can i add more then 2 category for groping.please reply me so i can use library easily..& welcome idea and suggestion....
Thanks,

annotationsToIgnore are only refreshed every other reload

hi,

I am using OCMapViews with a a few annotations that should always be visible. Adding and clustering all work, but when I zoom in or pan the view, the view is reloaded, and then the annotationsToIgnore are gone. Upon a next refresh, they are back. I could locate the issue to the final lines of the doClustering method:

    // Clear map but leave Userlcoation
    NSMutableArray *annotationsToRemove = [[NSMutableArray alloc] initWithArray:self.displayedAnnotations];
    [annotationsToRemove removeObject:self.userLocation];

    // add clustered and ignored annotations to map
    [super addAnnotations: clusteredAnnotations];
    [super addAnnotations: [annotationsToIgnore allObjects]];

    // fix for flickering
    [annotationsToRemove removeObjectsInArray: clusteredAnnotations];
    [super removeAnnotations:annotationsToRemove];

What happens is that upon first view, the annotationsToIgnore are are added to the annotations that are going to be displayed by super. However, upon second view, the annotationsToRemove is loaded with the current visible annotations., including the ones that are to be ignored during clustering. Of course, they are added when [super addAnnotations: [annotationsToIgnore allObjects]]; is executed, but as annotationsToIgnore objects are also in the annotationsToRemove, they are basically added at first but removed at the next line. For the third view, it is basically starting with a clean slate., so the annotationsToIgnore are back

A solution to this to change the order of the last two parts of the code above: first do the remove on super and then add all annotations. There is flickering, though, for the annotationsToIgnore

Showing all pins...

Hello,

Is there a way to show all the pins, but have the ones with the same long/lat display a different color?

I almost have that now, but I do not want it to expand out when I zoom. I was hoping I could show all the pin, but the ones in the same location get clustered (or grouped) and show a different color indicating multiple people at that address.

Thank you

OCMapView memory usage

Hi! I discovered one really bad problem while using OCMapView in my app. When the map is allocated - there is a huge block of data loaded into memory. I know that it`s normal, but the problem is that when i dealloc a map - some portion of memory is still present. It looks like not everything is deallocated. Im not using ARC in my app.

FilterAnnotationsForVisibleMap has problems with some of my annotations

I had some problems with a part of my annotations that are disappearing when i zoom in. The viewForAnnotation delegate was no longer called for this annotations and when i zoom out they appear again.
In my application that contents annotations in portugal, Spain, Italy, Swizerland, Germany and Greek this only happens for the annotations in portugal. It seems that filterAnnotationsForVisibleMap is not working properly for that annotations.

I just replaced the code of that method with the following and now it if working fine for me:

  • (NSArray *)filterAnnotationsForVisibleMap:(NSArray *)annotationsToFilter
    {
    NSMutableArray *filteredAnnotations = [[NSMutableArray alloc] initWithCapacity:[annotationsToFilter count]];

    MKCoordinateRegion self_region = self.region;

    for (id annotation in annotationsToFilter)
    {
    // if annotation is not inside the coordinates, kick it
    if([annotation coordinate].latitude > (self_region.center.latitude - self_region.span.latitudeDelta/2.0) &&
    [annotation coordinate].latitude < (self_region.center.latitude + self_region.span.latitudeDelta/2.0) &&
    [annotation coordinate].longitude > (self_region.center.longitude - self_region.span.longitudeDelta/2.0) &&
    [annotation coordinate].longitude < (self_region.center.longitude + self_region.span.longitudeDelta/2.0)) {
    [filteredAnnotations addObject:annotation];
    }
    }

    return filteredAnnotations;
    }

Error in OCAnnotation.m

- (void)setSubtitle:(NSString *)text
{
[text retain];
[title release];
subtitle = text;
}

Should be [subtitle release]

Clustering when panning

Cluster when panning is broken on new version guys.
I'll make a video later, but just so you can test with the example App, if you make a pan movement the cluster drawing disappears and is appears just after some movements.
This behaviour wasn't like that on the past version.

Facing issue with showing pins / clusters

In my app I have MKMapView which works well, then I have replaced MKMapView with OCMapView and pins stopped being displayed. and I found viewForAnnotation function stopped being called. Only thing I found different from sample project is method for adding annotation, in sample project addAnnotations method is used but in my case I have to use addAnnotation method in loop.

Any idea what I am missing or any workaround to call viewForAnnotation and start displaying pins.

Thanks

Update Usage docs

I played with OCMapView a while ago and came back to use it in my new project.
The documentation set's you up really simple with comment like

"Just replace your MKMapView references with OCMapView. It automatically handles annotations and combines them to clusters for you without any other change of your code."

Though I struggled to begin with the actual clustering and it hits me again this time. It is important to add

- (void)mapView:(MKMapView *)aMapView regionDidChangeAnimated:(BOOL)animated {
    [self.mapView doClustering];
}

which is not mentioned in the docs.
You might consider adding this to get new users a better start with OCMapView.

memory leak in OCAlgorithms

Hi,

First I want to thank all the contributors to this great project !
I believe there is a memory leak in the OCAlgorithms.m file raised also by xcode clang analyser.

Line 137 :
// check group if (grouped && [annotation respondsToSelector:@selector(groupTag)]) { if (![clusterAnnotation.groupTag isEqualToString:((id <OCGrouping>)annotation).groupTag]) continue; } // add annotation to the cluster [clusterAnnotation addAnnotation:annotation]; [clusterAnnotation release];

There should be [clusterAnnotation release] before the continue instruction

Thanks again for your great job!

Yunus

Annotations in same coordinate

Hello, I like you lib!

What should I do if I have Annotations in same coordinate? They can not be separated
I want to see separated annotationViews

Annotation views in Part of the world

Hi,

Using the sample project, and pressing the add 100 annotations button over America, nothing happens. When i scroll the map a little to the right/left the annotations appear, when i centre it over America, they disappear again.

Kind regards
Simon

Seleted cluster

Hello, I am select the cluster containing an annotation chooses how? cluster contains 3 example I annotations 1 , 2, 3 if I type 4 by not select against if I type 2 select the cluster.

id < MKAnnotation > ann;
for (int i = 0; i < [[self.mapView annotations] count]; i++)
{
for (ann in [self.mapView annotations])
{
NSString *annTitle = ann.title;
if ([annTitle isEqualToString:[NSString stringWithFormat:@"%d",numSc]])
{
[self.mapView selectAnnotation:ann animated:NO];
bToptrouve=true;
break;
}
}
if (bToptrouve) {
break;
}
}

memory bug

It seems to be that i have a bad, bad memory bug somewhere and i'm unable to find it.
The app crashes when the users zoom entirely out and removes all annotations or turn of clustering.

I'm unable to trace back the leak/whatever. Instruments say, it has to be in the OCAnnotation class in the line where i call super, which i don't really understand.

In addition to that, KVO throws a log:
An instance 0x5ba8170 of class OCAnnotation was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object.
Since i don't use any KVO, i technically wonder where this comes from.

I'm looking for some help from you guys. Help to make this control awesome. ;D

Problem with zooming in

thanks for the code first of all.
I'm having an issue with zooming in when selecting a cluster. In select annotation delegate method, I managed to select cluster and zoom in to a specific level. But, at a certain level it doesn't allow me to zoom in more. For example, there is a cluster which grouped 4 annotations. When I select that cluster, map zooms in and break the cluster into 2 separate annotations and 1 cluster. Then I select the cluster, it doesn't zoom in. I played with zooming rate and found that it lets me zoom in to a certain level. I don't know what's wrong actually.

mapView:viewForAnnotation: is not being called

I encounter strange problem when using OCMapView. After replacing MKMapView with OCMapView mapView:viewForAnnotation is not being called for my annotations anymore. It is only being called for MKUserLocation.
(I am not using Interface Builder, but allocating by code).
I have also added groupTag property to my annotation class.

What I am doing wrong?

How can i Differentiate pin cluster for multiple combination.

This is very good cluster example. I am implementing in my own project but i have a issue for cluster type.
I want to show three type of circle for 2 different category(A & B).
I have 2 type pin which makes three combination of cluster circle.
-> One for pint type A
-> Second for pint type B
-> Third for pint type A & B Both
If Cirlcle have only pin A type then circle color for A_color.
If Cirlcle have only pin B type then circle color for B_color
If Cirlcle have only pin A & B Both type then circle color for combination of A & B color
please help me for this combination how can i achieve in my project.
Thanks

Overlay support?

Hi,

I have exactly the same performance issue for overlays. Is there a easy way to use it with the overlays in stead of the annotations?

Thanks in advance.

Issue or Feature...

Hello,

I am using OCMapView in an App and it is wonderful!!! But I do have a question about the way the cluster is determined. In regionDidChangeAnimated I call doClustering but as I move the map around the screen the pins sometime change location slightly with the region. By region I am referring to the yellow circle around the clustered area. Without looking too much into the source, I was guessing that this is due to the fact that the long and lat of the pin pin which marks the circle is changing.

Is this the way it should work, or can this be corrected by possibly sorting the longs and lats in the region and then the one used for the pin would be the same each time?

This would of course not make a difference if the map was zoomed but it may help when moved.

Anyway, maybe it is just something I am doing wrong but in any case OCMapView is great!!

Marker Grid Algorithm will be implemented

This is very helpful which we have integrating in our app. Is in there is any way cluster using the grid marker algorithm. since it already using grid algorithm is there is any possibility to include it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.