iphone - Customize map annotations from plist -
i found short guide on how load "annotation data" plist , add annotations (pins) mapview.
my question if can tell me how customize pins easily? example, define color , image in plist , have set when adding pins.
the pins added this:
myannotation.m:
@implementation myannotation @synthesize title, subtitle, coordinate; -(void) dealloc { [super dealloc]; self.title = nil; self.subtitle = nil; }
myannotation.h
#import <foundation/foundation.h> #import <mapkit/mapkit.h> @interface myannotation : nsobject<mkannotation> { cllocationcoordinate2d coordinate; nsstring *title; nsstring *subtitle; } @property (nonatomic, assign) cllocationcoordinate2d coordinate; @property (nonatomic, copy) nsstring* title; @property (nonatomic, copy) nsstring* subtitle;
running through plist in viewdidload in mapviewcontroller.m (array containg dictionaries each pin) , adding annotations.
nsstring *path = [[nsbundle mainbundle] pathforresource:@"annotations" oftype:@"plist"]; nsmutablearray* anns = [[nsmutablearray alloc] initwithcontentsoffile:path]; for(int = 0; < [anns count]; i++) { float reallatitude = [[[anns objectatindex:i] objectforkey:@"latitude"] floatvalue]; float reallongitude = [[[anns objectatindex:i] objectforkey:@"longitude"] floatvalue]; myannotation* myannotation = [[myannotation alloc] init]; cllocationcoordinate2d thecoordinate; thecoordinate.latitude = reallatitude; thecoordinate.longitude = reallongitude; myannotation.coordinate = thecoordinate; myannotation.title = [[anns objectatindex:i] objectforkey:@"title"]; myannotation.subtitle = [[anns objectatindex:i] objectforkey:@"subtitle"]; [mapview addannotation:myannotation]; [annotations addobject:myannotation]; [myannotation release]; }
edit:
i have added viewforannotation method sets color how can set color of each pin individually?
-(mkannotationview *)mapview:(mkmapview *)mv viewforannotation:(id <mkannotation>)annotation { mkpinannotationview *pinview = nil; if(annotation != mapview.userlocation) { static nsstring *defaultpinid = @"com.invasivecode.pin"; pinview = (mkpinannotationview *)[mapview dequeuereusableannotationviewwithidentifier:defaultpinid]; if ( pinview == nil ) pinview = [[[mkpinannotationview alloc] initwithannotation:annotation reuseidentifier:defaultpinid] autorelease]; pinview.pincolor = mkpinannotationcolorpurple; pinview.canshowcallout = yes; pinview.animatesdrop = yes; } else { [mapview.userlocation settitle:@"i here"]; } return pinview; }
solved! can done in viewforannotation method:
for(myannotation* in mapview.annotations) { if(annotation == && annotation != mapview.userlocation) { pinview.image = [uiimage imagenamed:a.annimage]; } }
Comments
Post a Comment