Claude Bastien
Claude Bastien

Reputation: 141

Why are my custom annotation images changing?

I have 21 different images that I am using for custom annotations on a map. When I load the map initially, everything is perfect. It is when I navigate off of the area and back in that it seems that the images change. The annotation pins no longer correspond with the correct image (however the title and subtitle are still correct when I press on the annotation). It is like the images suddenly get mixed up. By the way I am just entering test information into NSUserDefaults and using that to test the functionality of displaying the custom annotations on the map. I am assuming it has something to do with the reusability of annotation views but I cannot get my code right.

MapViewController.h

#import "ViewController.h"
#import <MapKit/MapKit.h>

@interface MapViewController : ViewController<MKMapViewDelegate>
{
    NSUserDefaults *defaults;
    NSMutableArray *reportedSightings;
}

@property (strong, nonatomic) IBOutlet MKMapView *mapView;

@end

MapViewController.m

#import "MapViewController.h"
@import CoreLocation;

@interface MapViewController ()<CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;

@end

@implementation MapViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    defaults = [NSUserDefaults standardUserDefaults];
    reportedSightings = [[NSMutableArray alloc] init];


    [reportedSightings addObject:@"turtle1,44.008897,-77.743073"];
    [reportedSightings addObject:@"turtle2,43.997620,-77.675193"];
    [reportedSightings addObject:@"turtle3,44.001554,-77.689685"];
    [reportedSightings addObject:@"turtle4,43.992655,-77.712643"];
    [reportedSightings addObject:@"turtle5,44.005708,-77.725666"];

    [reportedSightings addObject:@"snake1,43.993950,-77.720637"];
    [reportedSightings addObject:@"snake2,43.994176,-77.717224"];
    [reportedSightings addObject:@"snake3,43.998308,-77.677515"];
    [reportedSightings addObject:@"snake4,44.007523,-77.719716"];
    [reportedSightings addObject:@"snake5,43.997320,-77.731911"];
    [reportedSightings addObject:@"snake6,43.995390,-77.716450"];

    [reportedSightings addObject:@"amphibian1,43.996503,-77.694154"];
    [reportedSightings addObject:@"amphibian2,43.989638,-77.704582"];
    [reportedSightings addObject:@"amphibian3,44.009406,-77.738130"];
    [reportedSightings addObject:@"amphibian4,44.001059,-77.733429"];
    [reportedSightings addObject:@"amphibian5,44.005405,-77.713410"];
    [reportedSightings addObject:@"amphibian6,44.002750,-77.699245"];
    [reportedSightings addObject:@"amphibian7,43.999160,-77.692886"];
    [reportedSightings addObject:@"amphibian8,43.993869,-77.722742"];
    [reportedSightings addObject:@"amphibian9,43.995589,-77.714681"];
    [reportedSightings addObject:@"amphibian10,43.998462,-77.675608"];


    [defaults setObject:reportedSightings forKey:@"reportedSightings"];
    [defaults synchronize];
    reportedSightings = [NSMutableArray arrayWithArray:[defaults objectForKey:@"reportedSightings"]];



    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
        [self.locationManager requestWhenInUseAuthorization];

    [self.locationManager startUpdatingLocation];
    self.mapView.showsUserLocation = YES;

    MKCoordinateRegion region = {{0.0, 0.0}, {0.0, 0.0}};
    region.center.latitude = 43.998564;
    region.center.longitude = -77.709888;

    [self.mapView setRegion:region];



    for(int i=0; i<reportedSightings.count; i++)
    {
        NSString *reportedSighting = reportedSightings[i];
        NSString *speciesName = [reportedSighting substringToIndex:[reportedSighting rangeOfString:@","].location];
        reportedSighting = [reportedSighting substringFromIndex:[reportedSighting rangeOfString:@","].location+1];
        NSString *sightingLatitude = [reportedSighting substringToIndex:[reportedSighting rangeOfString:@","].location];
        reportedSighting = [reportedSighting substringFromIndex:[reportedSighting rangeOfString:@","].location+1];
        NSString *sightingLongitude = reportedSighting;


        float latitude = [sightingLatitude floatValue];
        float longitude = [sightingLongitude floatValue];
        CLLocationCoordinate2D reportedSightingCoordinates = CLLocationCoordinate2DMake(latitude, longitude);


        MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
        point.coordinate = reportedSightingCoordinates;
        point.title = speciesName;
        point.subtitle = [NSString stringWithFormat:@"%f, %f", latitude, longitude];
        [self.mapView addAnnotation:point];

    } 
}



- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    if ([self.mapView respondsToSelector:@selector(camera)])
    {
        MKMapCamera *newCamera = [[self.mapView camera] copy];

        [newCamera setPitch:0.0];
        [newCamera setHeading:307.197710];
        [newCamera setAltitude:14515.983058];

        [self.mapView setCamera:newCamera animated:YES];
    }
}


- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    if([annotation isKindOfClass:[MKPointAnnotation class]])
    {
        MKAnnotationView *pinView = (MKAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomViewAnnotation"];

        if(!pinView)
        {
            pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomViewAnnotation"];
            pinView.canShowCallout = YES;
            pinView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@Annotation", [annotation title]]];
        }

        else
        {
            pinView.annotation = annotation;
        }

        return pinView;
    }

    return nil;
}


@end

Upvotes: 1

Views: 230

Answers (1)

Asaf
Asaf

Reputation: 2168

For this section:

    if(!pinView)
    {
        pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomViewAnnotation"];
        pinView.canShowCallout = YES;
        pinView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@Annotation", [annotation title]]];
    }

    else
    {
        pinView.annotation = annotation;
    }

Add the line:

pinView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@Annotation", [annotation title]]];

Also for the else, so it will look like that:

if(!pinView)
    {
        pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomViewAnnotation"];
        pinView.canShowCallout = YES;
        pinView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@Annotation", [annotation title]]];
    }

    else
    {
        pinView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@Annotation", [annotation title]]];
        pinView.annotation = annotation;
    }

Upvotes: 1

Related Questions