charlie johnston
charlie johnston

Reputation: 67

MKAnnotations not reloading correctly

Having trouble with MKAnnotation. Have a custom pinview and works fine for the first loading. Go to remove pins and then reload same pins they change colors. Im adding pins from two different DB's and works just fine. Once removed and then add each array separately the second array takes the first arrays custom pin instead of the one assigned.

- (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id   <MKAnnotation>)annotation
{

MKAnnotationView *pinView = nil; 
if(annotation != mapView.userLocation) 
{
    static NSString *defaultPinID = @"pin";
    pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil ) 
        pinView = [[MKAnnotationView alloc]
                   initWithAnnotation:annotation reuseIdentifier:defaultPinID];

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    [rightButton addTarget:self
                    action:@selector(showDetails:)
          forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = rightButton;

    UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"stores.png"]];
    pinView.leftCalloutAccessoryView = profileIconView;

    NSString *badgestringss = @"8 reviews";
    customBadge1 = [CustomBadge customBadgeWithString:badgestringss 
                                      withStringColor:[UIColor whiteColor]
                                       withInsetColor:RGB(255, 51, 0)
                                       withBadgeFrame:YES 
                                  withBadgeFrameColor:[UIColor whiteColor] 
                                            withScale:1.0
                                          withShining:YES];
    [customBadge1 setFrame:CGRectMake(100, 1, 85, 15)];
    [pinView.leftCalloutAccessoryView addSubview:customBadge1];



    if(setStoreOrShops==NO){
    pinView.image = [UIImage imageNamed:@"stores.png"];    //as suggested by Squatch  
    }
    else if (setStoreOrShops==YES){
         pinView.image = [UIImage imageNamed:@"shops.png"];   
    }


   else {
    [mapView.userLocation setTitle:@"Current Location"];
}
     }
return pinView;
}

have searched all over but cant seem to get an example to work or an idea where this is breaking down. Thanks for any help.

Upvotes: 0

Views: 237

Answers (2)

charlie johnston
charlie johnston

Reputation: 67

got it fixed this way

MyAnnotation *myAnnot = (MyAnnotation *)annotation;

if (myAnnot.mappin == @"42")
    customPinView.image = [UIImage imageNamed:@"shops.png"];
else
    customPinView.image = [UIImage imageNamed:@"stores.png"];   

Upvotes: 1

sc0rp10n
sc0rp10n

Reputation: 1118

I'm not sure what you mean by changing color, as I don't know what your CustomBadge code does or what your images look like. However, the problem could be that you are applying conditional logic inside of your initial setup of the view. Later when dequeReusableAnnotationViewWithIdentifier: actually returns something, it's been configured for something else.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

    MKAnnotationView *pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"default"];
    if (pinView == nil) {
        // CONFIGURE ONCE
        // things that are set up once and never change
    }

    // CONFIGURE EVERY VIEW
    // view configurations that change every pin
}

You need to take the code that sets your images out of the "configure once" section and put it in the "configure for every view" section. Otherwise, every time you make a call to dequeue a reusable view you can get something that is configured incorrectly.

Upvotes: 1

Related Questions