为什么我的自定义注释图像会发生变化?

时间:2015-05-31 05:37:46

标签: ios objective-c cocoa-touch mapkit mkannotationview

我在地图上使用21个不同的图像进行自定义注释。当我最初加载地图时,一切都很完美。当我离开该区域并返回时,似乎图像会发生变化。注释引脚不再与正确的图像对应(但是当我按下注释时标题和副标题仍然正确)。就像图像突然混淆了一样。顺便提一句,我只是将测试信息输入NSUserDefaults并使用它来测试在地图上显示自定义注释的功能。我假设它与注释视图的可重用性有关,但我无法正确获取我的代码。

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

1 个答案:

答案 0 :(得分:1)

对于本节:

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

添加以下行:

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

同样对于其他人,所以它看起来像那样:

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