0

试图将我的注释数据(姓名、地址、电话号码、网址、描述等)传递给带有表格的 DetailViewController。卡住----请阅读代码。数据未通过 calloutAccessoryTapped 传递。帮助?

    - (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)

    }
4

1 回答 1

0

mapItem存在于gasButton函数的 for 循环中,据我所知,没有其他地方。您已创建注释 ( zip) 并将其添加到地图中。在viewForAnnotation您获得该注释作为参数并要求从中创建annotationView。在calloutAccessoryControlTapped你得到一个 annotationView 并告诉它已被点击。您需要按照链条返回您的数据。从 annotationView 获取注释并从注释获取您的名字。

MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"%@ - tapped", view.annotation.title);
}
于 2013-02-25T22:12:51.007 回答