erdemgc
erdemgc

Reputation: 1721

When a cell is tapped, an annotation should be added

I need to add an annotation to a map view when it is touched the any cell of the tableview. I have a method where I add the annotation viewForAnnotation and the method didSelectRowAtIndexPath.

I could not make the logical connection that is where to call the method and how?

Could you please help me?

viewForAnnotation method

- (MKAnnotationView *)mapView:(MKMapView *)mapView2 viewForAnnotation:(id  <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
    return nil;
}
else if ([annotation isKindOfClass:[CustomAnnotation class]])
{
    static NSString * const identifier = @"MyCustomAnnotation";

    MKAnnotationView* annotationView = [mapView2 dequeueReusableAnnotationViewWithIdentifier:identifier];

    if (annotationView)
    {
        annotationView.annotation = annotation;
    }
    else
    {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                      reuseIdentifier:identifier];
    }

    UIImageView * ecz = [[UIImageView alloc]init];

    ecz.frame = CGRectMake(0, 0, 65, 49);

    ecz.image = [UIImage imageNamed:@"indicator.png"];


    UIImageView * vstd = [[UIImageView alloc]init];

    vstd.frame = CGRectMake(33, 10, 24, 22);

    vstd.image = [UIImage imageNamed:@"indicator_ziyaret_gri"];


    UIImageView * nbox = [[UIImageView alloc]init];

    nbox.frame = CGRectMake(48, -6, 22, 22);

    nbox.image = [UIImage imageNamed:@"numara_kutusu"];

    [annotationView addSubview:ecz];

    [annotationView addSubview:vstd];


    UILabel *index = [[UILabel alloc] initWithFrame:CGRectMake(5,4,15,15)];
    index.text = @"1";
    index.textColor = [UIColor whiteColor];
    [index setFont:[UIFont fontWithName:@"Arial-BoldMT" size:18]];
    [nbox addSubview:index];
    [index setBackgroundColor:[UIColor clearColor]];

    [annotationView addSubview:nbox];

    annotationView.canShowCallout = YES;

    return annotationView;
}

return nil;
}

didSelectRowAtIndexPath method

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
EczaneCell *cell = [tableView cellForRowAtIndexPath:indexPath];

 }

Upvotes: 0

Views: 87

Answers (1)

iphonic
iphonic

Reputation: 12717

mapView: viewForAnnotation: method is a delegate method of MapView, this method is called by the MapView internally when you ADD or, REMOVE any annotation from the mapview.

To add an annotation on TableView tap you should do the following in tableView:didSelectRowAtIndexPath:

[self.map addAnnotaion:<YOUR Annotation>];

Upvotes: 2

Related Questions