获取详细信息以显示注释上方

时间:2012-01-10 01:31:08

标签: iphone ios ios5 mapkit

好的,所以我的注释显示正常,但我无法弄清楚如何让小对话框出现在它上面。当我创建注释时,我这样做:

Annotation *annot = [[Annotation alloc] init];
annot.coordinate = touchMapCoordinate;
annot.title = @"Location";
annot.subtitle = @"The address";
[self.mapView addAnnotation:annot];

同样,这可以找到,但它没有显示小对话框。

我知道我与代表有关。我下载了Apple的MapCallouts示例代码,但无法弄清楚他们是如何联系代理注释的。

这是我的注释类

Annotation.H

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface Annotation : NSObject <MKAnnotation>{
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}

@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

-(id)initWithCoordinate:(CLLocationCoordinate2D) c 
                  title:(NSString *) t
               subtitle:(NSString *) st;

-(void) moveAnnotation: (CLLocationCoordinate2D) newCoordinate;

-(NSString *)subtitle;
-(NSString *)title;



@end

Annotation.m

#import "Annotation.h"

@implementation Annotation

@synthesize coordinate = _coordinate;
@synthesize title = _title;
@synthesize subtitle = _subtitle;



-(id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t subtitle:(NSString *)st
{
    coordinate = c;
    self.title = t;
    self.subtitle = st;
    return self;
}

-(void)moveAnnotation:(CLLocationCoordinate2D)newCoordinate
{
    coordinate = newCoordinate;
}

-(NSString *)subtitle {
    return subtitle;
}
-(NSString *)title{
    return title;
}
@end

这是我设置的delagate:

-(MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id < MKAnnotation >)annotation
{  
if ([annotation isKindOfClass:[Annotation class]])
{
    static NSString *reuseId = @"customAnn";

    MKAnnotationView *customAnnotationView = [aMapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
    if (customAnnotationView == nil)
    {
        customAnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
        UIImage *pinImage = [UIImage imageNamed:@"pin-green.png"];
        [customAnnotationView setImage:pinImage];
        customAnnotationView.canShowCallout = YES;
        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        customAnnotationView.rightCalloutAccessoryView = rightButton;
    }

    customAnnotationView.annotation = annotation;

    return customAnnotationView; 
}

return nil; 
}

如果您需要我的更多信息,请提前告知我们并提前感谢您!

1 个答案:

答案 0 :(得分:2)

您需要在MKMapView的委托上实现以下MKMapViewDelegate方法:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation

确保在canShowCallout启用{{1}}的情况下返回MKAnnotationView