0

我正在开发一个地图应用程序,我想在点击注释时获取注释的索引。我正在尝试使用:

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

        MKPointAnnotation *point = view.annotation;

        NSLog(@"%@",point.title);

        NSLog(@"%d",indexOfTheObject);
}

但它总是给我一个随机数。

我假设didSelectAnnotationView委托像didSelectRowAtIndexPath.

纠正我犯错的地方。

任何帮助都是不言而喻的。

谢谢。

4

4 回答 4

1
 - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
    // Annotation is your custom class that holds information about the annotation
    if ([view.annotation isKindOfClass:[Annotation class]]) {
        Annotation *annot = view.annotation;
        NSInteger index = [self.arrayOfAnnotations indexOfObject:annot];
    }
  }
于 2013-06-24T06:30:48.080 回答
0
NSUInteger index = [mapView.annotations indexOfObject:view.annotation];

使用这行代码,您可以获得当前索引

于 2015-04-13T08:15:07.663 回答
0

使用此代码。mapView.annotations是您要查找的数组。

  - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
            Annotation *annot = view.annotation;
            NSInteger indexOfTheObject = [mapView.annotations indexOfObject:annot];
            NSLog(@"%d",indexOfTheObject); 
            NSLog(@"%@",point.title);
      }
于 2013-06-24T06:33:49.260 回答
0

首先,在加载引脚时,您需要设置每个引脚的标签以便稍后识别它们。

       int count=0;
            for(NSDictionary *dict in self.mapArray)
            {
                CLLocationCoordinate2D coord= CLLocationCoordinate2DMake([[dict objectForKey:@"latitude"] doubleValue], [[dict objectForKey:@"longitude"] doubleValue]);
                [self loadAnnotationWithLocation:coord tag:count];
         count++;
            }

    #pragma mark-map delegates
    -(void)loadAnnotationWithLocation:(CLLocationCoordinate2D)centerLocation tag:(NSInteger)annotationTag
    {
        MyAnnotation *ann = [[MyAnnotation alloc] initWithCoordinate:centerLocation];
        ann.ann_tag = annotationTag;
        [self.map addAnnotation:ann];
        [ann release];
        ann=nil;
    }

    -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
    {
        if (view.annotation)
        {
            MyAnnotation *myAnnotation  = (MyAnnotation *)view.annotation;
            if((id <MKAnnotation>)myAnnotation !=self.map.userLocation)
            {
              NSDictionary *dict=[self.mapArray objectAtIndex:myAnnotation.ann_tag];
//do rest of the work here with different pin respect to their tag
            }
        }
    }

希望它对您有用。如果您有任何问题,请告诉我!!!

于 2013-06-24T07:23:01.210 回答