Deepak Khiwani
Deepak Khiwani

Reputation: 744

Correct Index of a pin when tapped on pin in map

I am developing a map application I wanted to get the index the annotation when tapped on the annotation. I am trying to achieve the same using :

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

        MKPointAnnotation *point = view.annotation;

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

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

but it always returns me a random number.

I am assuming that didSelectAnnotationView delegate works like didSelectRowAtIndexPath.

Correct me where I am making mistake.

Any help would be appreciable.

Thanks.

Upvotes: 0

Views: 1254

Answers (4)

Janisar
Janisar

Reputation: 31

NSUInteger index = [mapView.annotations indexOfObject:view.annotation];

Use this line of code so you can get your current index

Upvotes: 0

Rohit Pathak
Rohit Pathak

Reputation: 359

First when loading the pin you need to set the tag of each pin to identify them later.

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

Hope it will work for you.Let me know if you have any problem!!!

Upvotes: 0

Divyu
Divyu

Reputation: 1313

Use this code. mapView.annotations is the array you are looking for.

  - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
            Annotation *annot = view.annotation;
            NSInteger indexOfTheObject = [mapView.annotations indexOfObject:annot];
            NSLog(@"%d",indexOfTheObject); 
            NSLog(@"%@",point.title);
      }

Upvotes: 0

the1pawan
the1pawan

Reputation: 1204

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

Upvotes: 1

Related Questions