如何在Mapkit中获取注释索引? [英] How to get Annotation index in Mapkit?

查看:76
本文介绍了如何在Mapkit中获取注释索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to get Annotation index in Mapkit?

我到目前为止尝试的是...

i try so far is...

ViewDidLoad中像这样...

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

在这样的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);
    }

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

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

请帮助我

推荐答案

使用此

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

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

这篇关于如何在Mapkit中获取注释索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆