ios - Get Directions Button on Map Annotation Detail View -
i have map view several annotations. title of annotation name of business , subtitle address. there detaildisclosure button on annotation view takes user detail view selected annotation. detail view has uilabel text annotation title, uilabel text annotation subtitle (the address), button business website, button business phone number, , lastly, button getting directions address. i'm wanting take address annotation subtitle when user taps directions button, opens in apple maps app can directions. question is, how open maps app address in annotation subtitle?
- (mkannotationview*)mapview:(mkmapview *)mapview viewforannotation:(id<mkannotation>)annotation { // if it's user location, return nil if ([annotation iskindofclass:[mkuserlocation class]]) return nil; // try dequeue existing pin view first static nsstring *annotationidentifier = @"annotationidentifier"; mkpinannotationview *pinview = [[mkpinannotationview alloc]initwithannotation:annotation reuseidentifier:annotationidentifier]; pinview.animatesdrop = yes; pinview.canshowcallout = yes; uibutton *rightbutton = [uibutton buttonwithtype:uibuttontypedetaildisclosure]; [rightbutton settitle:annotation.title forstate:uicontrolstatenormal]; // [rightbutton addtarget:self action:@selector(showdetails:) forcontrolevents:uicontroleventtouchupinside]; pinview.rightcalloutaccessoryview = rightbutton; return pinview; } - (void)mapview:(mkmapview *)mapview annotationview:(mkannotationview *)view calloutaccessorycontroltapped:(uicontrol *)control { annotationdetailview *detail = [[annotationdetailview alloc] initwithnibname:nil bundle:nil]; id<mkannotation> ann = [mapview.selectedannotations objectatindex:0]; ann = view.annotation; detail.modaltransitionstyle = uimodaltransitionstylefliphorizontal; nslog(@"%@", ann.title); nslog(@"%@", ann.subtitle); detail.anntitle = ann.title; detail.annsub = ann.subtitle; [self.navigationcontroller pushviewcontroller:detail animated:yes]; } viewdidload annotationdetailview
- (void)viewdidload { [super viewdidload]; // additional setup after loading view nib. [ti settext:anntitle]; [sub settext:annsub]; } here map view annotations.
// // rsfm.m #import "rsfm.h" #import "annotationdetailview.h" @interface rsfm () @end @implementation rsfm { } @synthesize centercoordinate, coordinate, title, subtitle, marketannotation, location; - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { self.title = nslocalizedstring(@"farm markets", @"farm markets"); // create location manager object locationmanager = [[cllocationmanager alloc]init]; [locationmanager setdelegate:self]; // , want accurate possible regardless of how time/power takes [locationmanager setdesiredaccuracy:kcllocationaccuracythreekilometers]; // tell our manager start looking location // [locationmanager startupdatinglocation]; } return self; } /* - (void)setcentercoordinate:(cllocationcoordinate2d)centercoordinate { centercoordinate = cllocationcoordinate2dmake(37.7885, 85.3279); } */ - (void)findlocation { [locationmanager startupdatinglocation]; [activityindicator startanimating]; // [locationtitlefield sethidden:yes]; [locationmanager stopupdatinglocation]; } - (void)foundlocation:(cllocation *)loc { cllocationcoordinate2d coord = [loc coordinate]; // zoom region location mkcoordinateregion region = mkcoordinateregionmakewithdistance(coord, 700000, 700000); [worldview setregion:region animated:yes]; // reset ui // [locationtitlefield settext:@""]; [activityindicator stopanimating]; // [locationtitlefield sethidden:no]; [locationmanager stopupdatinglocation]; } - (void)locationmanager:(cllocationmanager *)manager didupdatetolocation:(cllocation *)newlocation fromlocation:(cllocation *)oldlocation { nslog(@"%@", newlocation); // how many seconds ago new location created? nstimeinterval t = [[newlocation timestamp]timeintervalsincenow]; // cllocationmanagers return last found location of device first, don't want data in case. // if location made more 3 minutes ago, ignore it. if (t < -180) { // cached data, don't want it, keep looking return; } [self foundlocation:newlocation]; } - (void)locationmanager:(cllocationmanager *)manager didfailwitherror:(nserror *)error { nslog(@"could not find location: %@", error); } - (void)dealloc { // tell location manager stop sending messages [locationmanager setdelegate:nil]; } /* - (ibaction)showdetails:(id)sender { annotationdetailview *detail = [[annotationdetailview alloc] initwithnibname:nil bundle:nil]; detail.modaltransitionstyle = uimodaltransitionstylefliphorizontal; nslog(@"%@", marketannotation.title); detail.ti.text = marketannotation.title; [self.navigationcontroller pushviewcontroller:detail animated:yes]; } */ - (mkannotationview*)mapview:(mkmapview *)mapview viewforannotation:(id<mkannotation>)annotation { // if it's user location, return nil if ([annotation iskindofclass:[mkuserlocation class]]) return nil; // try dequeue existing pin view first static nsstring *annotationidentifier = @"annotationidentifier"; mkpinannotationview *pinview = [[mkpinannotationview alloc]initwithannotation:annotation reuseidentifier:annotationidentifier]; pinview.animatesdrop = no; pinview.pincolor = mkpinannotationcolorred; pinview.canshowcallout = yes; uibutton *rightbutton = [uibutton buttonwithtype:uibuttontypedetaildisclosure]; [rightbutton settitle:annotation.title forstate:uicontrolstatenormal]; // [rightbutton addtarget:self action:@selector(showdetails:) forcontrolevents:uicontroleventtouchupinside]; pinview.rightcalloutaccessoryview = rightbutton; return pinview; } - (void)mapview:(mkmapview *)mapview annotationview:(mkannotationview *)view calloutaccessorycontroltapped:(uicontrol *)control { annotationdetailview *detail = [[annotationdetailview alloc] initwithnibname:nil bundle:nil]; id<mkannotation> ann = [mapview.selectedannotations objectatindex:0]; // ann = [mapview.selectedannotations objectatindex:0]; ann = view.annotation; detail.modaltransitionstyle = uimodaltransitionstylefliphorizontal; nslog(@"%@", ann.title); nslog(@"%@", ann.subtitle); detail.anntitle = ann.title; detail.annsub = ann.subtitle; // detail.anncoord = ann.coordinate; [self.navigationcontroller pushviewcontroller:detail animated:yes]; /* id<mkannotation> ann = [mapview.selectedannotations objectatindex:0]; cllocationcoordinate2d coord = ann.coordinate; mkplacemark *placemark = [[mkplacemark alloc]initwithcoordinate:coord addressdictionary:nil]; mkmapitem *mapitem = [[mkmapitem alloc]initwithplacemark:placemark]; [mapitem openinmapswithlaunchoptions:nil]; */ } - (void)viewdidload { [locationmanager startupdatinglocation]; [worldview setshowsuserlocation:yes]; [locationmanager stopupdatinglocation]; nsmutablearray *marketlocations = [[nsmutablearray alloc]init]; nsmutablearray *lat = [[nsmutablearray alloc]initwithobjects:@"37.7867266", @"37.0703517", @"37.1610806", @"37.318367", @"37.3559204", @"37.4154066", @"37.4757622", @"37.7450252", @"37.6318978", @"37.0716803", nil]; nsmutablearray *lon = [[nsmutablearray alloc]initwithobjects:@"-87.608209", @"-88.1237899", @"-87.9148629", @"-87.5074402", @"-87.5448032", @"-87.8003148", @"-87.9515986", @"-87.9061638", @"-87.1148574", @"-87.3008418", nil]; nsmutablearray *title1 = [[nsmutablearray alloc]initwithobjects:@"cates farm", @"broadbent b & b foods", @"cayce's pumpkin patch", @"metcalfe landscaping", @"brumfield farm market", @"dogwood valley farm", @"country fresh meats & farmers market", @"jim david meats", @"trunnell's farm market", @"lovell's orchard & farm market", nil]; nsmutablearray *subtitle1 = [[nsmutablearray alloc]initwithobjects:@"hwy 425 henderson, ky 42420", @"257 mary blue road kuttawa, ky 42055", @"153 farmersville road princeton, ky 42445", @"410 princeton road madisonville, ky 42431", @"3320 nebo road madisonville, ky 42431", @"4551 state route 109n clay, ky 42404", @"9355 hwy 60 w sturgis, ky 42459",@"350 t. frank wathen rd. uniontown, ky 42461", @"9255 hwy 431 utica, ky 42376", @"22850 coal creek road hopkinsville, ky 42240", nil]; // cllocationcoordinate2d location; // mkpointannotation *marketannotation; (int x = 0; x < [lat count]; x++) { marketannotation = [[mkpointannotation alloc]init]; location.latitude = [[lat objectatindex:x]floatvalue]; location.longitude = [[lon objectatindex:x]floatvalue]; marketannotation.coordinate = location; marketannotation.title = [title1 objectatindex:x]; marketannotation.subtitle = [subtitle1 objectatindex:x]; [marketlocations addobject:marketannotation]; } [worldview addannotations:marketlocations]; /* mkpointannotation *point = [[mkpointannotation alloc] init]; point.coordinate = cllocationcoordinate2dmake(37.7867266, -87.608209); point.title = @"cates farm"; point.subtitle = @"hwy 425 henderson, ky 42420"; [worldview addannotation:point]; */ } - (void)mapview:(mkmapview *)mapview didupdateuserlocation:(mkuserlocation *)userlocation { cllocationcoordinate2d loc = [userlocation coordinate]; mkcoordinateregion region = mkcoordinateregionmakewithdistance(loc, 700000, 700000); [worldview setregion:region animated:yes]; [locationmanager stopupdatinglocation]; locationmanager.delegate = nil; } - (ibaction)selectsegmentcontrol { int segmenttouched = [mapvarieties selectedsegmentindex]; nsstring *segmentname = [mapvarieties titleforsegmentatindex:segmenttouched]; if ([segmentname isequaltostring:@"street"]) { [worldview setmaptype:mkmaptypestandard]; } if ([segmentname isequaltostring:@"satellite"]) { [worldview setmaptype:mkmaptypesatellite]; } if ([segmentname isequaltostring:@"hybrid"]) { [worldview setmaptype:mkmaptypehybrid]; } } @end
you need use mkmapitem -openinmapswithlaunchoptions: documented here. need first create mkplacemark, if have required coordinate can create placemark directly , if have address you'll need use clgeocoder.
Comments
Post a Comment