2014-12-14 4 views
2

Я пытаюсь изменить цвет аннотаций штифтом на MKMapView переопределив это ниже:Изменить аннотацию контактный цвет на MKMapView

- (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; 
} 

Здесь ниже соответствующие методы:

- (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]; 

} 

} 

Моя проблема заключается в контактный аннотаций цвет по-прежнему отображается красным цветом по умолчанию не зеленым, как я хочу. Что мне здесь не хватает?

+0

Убедитесь вид карты в 'delegate' установлен или подключен. – Anna

+0

Привет @ Анна не могли бы вы рассказать мне, где это сделать или даже лучше показать мне пример? – SanitLee

+1

Если вид карты является IBOutlet, в xib или раскадровке подключите выход делегата карты к свойству «mapView». Или в 'viewDidLoad', do' self.mapView.delegate = self; '. Код в 'viewForAnnotation' выглядит нормально, но если делегат не установлен, метод не будет вызван. – Anna

ответ

0

Кредит @Anna, добавьте строку ниже, чтобы - (Недействительными) configureView

self.mapView.delegate = self; 
Смежные вопросы