1

I'm building an app using swift and MapKit. It has several custom point annotations. I would like to display the title above all annotations at any time, by default. Hence they would be all selected. Therefore I could get inspiration from this post and use:

[mapView selectAnnotation:pinView animated:YES]

But I still need to be able to select a pin by clicking on it and get a new mapScope based on this annotation. How could I do it?

If the first solution is not possible, I would use a specific label on each annotation. But then is there a way to hide the annotation title forever?

EDIT:

Here is the code for the custom point annotation:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    println("viewForAnnotation")
    if !(annotation is MKPointAnnotation) {
        return nil
    }
    var seleccion:Bool

    let cpa = annotation as! CustomPointAnnotation

    let reuseId = cpa.nickName as String
    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)


    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView.canShowCallout = true
//            create and add UILabel only when actually creating MKAnnotationView

        var nameLbl: UILabel! = UILabel(frame: CGRectMake(-24, 40, 100, 30))
        nameLbl.tag = 42
        nameLbl.textColor = UIColor.blackColor()
        nameLbl.font = UIFont(name: "Atari Classic Extrasmooth", size: 10)
        nameLbl.textAlignment = NSTextAlignment.Center
        anView.addSubview(nameLbl)
    }
    else {
        anView.annotation = annotation
    }

    anView.image = cpa.image
    if cpa.toBeTriggered == true {
        anView.selected = true
    }

    if let nameLbl = anView.viewWithTag(42) as? UILabel {
        nameLbl.text = cpa.nickName
    }

    return anView
}
Community
  • 1
  • 1
Quentin Malgaud
  • 405
  • 6
  • 21

1 Answers1

1

As you are using custom annotation views this code will be helpful to you.

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation  //thammu
{
    static NSString *reuseId = @"StandardPin";

    MKAnnotationView *aView;

    //MKAnnotationView *aView = (MKAnnotationView *)[Mapsview dequeueReusableAnnotationViewWithIdentifier:reuseId];

    aView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                     reuseIdentifier:reuseId];

    aView.enabled = YES;
    aView.canShowCallout = YES;

     aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];


    UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(-41,-22,121,25)];
    img.image=[UIImage imageNamed:@"annotation-bg@2x.png"]; //This is the Details image view


    UILabel *lblPlaceholder1=[[UILabel alloc]init];

    lblPlaceholder1 = [[UILabel alloc]initWithFrame:CGRectMake(4,2,117, 21)];
    lblPlaceholder1.backgroundColor=[UIColor clearColor];
    lblPlaceholder1.numberOfLines=1;
    [lblPlaceholder1 setTextColor:[UIColor redColor]];
    lblPlaceholder1.textAlignment=NSTextAlignmentCenter;
    lblPlaceholder1.text=annotation.title;
    [lblPlaceholder1 setTextColor:[UIColor darkGrayColor]];
    [lblPlaceholder1 setFont:[UIFont fontWithName:@"Roboto-Regular" size:12.0f]];


    NSString *subTitleStr = annotation.subtitle;


    [img addSubview:lblPlaceholder1];

    [aView addSubview:img];

    [img setUserInteractionEnabled:YES];

    aView.image = [UIImage imageNamed:@"tracker.png"];//This is marker image view

    aView.annotation = annotation;

    return aView;
}

Here all the annotations in map are by default visible.

Dev
  • 1,215
  • 9
  • 17
  • 1
    hi @Dev, thank you for the quick answer. I already use a code quite similar for a label. The problem is that If the user clicks on the annotation, there is still the standard title (&subtitle) label being displayed and it messes up with the custom label. So what I would like is: 1. Use only the title, all annotations should have their title visible at any time while still being "clickable" 2. Use a custom label, but never display the standard label – Quentin Malgaud Aug 20 '15 at 12:38