简体   繁体   中英

Change annotation pin color on MKMapView

I am trying to change colour of annotation pin on MKMapView by overriding this below:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// If it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

// Handle any custom annotations.
if ([annotation isKindOfClass:[MKPointAnnotation class]])
{
    MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
    if (!pinView)
    {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
        pinView.canShowCallout = YES;
        pinView.pinColor = MKPinAnnotationColorGreen;
    } else {
        pinView.annotation = annotation;
    }
    return pinView;
}
return nil;
}

Here below are relevant methods:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[self configureView];
}


- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {

    // display map ????
    PFGeoPoint *geoPoint = self.detailItem[@"location"];
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(geoPoint.latitude,geoPoint.longitude);

    MKPointAnnotation *annotation =[[MKPointAnnotation alloc] init];
    annotation.coordinate = coordinate;

    // remove previous annotation pin
    if([self.mapView.annotations count] == 1) {
        [self.mapView removeAnnotation:[self.mapView.annotations objectAtIndex:0]];
    }

    [self.mapView addAnnotation:annotation];
    annotation.title = self.detailItem[@"text"];

    MKCoordinateSpan span = {.latitudeDelta =  0.05, .longitudeDelta =  0.05};
    MKCoordinateRegion region = {coordinate, span};
    [self.mapView setRegion:region];

    [self.mapView setCenterCoordinate:self.mapView.region.center animated:NO];

}

}

My problem is pin annotation colour is still shown Red as default not Green as I want. What am I missing here?

归功于@Anna,将以下行添加到-(void)configureView

self.mapView.delegate = self;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM