subba raj subba raj - 5 months ago 28
iOS Question

How to get Annotation index in Mapkit?

How to get Annotation index in

Mapkit
?

i try so far is...

In
ViewDidLoad
like this...

[self.mapView addAnnotations:[self createAnnotations]];


In CreateAnnotations like this....

- (NSMutableArray *)createAnnotations
{
NSMutableArray *annotations = [[NSMutableArray alloc] init];

//Read locations details from plist
NSString * path = [[NSBundle mainBundle] pathForResource:@"locations" ofType:@"plist"];
NSArray * locations = [NSArray arrayWithContentsOfFile:path];
NSDictionary * imageDic;
customTickLocations = [NSMutableArray new];
for (NSDictionary *row in locations) {
NSNumber *latitude = [row objectForKey:@"latitude"];
NSNumber *longitude = [row objectForKey:@"longitude"];
NSString *title = [row objectForKey:@"title"];
NSString * image=[row objectForKey:@"image"];
imageDic =[row objectForKey:image];
[customTickLocations addObject:[row objectForKey:@"image"]];

//Create coordinates from the latitude and longitude values
CLLocationCoordinate2D coord;
coord.latitude = latitude.doubleValue;
coord.longitude = longitude.doubleValue;

MapViewAnnotation * annotation =[[MapViewAnnotation alloc] initWithTitle:image initWithSubTitle:title AndCoordinate:coord];

[annotations addObject:annotation];

}
NSLog(@"%@",customTickLocations);
return annotations;
}

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
MapViewAnnotation * annos = view.annotation;


NSInteger indexOfTheObject = [mapView.annotations indexOfObject: annos];

NSString * image =[customTickLocations objectAtIndex:indexOfTheObject];
NSLog(@"------ %ld ------",(long)indexOfTheObject);
}


i am getting index , The Problem is index value is changing for same Pin

Please help me

Answer

use this

 NSUInteger index = [mapView.annotations indexOfObject:view.annotation];
  NSLog(@"index no %d",index);

you get the index

 NSInteger i = 0;
 for (NSDictionary *row in locations) {
        NSNumber *latitude = [row objectForKey:@"latitude"];
        NSNumber *longitude = [row objectForKey:@"longitude"];
        NSString *title = [row objectForKey:@"title"];
        NSString * image=[row objectForKey:@"image"];
        imageDic =[row objectForKey:image];
        [customTickLocations addObject:[row objectForKey:@"image"]];

        //Create coordinates from the latitude and longitude values
        CLLocationCoordinate2D coord;
        coord.latitude = latitude.doubleValue;
        coord.longitude = longitude.doubleValue;

        MapViewAnnotation * annotation =[[MapViewAnnotation alloc] initWithTitle:image initWithSubTitle:title AndCoordinate:coord Withindex:i];

 i++;
               [annotations addObject:annotation];

    }

plz add one more property in MapViewAnnotation for index then try.