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

发布于 2024-10-21 05:26:04 字数 2535 浏览 4 评论 0原文

我的地图可以很好地使用 NSMutableArray 中的多个注释,MyAnnotations 可以很好地显示标题和副标题,以及通过操作 @selector showDetails 链接到视图的 rightcallout 按钮。但是,如何将每个注释的详细信息加载到该视图中?即,如果有人单击帝国大厦,它会在该视图中显示有关其自身的信息,而有人单击克莱斯勒大厦,它也会在此视图中显示有关其自身的详细信息。

我一段时间以来一直在寻找方法,但我似乎无法弄清楚这是我的 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

I got my map working fine with multiple annotations in an NSMuttableArray, MyAnnotations display fine with a title and subtitle and a rightcalloutbutton linked to a view with action @selector showDetails. However how do I load details for each annotation into that view? i.e if someone clicks empire state building it displays info about itself into that view, and someone clicks Chrysler building it also displays details about itself in this view.

I have been searching for ways for sometime however i cant seem to figure it out here is a snippet of my rootviewcontroller.m

if it helps all my annotations are named so on and so forth

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

with corresponding

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╭ゆ眷念 2024-10-28 05:26:04

根据 文档,当点击按钮时,您的地图委托应该收到消息 mapView:annotationView:calloutAccessoryControlTapped:。这将为您提供被点击的 MKAnnotationView 实例,该实例具有一个 annotation 属性来为您提供相应的注释。您应该利用它,而不是尝试直接在按钮上使用操作。

According to the documentation, your map's delegate should receive the message mapView:annotationView:calloutAccessoryControlTapped: when the button is tapped. This hands you the MKAnnotationView instance that was tapped, which has an annotation property to give you the corresponding annotation. You should make use of that instead of trying to use an action on the button directly.

梨涡 2024-10-28 05:26:04

你可以这样做。

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

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

在注释中

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

 mTitle=tit;


mSubTitle=sub;

return self;

}

You could do lik this.

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

Therefore in Annotation you had delegate method title and subtitle-(NSSTring)title{, using this you can get your title and subtitle when you click appropriate Annotation.

in Annotation

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

 mTitle=tit;


mSubTitle=sub;

return self;

}

︶ ̄淡然 2024-10-28 05:26:04

我解决了这个问题,我在详细信息视图类中声明了一个名为 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";

I worked it out I declared a label in my detailsview class whick is called userProfileVC then used the below if statement to pass the information text to the correct label for each annotation.

-(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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文