ios - Adding action to MKMapView annotation -
i have mkmapview bunch of annotations can't add action them. creating mkpointannotation, couldn't add action that. whenever click annotation, nothing happens. here how setting annotations -
cllocationcoordinate2d montevista; montevista.latitude = (double) 37.83029; montevista.longitude = (double) -121.98827; mkpointannotation *montevistapoint = [[mkpointannotation alloc] init]; montevistapoint.coordinate = montevista; montevistapoint.title = @"monte vista"; cllocationcoordinate2d sanramonvalley; sanramonvalley.latitude = (double) 37.82609; sanramonvalley.longitude = (double) -122.00603; mkpointannotation *sanramonvalleypoint = [[mkpointannotation alloc] init]; sanramonvalleypoint.coordinate = sanramonvalley; sanramonvalleypoint.title = @"san ramon valley"; cllocationcoordinate2d doughertyvalley; doughertyvalley.latitude = (double) 37.76845; doughertyvalley.longitude = (double) -121.90342; mkpointannotation *doughertyvalleypoint = [[mkpointannotation alloc] init]; doughertyvalleypoint.coordinate = doughertyvalley; doughertyvalleypoint.title = @"dougherty valley"; nsarray *points = [[nsarray alloc] initwithobjects: montevistapoint, sanramonvalleypoint, doughertyvalleypoint, nil]; [self.schoolmap addannotations:points]; } - (mkannotationview *)mapview:(mkmapview *)mapview viewforannotation:(id <mkannotation>)annotation { mkpinannotationview *annotationview = [[mkpinannotationview alloc] initwithannotation:annotation reuseidentifier:nil]; annotationview.enabled = yes; annotationview.canshowcallout = yes; nslog(@"pin created"); return annotationview; } - (void)mapview:(mkmapview *)mapview didselectannotationview:(mkannotationview *)view { mkpointannotation *testpoint = [[mkpointannotation alloc] init]; testpoint = view.annotation; self.testtext.text = testpoint.title; nslog(@"selected"); }
you need change viewforannotation:
method in order customize , implement opendetail: action method
- (mkannotationview *)mapview:(mkmapview *)mapview viewforannotation:(id <mkannotation>)annotation { if (annotation == mapview.userlocation) { return nil; } static nsstring *identifier = @"customannotation"; mkannotationview* annview = nil; if ([annotation iskindofclass:[customannotation class]]) { annview = (mkannotationview *) [mapview dequeuereusableannotationviewwithidentifier:identifier]; if (annview == nil) { annview = [[mkannotationview alloc] initwithannotation:annotation reuseidentifier:identifier]; } else { annview.annotation = annotation; } uiimage *image = nil; image = [uiimage imagenamed:@"pin2"]; uiimageview *imageview = [[uiimageview alloc] initwithimage:image]; [annview addsubview:imageview]; [annview setframe:cgrectmake(0, 0, imageview.frame.size.width, imageview.frame.size.height)]; // [annview setbackgroundcolor:[uicolor colorwithred:1 green:0 blue:0 alpha:0.1]]; uibutton*accessory = [uibutton buttonwithtype:uibuttontypedetaildisclosure]; [accessory settag:[(customannotation*)annotation tag]]; [accessory addtarget:self action:@selector(opendetail:) forcontrolevents:uicontroleventtouchupinside]; [accessory setframe:cgrectmake(0, 0, 30, 30)]; [annview setrightcalloutaccessoryview:accessory]; } [annview setenabled:yes]; [annview setcanshowcallout:yes]; return annview; }
Comments
Post a Comment