0
votes

I have custom pin images for different locations, i can show all the different pin at the same time. But Proble is, when i show Current Location of the User, All the Pins color change.

here is the Code:

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

if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

if ([annotation isKindOfClass:[MapAnnotation class]]) {

    MKAnnotationView *test=[[MKAnnotationView alloc] 
                            initWithAnnotation:annotation reuseIdentifier:@"AnnotationIdentifier"];

    test.canShowCallout = YES;
    //        test.animatesDrop = YES;


    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    test
    .rightCalloutAccessoryView = rightButton;

    switch (_pushpinTag) {
        case 5:
            test.image = [UIImage imageNamed:@"pushpin_green.png"];
            break;

        case 6:
            test.image = [UIImage imageNamed:@"pushpin_blue.png"];
            break;

        case 7:
            test.image = [UIImage imageNamed:@"pushpin_black.png"];
            break;

        case 8:
            test.image = [UIImage imageNamed:@"pushpin_yellow.png"];
            break;

        case 3:
            test.image = [UIImage imageNamed:@"pushpin_red.png"];
            break;

        default:
            break;
    }

    return test;
}
}

Now on different button press, different Pins (with custom images) are shown. Lets say i Have pins with Green, Blue, Black and Yellow. I press button to show Green Pins, then For Blue, then for Black, all the Pins show in their respective images. But When i Press the button to show Users Current Location, all the Pins changes to the last press Pin, which was Black.

Here is the Code to show User's Current Location:

- (IBAction)currentLocationButton:(id)sender {

_mapView.showsUserLocation = YES;
[_mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];

}

Can anyone point out whats wrong am i doing?

Thank you everyone :)

2
What is _pushpinTag and how is it set?user467105

2 Answers

2
votes

Your annotations need to contain something that you can use to set their colour in viewForAnnotation. The MKAnnotation protocol defines all annotations to have title, subtitle and coordinate. If title and subtitle are not enough to determine which colour you want your pin, write your own class and add a property called pinType. Then when you create the annotation set pinType according to the button the user has pressed. When viewForAnnotation gets called you do the usual dequeueReusableAnnotationViewWithIdentifier/initWithAnnotation to get a view ready, cast the provided annotation to your class and use its pinType to set the image. here's some untested code, enough to give you the idea

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MyAnnotation class]])
    {
        MyAnnotation* myAnno = (MyAnnotation)annotation;
        MKAnnotationView *test;

        test = (MKAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:@"AnnotationIdentifier"];
        if (view == nil)
        {
            test=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"AnnotationIdentifier"]; 
        } 
        switch(myAnno.pinType)
        {
            case kBLACK_TAG: test.image = [UIImage imageNamed:@"pushpin_black.png"]; 
                             break;
        }
    }
}
1
votes

you're not making use of reusable views

MKAnnotationView *test;

test = (MKAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:@"AnnotationIdentifier"];
if (view == nil)
{
 test=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"AnnotationIdentifier"]; 
 test.tag = _pushpinTag;    // set tag of new pins to _pushpinTag
} 

secondly, your general error is a logic error. Whenever iOS is asking for your annotation view, you're setting the image based on the value of _pushpinTag, which is why all your pins are redrawing as the last selected color. If you also set a TAG value on your pin, something like this:

static int kGREEN_TAG = 5;
static int kBLUE_TAG = 6;
static int kBLACK_TAG = 7;
static int kYELLOW_TAG = 8;

switch (test.tag)
{
 case kBLACK_TAG:
            test.image = [UIImage imageNamed:@"pushpin_black.png"]; 
            break; 
.
.
.
}