objective c - IOS custom pin is not showing -
i trying show image instead of default red pin source location , image destination location.
- (void) sourcemarker:(mkplacemark * ) homeloc { mkannotationview *point = [[mkannotationview alloc] init]; mkpointannotation *ml = point.annotation; ml.coordinate = homeloc.coordinate; ml.title = @"home"; point.image = [uiimage imagenamed:@"home.png"]; point.canshowcallout = yes; [self.mapview addannotation:ml]; }
as suggested apple doc annotating maps. 1 has create custom annotation class, please refer following code.
myannotation.h
@interface myannotation : nsobject<mkannotation> { cllocationcoordinate2d coordinate; nsstring *title; nsstring *subtitle; nsstring *type; } @property (nonatomic, assign) cllocationcoordinate2d coordinate; @property (nonatomic, copy) nsstring *title; @property (nonatomic, copy) nsstring *subtitle; @property (nonatomic, copy) nsstring *type; @end
myannotation.m
@implementation myannotation @synthesize coordinate; @synthesize title; @synthesize subtitle; @synthesize type; @end
yourviewcontroll.m - showing map.
create annotation , set type property
myannotation *ann = [[myannotation alloc] init]; offann.coordinate = offlocation.coordinate; offann.title = @"my office"; offann.type = @"office"; [_mapview removeannotation: offann]; [_mapview addannotation: offann]; - (mkannotationview *)mapview:(mkmapview *)mapview viewforannotation:(id <mkannotation>)annotation { nslog(@"viewforannotation called"); if ([annotation iskindofclass:[myannotation class]]) { myannotation *myann=(myannotation *)annotation; mkannotationview *pinview = (mkannotationview*)[self.mapview dequeuereusableannotationviewwithidentifier:@"myannotation"]; if (!pinview) { pinview = [[mkannotationview alloc] initwithannotation:annotation reuseidentifier:@"myannotation"]; //pinview.animatesdrop = yes; pinview.canshowcallout = yes; pinview.calloutoffset = cgpointmake(0, 4); } else { pinview.annotation = annotation; } if([myann.type isequal: @"office"]) { pinview.image = [uiimage imagenamed:@"office.png"]; } return pinview; } return nil; }
Comments
Post a Comment