How to get Annotation index in Mapkit?

半城伤御伤魂 提交于 2019-12-12 03:37:14

问题


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


回答1:


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.




回答2:


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



回答3:


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

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

}


来源:https://stackoverflow.com/questions/38156800/how-to-get-annotation-index-in-mapkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!