Thursday, 15 September 2011

ios - Detect which specific map pin was touched -



ios - Detect which specific map pin was touched -

i have map view big number of pins, each having unique annotation. map shows pulsing bluish dot user location. far, i've been able determine if pin touched, not specific pin touched.

how determine specific pin in map touched user? i'm using xcode v6.1. sample code (for 1 of many pins):

- (void)viewdidload { [super viewdidload]; //** info location 1 ** mkcoordinateregion part = { {0.0, 0.0 }, { 0.0, 0.0 } }; region.center.latitude = 45.5262223; region.center.longitude = -122.63642379999999; region.span.longitudedelta = 0.01f; region.span.latitudedelta = 0.01f; [mapview setregion:region animated:yes]; [self.mapview setdelegate:self]; displaymap *ann = [[displaymap alloc] init]; ann.title = @"this location 1"; ann.subtitle = @"1234 north main street"; ann.coordinate = region.center; [self.mapview addannotation:ann]; - (void)mapview:(mkmapview *)mapview didselectannotationview:(mkannotationview *)view { nslog(@"this logs when pin touched, need know pin"); }

in didselectannotationview, view parameter passed method contains reference annotation it's associated with.

before using reference, should check type (eg. using iskindofclass) , handle accordingly. because delegate method called when annotation tapped includes user location bluish dot of type mkuserlocation. it's possible custom annotation objects have non-standard properties , attempting access these on wrong type of annotation cause exception.

example:

- (void)mapview:(mkmapview *)mapview didselectannotationview:(mkannotationview *)view { //annotation selected in view parameter... id<mkannotation> annselected = view.annotation; //see if annotation our custom type (displaymap) //and not else mkuserlocation... if ([annselected iskindofclass:[displaymap class]]) { //now know annselected of type displaymap //so it's safe cast type displaymap... displaymap *dm = (displaymap *)annselected; nslog(@"pin touched: title=%@", dm.title); } }

ios mkmapview mkannotation

No comments:

Post a Comment