如何在Mapkit中获取注释索引?

时间:2016-07-02 05:26:43

标签: ios objective-c annotations 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);
    }

我正在获取索引,问题是索引值正在针对相同的Pin更改

请帮帮我

3 个答案:

答案 0 :(得分:2)

试试这个;

注释是包含注释信息的自定义类

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

答案 1 :(得分:1)

使用此

 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中添加一个属性作为索引,然后尝试。

答案 2 :(得分:0)

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{

    NSInteger indexOfTheObject = [mapView.annotations indexOfObject:view.annotation];

}