如何识别在MKMapView中选择的标注?

时间:2011-07-25 16:01:51

标签: iphone objective-c mkmapview

我有一个地图视图,我在其上显示了许多Pin注释。我也在这些引脚上使用了标注,所以现在当有人点击这些引脚时,会出现一个标注,其中包含有关该地点的详细信息,现在我想识别标注并推送另一个特定于该视图的视图。 这是代码:

-(void)configureView:(NSDictionary *)serverResult {

    // Get the set of projects from the project list
    [spinner stopAnimating];
    NSSet * projects = [[serverResult valueForKey:@"Map"] valueForKey:@"projects"];
    NSLog(@"Map got %d projects", projects.count);

    mapView.showsUserLocation = YES;
    mapView.delegate = self;

    MKCoordinateRegion newRegion;
    MKCoordinateSpan span;
    span.latitudeDelta=130.0;
    span.longitudeDelta=130.0;
    collection = [[NSMutableArray alloc]init];
    // Iterate over the set of projects
    for (NSManagedObject *project in projects) {

        // Get the set of locations for the current project
        NSSet *locations = [project valueForKey:@"locations"];
        int i =0;
        for (NSManagedObject *location in locations) {
            NSString * projectId = [projects valueForKey:@"id"];
            NSString * lat = [location valueForKey:@"latitude"];
            double lati = [lat doubleValue]; 
            NSString * lang = [location valueForKey:@"longitude"];
            double longi = [lang doubleValue];
            CLLocationCoordinate2D annotation = mapView.userLocation.coordinate; 
            annotation.latitude= lati;
            annotation.longitude= longi;
            newRegion.span=span;
            newRegion.center=annotation;
            geoCoder=[[MKReverseGeocoder alloc] initWithCoordinate:annotation];
            geoCoder.delegate=self;
            [geoCoder start];
            [self.mapView setRegion:newRegion animated:YES];
            i= i +1;
        }
    }
    [self.view addSubview:mapView];
    [self.view addSubview:segments];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{
    NSLog(@"Reverse Geocoder Errored");

}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{
    NSLog(@"Reverse Geocoder completed");
    mPlacemark=placemark;
    [mapView addAnnotation:placemark];
}

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    annView.animatesDrop=TRUE;
    annView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    annView.canShowCallout = YES;
    annView.enabled = YES;
    return annView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    AllDetailDashboard *summary = [[AllDetailDashboard alloc] initWithNibName:@"View Controller" bundle:nil];
    //Want to write some code here to recognize which project is clicked, in AllDetailDashBoard class we have a variable called project id which is used to recognize the project but i am not able to fetch the project id for specific project which is clicked here
    [self.navigationController pushViewController:summary animated:YES];
    [summary release];
}

我是否需要使用任何其他委托方法来识别标注。 现在点击标注我想将projectId发送到AllDetailDashBoard。

谢谢,

3 个答案:

答案 0 :(得分:2)

是的,这很容易..

这可能会有所帮助...

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

MyObject *objectTemp = (MyObject *) view.annotation;
//Do stuff here with that object such as

AllDetailDashboard *summary = [[AllDetailDashboard alloc] initWithNibName:@"View Controller" bundle:nil];
summary.projectID = objectTemp.projectID;

}

答案 1 :(得分:1)

MKAnnotationView *是你的标注。

您可以使您的数据对象实现MKAnnotation协议(返回坐标,标题和副标题),然后在calloutAccessoryControlTapped中,将注释转换为您的数据对象(包含您的项目ID)。

或者,您可以使用与项目ID相关的数字标记每个注释视图。

答案 2 :(得分:0)

选择注释时会显示标注。实现这些委托方法以获取被轻击的annotationView:

– mapView:didSelectAnnotationView:
– mapView:didDeselectAnnotationView:

顺便说一下,我没有看到任何添加注释的代码。在注释类中添加属性“projectID”并在初始化ur注释时对其进行初始化。这样你就可以访问这个属性:

if([annotationView.annotation isKindOfClass:[<your annotation classname> class]]) {
      <your annotation classname> *ann = (<your annotation classname>*)annotationView.annotation;
      NSString *projectID = [NSString stringWithString:ann.projectID];
}