0

trying to pass my annotation's data (name, address, phone #, url, description, etc) to a DetailViewController with a table. stuck----please read the code. the data isnt passed with calloutAccessoryTapped. Help?

    - (IBAction)gasButton:(id)sender {


    [self.mapView removeAnnotations:self.mapView.annotations];
    self.localSearchRequest = [[MKLocalSearchRequest alloc] init];
    self.localSearchRequest.region = self.mapView.region;
    self.localSearchRequest.naturalLanguageQuery = @"gas station";

    self.localSearch = [[MKLocalSearch alloc] initWithRequest:self.localSearchRequest];
    [self.localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {

        if(error){

            NSLog(@"localSearch startWithCompletionHandlerFailed!  Error: %@", error);
            return;

        } else {


            for(mapItem in response.mapItems){
                MKPointAnnotation *zip = [[MKPointAnnotation alloc] init ];
                zip.coordinate = mapItem.placemark.location.coordinate;
                zip.title = mapItem.name;


                 self.mapView.delegate = self;
                [self.mapView addAnnotation: zip];
                [self.mapView selectAnnotation:zip animated:YES];
                [self.mapView setUserTrackingMode:MKUserTrackingModeFollow];


                NSLog(@"%@ - 1", mapItem.name);

                CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:zip.coordinate.latitude longitude:zip.coordinate.longitude];
                CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude];



                CLLocationDistance distance = [loc1 distanceFromLocation:loc2]; 
                NSString *dist =  [[NSString alloc] initWithFormat:@"%.2f miles", distance * 0.000621371192];
                zip.subtitle = dist;

                           }

                  }

    }];
}


    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{

    MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MYVC"];

       if ([annotation isKindOfClass:[MKUserLocation class]])
       {

        return nil;

       }

       else if ([annotation isKindOfClass: [MKPointAnnotation class] ])
       {


           annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
           annotationView.enabled = YES;
           annotationView.animatesDrop = YES;
           annotationView.pinColor = MKPinAnnotationColorGreen;
           annotationView.canShowCallout = YES;



           NSLog(@"%@ - 2", mapItem.name);


           return annotationView;
       }


    return nil;

    }




    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
    {



  NSLog(@"%@ - tapped", mapItem.name);

     //STUCK HERE - mapItem.name is (null)

    }
Adrian P
  • 6,479
  • 4
  • 38
  • 55

1 Answers1

0

mapItem exists in the for loop of your gasButton function, no where else as far as I can see. You've created an annotation (zip) and added it to your map. In viewForAnnotation you are given that annotation as a parameter and asked to make an annotationView from it. In calloutAccessoryControlTapped you are given an annotationView and told it has been tapped. You need to follow the chain back to your data. From the annotationView get the annotation and from the annotation get your name.

MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"%@ - tapped", view.annotation.title);
}
Craig
  • 8,093
  • 8
  • 42
  • 74
  • getting "Property "title" not found on object of type MKPinAnnotationView" on the annotationView.title = annotation.title; line. – collegecoder Feb 25 '13 at 22:51
  • Sorry, I think you're supposed to set the title of the annotation only so you can take that line out, I'll adjust my answer. You may also need to get your own MKAnnotation class since MKPointAnnotation doesn't have a `title` attribute. A quick way to do that is to use the code in the answer here: http://stackoverflow.com/questions/6495419/mkannotation-simple-example – Craig Feb 25 '13 at 23:40