【问题标题】:How can i recognize the callout which is been selected in MKMapView?我如何识别在 MKMapView 中选择的标注?
【发布时间】:2011-07-26 16:09:04
【问题描述】:

我有一个地图视图,上面显示了一些 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。

谢谢,

【问题讨论】:

标签: iphone objective-c mkmapview


【解决方案1】:

是的,这很容易..

这可能有点帮助......

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

}

【讨论】:

  • 我建议将 MKAnnotation 子类化,然后您可以将其用作“MyObject”或您在那里定义的任何内容
  • MKAnnotation 不能被子类化,因为它是一个协议。 “MyObject”就是任何遵守 MKAnnotation 协议的对象。
【解决方案2】:

MKAnnotationView * 是您的标注。

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

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

【讨论】:

  • 让我包含我正在循环服务器结果的代码的另一部分,请参阅我的编辑
【解决方案3】:

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

– mapView:didSelectAnnotationView:
– mapView:didDeselectAnnotationView:

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

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

【讨论】:

  • 您需要子类化 mkannotation。没有用于注释的内置类。您如何将注释添加到地图中???
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-18
相关资源
最近更新 更多