Reputation: 37
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
Upvotes: 0
Views: 783
Reputation: 4474
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
NSInteger indexOfTheObject = [mapView.annotations indexOfObject:view.annotation];
}
Upvotes: 0
Reputation: 2786
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.
Upvotes: 1
Reputation: 1960
Try like this;
Annotation is your custom class that holds information about the annotation
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
if ([view.annotation isKindOfClass:[Annotation class]])
{
Annotation *annos = view.annotation;
NSInteger index = [self.arrayOfAnnotations indexOfObject:annos];
NSString * image =[customTickLocations objectAtIndex:indexOfTheObject];
NSLog(@"------ %ld ------",(long)indexOfTheObject);
}
}
Upvotes: 2