ios - How to add an image for each customized annotation on MapView? -
ios - How to add an image for each customized annotation on MapView? -
i have class of picture
contains latitude
, longitude
, image
of each location. want add together them annotation mapview
, show image instead of pin locations.
i have customized annotation is:
@interface annotation : mkannotationview <mkannotation> @property (nonatomic,assign) cllocationcoordinate2d coordinate; @property (nonatomic,copy) nsstring *title; @property (nonatomic,copy) nsstring *subtitle; @property (nonatomic,retain) uiimage *image; @end
and
@implementation annotation @synthesize title,subtitle,coordinate,image; @end
now in main code trying this:
cllocationcoordinate2d location; annotation *myann; for(pictures *pic in picturesfromdb) { myann = [[annotation alloc] init]; location.latitude = [pic.langt doublevalue]; location.longitude = [pic.longt doublevalue]; myann.coordinate = location; myann.title = pic.descript; myann.image = [uiimage imagewithdata:pic.image]; //not sure line! [self.mapview addannotation:myann];
}
do still need delegate , must phone call "viewforannotation"? because did not work, did delegate:
- (mkannotationview *)mapview:(mkmapview *)mapview viewforannotation:(id <mkannotation>)annotation { // if it's user location, homecoming nil. if ([annotation iskindofclass:[mkuserlocation class]]) homecoming nil; // handle custom annotations. if ([annotation iskindofclass:[annotation class]]) { // seek dequeue existing pin view first. mkannotationview *pinview = (mkannotationview*)[mapview dequeuereusableannotationviewwithidentifier:@"custompinannotationview"]; if (!pinview) { // if existing pin view not available, create one. pinview = [[mkannotationview alloc] initwithannotation:annotation reuseidentifier:@"custompinannotationview"]; //pinview.animatesdrop = yes; pinview.canshowcallout = yes; pinview.image = [uiimage imagenamed:@"image.png"]; pinview.calloutoffset = cgpointmake(0, 4); } else { pinview.annotation = annotation; } homecoming pinview; } homecoming nil; }
this working fine adding same image locations. have specific image show instead of image.png
above each location.
number of location variable , dynamic.
how can pass image delegate. tried find image field in passed annotation, not exist! appreciate suggestion.
you need cast annotation custom class can access properties -
- (mkannotationview *)mapview:(mkmapview *)mapview viewforannotation:(id <mkannotation>)annotation { // handle custom annotations. if ([annotation iskindofclass:[annotation class]]) { annotation *myann=(annotation *)annotation; // seek dequeue existing pin view first. mkannotationview *pinview = (mkannotationview*)[mapview dequeuereusableannotationviewwithidentifier:@"custompinannotationview"]; if (!pinview) { // if existing pin view not available, create one. pinview = [[mkannotationview alloc] initwithannotation:annotation reuseidentifier:@"custompinannotationview"]; //pinview.animatesdrop = yes; pinview.canshowcallout = yes; pinview.calloutoffset = cgpointmake(0, 4); } else { pinview.annotation = annotation; } pinview.image = myann.image; homecoming pinview; } homecoming nil; }
ios xcode annotations mapkit mkannotationview
Comments
Post a Comment