Search code examples
iosmapkitmkannotationview

How to get the map annotations property in another class?


I'm using the mapKit. I have created custom annotations with title, subtitle and an ID. This is method that creates the annotation:

- (instancetype) initWithCoordinates:(CLLocationCoordinate2D)paramCoordinates title: (NSString *)paramTitle subTitle:(NSString *)paramSubTitle ID:(NSString*)ID
{
self = [super init];
if (self != nil)
     {
    _coordinate = paramCoordinates; _title = paramTitle;
    _subtitle = paramSubTitle;
    _ID = ID;
     }
return self;
} 

Now i use this to create annotations in another class:

-(void)createCityAnnotation
{
CLLocationCoordinate2D location1 =
CLLocationCoordinate2DMake(35.6961, 51.4231);
mapAnnotation *annotation1 =
[[mapAnnotation alloc] initWithCoordinates:location1
                                     title:@"City 1"
                                  subTitle:@"XXX"
                                        ID:@"1"];
[self.myMapView addAnnotation:annotation1];

CLLocationCoordinate2D location2 =
CLLocationCoordinate2DMake(32.6333, 51.6500);
mapAnnotation *annotation2 =
[[mapAnnotation alloc] initWithCoordinates:location2
                                     title:@"City2"
                                  subTitle:@"YYY"
                                        ID:@"2"];
[self.myMapView addAnnotation:annotation2];

CLLocationCoordinate2D location3 =
CLLocationCoordinate2DMake(38.0667, 46.3000);
mapAnnotation *annotation3 =
[[mapAnnotation alloc] initWithCoordinates:location3
                                     title:@"City3"
                                  subTitle:@"WWW"
                                        ID:@"3"];
[self.myMapView addAnnotation:annotation3];
}

Now upon clicking on the annotations i want to be able to get the annotations ID in the calloutAccessoryControlTapped method. I dont know exactly how to do so.It would be something like this:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    ////Use the Annotations ID here.
    [table reloadData];
}

Solution

  • Try This

         - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
            {
               mapAnnotation *annotaion= view.annotation;//this annotationWill be ur annotation.then u can get ID by annotation.ID.
    NSLog(@"%@",annotation.ID);
            }