如何获得Mapkit的注释索引?

如何获得Mapkit注释索引?

我尝试到目前为止…

ViewDidLoad这样…

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

在CreateAnnotations这样的….

 - (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); } 

我正在索引,问题是索引值正在改变相同的引脚

请帮帮我

用这个

  NSUInteger index = [mapView.annotations indexOfObject:view.annotation]; NSLog(@"index no %d",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在MapViewAnnotation中添加一个属性以获取索引,然后尝试。

像这样尝试;

注释是您的自定义类,它包含关于注释的信息

 - (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); } } 
 - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ NSInteger indexOfTheObject = [mapView.annotations indexOfObject:view.annotation]; }