Apple MapKit with Kingpin annotations clustering custom annotations

689 views Asked by At

I have built a Map application that uses the Kingpin map pin clustering library, (found here) at present the library succeeds in approximating the position of the pins and placing the cluster pin. However, the original pins are never removed as seen here. I think the relevant code is this:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKAnnotationView *annotationView = nil;
if([annotation isKindOfClass:[KPAnnotation class]]){
    KPAnnotation *kingpinAnnotation = (KPAnnotation *)annotation;
    if ([kingpinAnnotation isCluster]) {
        annotationView=(MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"cluster"];
        if (annotationView==nil){
            annotationView=[[MKAnnotationView alloc]initWithAnnotation:kingpinAnnotation reuseIdentifier:@"cluster"];
            annotationView.canShowCallout=YES;

            annotationView.image=[UIImage imageNamed:@"icon_notif_recall.png"];
            annotationView.frame=CGRectMake(0,0,25,25);
        }
    }else{
        annotationView=(MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];
        if (annotationView==nil){
            annotationView=[[MKAnnotationView alloc]initWithAnnotation:[kingpinAnnotation.annotations anyObject]reuseIdentifier:@"pin"];
            annotationView.canShowCallout=YES;

            annotationView.image=[UIImage imageNamed:@"icon_notif_recall.png"];
            annotationView.frame=CGRectMake(0,0,25,25);
        }
    }
    return annotationView;
}else if([annotation isKindOfClass:[REC_CustomAnnotation class]]){//Check that is our custom pin class
    REC_CustomAnnotation *myLocation = (REC_CustomAnnotation *)annotation;
    MKAnnotationView *annotationView=[mapView dequeueReusableAnnotationViewWithIdentifier:@"REC_CustomAnnotation"];

    if(annotationView==nil){
        annotationView=myLocation.annotationView;
    }else{
        annotationView.annotation=annotation;
    }
    return annotationView;
}else{
    return annotationView;
}

}

but I am really not sure what is wrong. Any thoughts on what the problem is or things to try would be greatly appreciated.

1

There are 1 answers

0
user3192649 On BEST ANSWER

As it turned out, the function posted above was in fact the problem. However, I was on the complete wrong track in terms of understanding how kingpin worked. Got everything working with this:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKAnnotationView *annotationView = nil;
if ([annotation isKindOfClass:[KPAnnotation class]]) {
    KPAnnotation *a = (KPAnnotation *)annotation;
    if ([annotation isKindOfClass:[MKUserLocation class]]){
        return nil;
    }
    if (a.isCluster) {
        annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"cluster"];
        if (annotationView == nil) {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:a reuseIdentifier:@"cluster"];
        }
        annotationView.image=[UIImage imageNamed:@"icon_notif_recall.png"];
        annotationView.frame=CGRectMake(0,0,25,25);
    }else {
        annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];
        if (annotationView == nil) {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:[a.annotations anyObject] reuseIdentifier:@"pin"];
        }

        KPAnnotation *thisKPAnnot=(KPAnnotation *)annotation;
        NSMutableArray *arrayOfAnnots = [NSMutableArray arrayWithArray:[[thisKPAnnot annotations] allObjects]];
        if([arrayOfAnnots count]==1){
            REC_CustomAnnotation *thisAnnot=(REC_CustomAnnotation *)[arrayOfAnnots objectAtIndex:0];
            if(thisAnnot.type==1){
                annotationView.image=[UIImage imageNamed:@"icon_notif_facebook.png"];
            }else if(thisAnnot.type==2){
                annotationView.image=[UIImage imageNamed:@"icon_notif_twitter.png"];
            }else if(thisAnnot.type==3){
                annotationView.image=[UIImage imageNamed:@"icon_notif_instagram.png"];
            }else if(thisAnnot.type==4){
                annotationView.image=[UIImage imageNamed:@"icon_notif_foursquare.png"];
            }else if(thisAnnot.type==5){
                annotationView.image=[UIImage imageNamed:@"icon_notif_camera.png"];
            }else{
                annotationView.image=[UIImage imageNamed:@"icon_notif_recall.png"];
            }
        }else{annotationView.image=[UIImage imageNamed:@"icon_notif_recall.png"];}
        annotationView.frame=CGRectMake(0,0,25,25);
    }
    annotationView.canShowCallout = YES;
}
return annotationView;

}

I hope this ends up helping someone in the future.