2015-05-31 108 views
1

我有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 
+0

引脚图像相互混合,或者你看到其他图像/空图像? – Asaf

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

谢谢你!这似乎修复了它。 –