【问题标题】:How to get Annotation index in Mapkit?如何在 Mapkit 中获取注释索引?
【发布时间】:2016-07-02 05:26:43
【问题描述】:

如何获取Mapkit中的Annotation索引?

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

像这样在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 indexOfTheObject = [mapView.annotations indexOfObject: view.annotation];
  • @BhavinRamani 谢谢。但问题是一样的
  • 但是它还包括用户当前位置?

标签: ios objective-c annotations mapkit


【解决方案1】:

这样试试;

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

【讨论】:

  • 谢谢。我得到正确的索引值,但问题是随机更改索引..
【解决方案2】:

使用这个

 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];

    }

请在 MapViewAnnotation 中为索引再添加一个属性,然后尝试。

【讨论】:

  • 你能展示你是如何使用的吗 -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation
  • 谢谢。我得到了正确的索引值,但问题是随机更改索引。
  • 我正在使用像 MapViewAnnotation 这样的单独类..-(id) initWithTitle:(NSString *) title initWithSubTitle:(NSString *) imgTitle AndCoordinate:(CLLocationCoordinate2D)coordinate { self = [super init ]; _title = 标题; _coordinate = 坐标; _imgTitle = imgTitle;回归自我; }
  • 我正在获取索引......但索引正在改变运行应用程序的时间
  • 坐标是静态的
【解决方案3】:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-20
    • 2013-04-06
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    相关资源
    最近更新 更多