ios - Why are my custom annotation images changing? -
i have 21 different images using custom annotations on map. when load map initially, perfect. when navigate off of area , in seems images change. annotation pins no longer correspond correct image (however title , subtitle still correct when press on annotation). images mixed up. way entering test information nsuserdefaults , using test functionality of displaying custom annotations on map. assuming has reusability of annotation views cannot 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 ios 8. without guard code crash "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
for 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 line:
pinview.image = [uiimage imagenamed:[nsstring stringwithformat:@"%@annotation", [annotation title]]]; also else, 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; }
Comments
Post a Comment