Skip to content

A drop-in MapKit/MKAnnotation pin clustering library for MKMapView on iOS

License

Notifications You must be signed in to change notification settings

Cleverlance/kingpin

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kingpin

A drop-in MKAnnotation clustering library for iOS

features

  • Uses a 2-d tree under the hood for maximum performance
  • No subclassing required, making the library easy to integrate with existing projects.

usage

Create an instance of a KPTreeController:

self.treeController = [[KPTreeController alloc] initWithMapView:self.mapView]

Set the controller's annotations:

[self.treeController setAnnotations:[self annotations]];

Handle the clusters:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    
    KPAnnotation *a = (KPAnnotation *)annotation;
    
    MKPinAnnotationView *v = 
      (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];
    
    if(!v){
        v = [[MKPinAnnotationView alloc] initWithAnnotation:a reuseIdentifier:@"pin"];
    }
    
    v.pinColor = (a.annotations.count > 1 ? MKPinAnnotationColorPurple : MKPinAnnotationColorRed);
    
    return v;
    
}

Note: You can gain access to the cluster's annotations via -[KPAnnotation annotations].

Refresh visible annotations as needed:

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    [self.treeController refresh:self.animationSwitch.on];
}

This is typically done in -mapView:regionDidChangeAnimated:

demo

Check out the tester target in kingpin.xcodeproj

licence

Apache 2.0

About

A drop-in MapKit/MKAnnotation pin clustering library for MKMapView on iOS

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 98.5%
  • Ruby 1.5%