使用showdetails的多个Mapkit注释如何加载每个注释的详细信息?

时间:2011-03-09 17:13:24

标签: iphone mapkit

我在NSMuttableArray中使用多个注释使我的地图正常工作,MyAnnotations显示正常,标题和副标题以及右视图链接到带有动作@selector showDetails的视图。但是,如何将每个注释的详细信息加载到该视图中?即如果有人点击帝国大厦,它会在该视图中显示有关自身的信息,而有人点击克莱斯勒建筑,它也会在此视图中显示有关自身的详细信息。

我一直在寻找方法但是我似乎无法弄清楚这里是我的rootviewcontroller.m的片段

如果它有助于我的所有注释都被命名等等

[mapView addAnnotation:myAnnotation1];
[mapView addAnnotation:myAnnotation2];
[mapView addAnnotation:myAnnotation3];
[mapView addAnnotation:myAnnotation4];

与相应的

MyAnnotation* myAnnotation1=[[MyAnnotation alloc] init];

myAnnotation1.coordinate=theCoordinate1;
myAnnotation1.title=@"Asda";
myAnnotation1.subtitle=@"Kimberley Way, TN240SE";



- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    NSLog(@"welcome into the map view annotation");

    // if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    // try to dequeue an existing pin view first
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
                                     initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
    pinView.animatesDrop=YES;
    pinView.canShowCallout=YES;
    pinView.pinColor=MKPinAnnotationColorPurple;

    //differnt colored pins based on title name

    if ([[annotation title] isEqualToString:@"Asda"])

        pinView.pinColor = MKPinAnnotationColorRed;

    else if ([[annotation title] isEqualToString:@"Ashford Football Club"])

        pinView.pinColor = MKPinAnnotationColorGreen;

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    [rightButton addTarget:self
                    action:@selector(showDetails:)
          forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = rightButton;



    UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"profile.png"]];
    pinView.leftCalloutAccessoryView = profileIconView;
    [profileIconView release];

    return pinView;
}


-(IBAction)showDetails:(id)sender{

    NSLog(@"Annotation Click");
    self.userProfileVC.title=((UIButton*)sender).currentTitle;
    [self.navigationController pushViewController:self.userProfileVC animated:YES];
}
- (void)dealloc {
    [super dealloc];
}


@end

3 个答案:

答案 0 :(得分:1)

根据the documentation,点击按钮时,地图的代理人应收到消息mapView:annotationView:calloutAccessoryControlTapped:。这将为您提供已轻触的MKAnnotationView实例,该实例具有annotation属性,可为您提供相应的注释。您应该使用它而不是尝试直接在按钮上使用操作。

答案 1 :(得分:0)

你可以这样做。

CLLocationCoordinate2D location = [self addressLocation:address];
region.span=span;
region.center=location;
Annotation *addAnno= [[Annotation alloc] initWithCoordinate:location title:address subtitle:subaddres];
[annotations addObject:addAnno];

因此,在注释中,您有委托方法标题和副标题 - (NSSTring)标题{,使用此标题,您可以在单击相应的注释时获取标题和副标题。

<注释

中的

  -(id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString*)tit subtitle:(NSString*)sub{

 mTitle=tit;


mSubTitle=sub;

return self;

}

答案 2 :(得分:0)

我解决了我在我的detailsview类中声明了一个标签,称为userProfileVC,然后使用下面的if语句将信息文本传递给每个注释的正确标签。

-(IBAction)showDetails:(id)sender{

    NSLog(@"Annotation Click");
    self.userProfileVC.title=((UIButton*)sender).currentTitle;
    if([((UIButton*)sender).currentTitle isEqualToString:@"Asda"])
        self.userProfileVC.label.text= @"Glass, Paper, Cans, Textiles, Card, Shoes and Books";