iosobjective-cmkpinannotationview

Trying to change MKPointAnnotation colour but losing the title


I am trying to change my pin colour to purple, when I do it I lose the title though. Code is:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.navigationBarHidden=YES;

    //init the location manager
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager requestWhenInUseAuthorization];
    self.mapView.showsUserLocation=YES;

    self.userGeoPoint=self.message[@"userLocation"];
    self.pinView = [[MKPointAnnotation alloc] init];

    self.pinView.title=self.message[@"fromUser"];
    self.coord = CLLocationCoordinate2DMake(self.userGeoPoint.latitude, self.userGeoPoint.longitude);
    self.pinView.coordinate=self.coord;
    //use a slight delay for more dramtic zooming
    [self performSelector:@selector(addPin) withObject:nil afterDelay:0.5];
}

-(void)addPin{

    [self.mapView addAnnotation:self.pinView];
    [self.mapView selectAnnotation:self.pinView animated:YES];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.coord, 800, 800);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];

}

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotationPoint
{
    if ([annotationPoint isKindOfClass:[MKUserLocation class]])//keep the user as default
        return nil;

    static NSString *annotationIdentifier = @"annotationIdentifier";
    MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotationPoint reuseIdentifier:annotationIdentifier];
    pinView.pinColor = MKPinAnnotationColorPurple;
    //now we can throw an image in there

    return pinView;
}

I tried setting the title property for MKPinAnnotation but there isn't one. Is there anyway I can get around this?


Solution

  • In viewForAnnotation, you need to set canShowCallout to YES (it's NO by default):

    pinView.pinColor = MKPinAnnotationColorPurple;
    pinView.canShowCallout = YES;
    



    A couple of unrelated points:

    1. It looks like you have a property named pinView of type MKPointAnnotation that you are using to create the annotation object in viewDidLoad. Then in viewForAnnotation, you have a local variable also named pinView of type MKPinAnnotationView. Although there is no naming conflict here, it causes and implies some conceptual confusion. MKPointAnnotation is an annotation model class while MKPinAnnotationView is an annotation view class -- they are completely different things. It would be better to name the annotation property pin or userAnnotation for example.
    2. This comment in viewForAnnotation:

      //now we can throw an image in there
      

      seems to imply that you could set a custom image in the view at this point. Setting a custom image in a MKPinAnnotationView is not recommended since that class is designed to display default pin images in one of three colors. To use a custom image, create a plain MKAnnotationView instead.