Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out pin id in map annotation view?

How can I find out the pin id (i.e. the details of pin title and subtitle) of pin that was clicked?

I have this to show pins annotations. This code is in view did load:

[resultCoordinate addObjectsFromArray:[sqlClass return_Coordinate]];

if ([resultCoordinate count])
{
    for (int i =0; i < [resultCoordinate count]; i++) 
    {
        dict  = [resultCoordinate objectAtIndex:i];

        MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
        region.center.latitude = [[dict objectForKey:@"Lati"] floatValue];
        region.center.longitude = [[dict objectForKey:@"Longi"] floatValue];
        region.span.longitudeDelta = 0.2f;
        region.span.latitudeDelta = 0.2f;

        NSString *fishName = [dict objectForKey:@"Fish"];
        fishName = [fishName stringByAppendingString:@"  "];

        NSString *fishWeight = [dict objectForKey:@"Weight"];
        fishWeight = [fishWeight stringByAppendingString:@" kg"];


        fishName = [fishName stringByAppendingString:fishWeight];

        MapAnnotation *ann = [[MapAnnotation alloc] init];

        ann.title = fishName;
        ann.subtitle = [dict objectForKey:@"DateTime"];

        ann.coordinate = region.center;
        [mapView addAnnotation:ann];
    }
}

and in delegate:

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinView = nil;

    static NSString *defaultPinID = @"com.invasivecode.pin";
    pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil )
    pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
    pinView.pinColor = MKPinAnnotationColorPurple;
    pinView.canShowCallout = YES;
    pinView.animatesDrop = YES;

    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [infoButton addTarget:self action:@selector(infoButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = infoButton;
    [defaultPinID release];

    return pinView;
}

and on click of info button:

-(void)infoButtonPressed:(id)sender
{
    recordCatch = [[RecordCatchDetails alloc] initWithNibName:@"RecordCatchDetails" bundle:nil];
    [self.view addSubview:recordCatch.view] ;
}

Where record catch is new class object.

like image 415
Sandeep Singh Avatar asked Oct 12 '22 02:10

Sandeep Singh


1 Answers

When annotation gets selected map view calls mapView:didSelectAnnotationView: method in its delegate, you can override this to get what you want.

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
 MKAnnotation *selectedAnnotation = view.annotation // This will give the annotation.
 if([selectedAnnotation.title isEqualToString:@"yourTitle"])
 {
  // Do something
 }
}

If you want to get the action for accessory control click make use of

-(void)mapView:(MKMapView *)mapView annotationView:(MKPinAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
// get annotation details here.
}
like image 191
visakh7 Avatar answered Oct 23 '22 03:10

visakh7