"点击CustomAnnotation"用于MKMapView

时间:2013-09-09 06:58:05

标签: ios mkmapview mkannotation

我需要将自定义注释视图放到mapview中,并且它们需要是可点击的。我需要而不是经典标注,我将点击注释,它将执行一些功能。

我尝试过很多东西,但仍未实现。

2 个答案:

答案 0 :(得分:2)

我找到了一个非常好的实现方式:

http://blog.asolutions.com/2010/09/building-custom-map-annotation-callouts-part-1/

你可以通过动作

为calloutView添加按钮

或..

在mapView委托中,您可以自定义注释和标注视图:

viewForAnnotation: 返回与指定注释对象关联的注释视图(如果有)。

  • (MKAnnotationView *)viewForAnnotation:(id< MKAnnotation>)注释

有一个例子:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{   
    MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"loc"];

    // Button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    button.frame = CGRectMake(0, 0, 23, 23);
    annotationView.rightCalloutAccessoryView = button;

    // Image and two labels
    UIView *leftCAV = [[UIView alloc] initWithFrame:CGRectMake(0,0,23,23)];
    [leftCAV addSubview : yourImageView];
    [leftCAV addSubview : yourFirstLabel];
    [leftCAV addSubview : yourSecondLabel];
    annotationView.leftCalloutAccessoryView = leftCAV;

    annotationView.canShowCallout = YES;

    return annotationView;
}

针对CUSTOM PIN的编辑答案:

制作MKAnnotationView子类

.h

#import <MapKit/MapKit.h>

@interface AnnotationView : MKAnnotationView

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;

@end

.m

#import "AnnotationView.h"

@implementation AnnotationView

{
    NSString *identifier;
}

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];

    if (self != nil)

    {

        CGRect frame = self.frame;

        frame.size = CGSizeMake(78.0, 35.0);

        self.frame = frame;

        self.backgroundColor = [UIColor clearColor];

        self.centerOffset = CGPointMake(0, 0);


    }

    return self;

}

-(void) drawRect:(CGRect)rect

{


    [[UIImage imageNamed:@"yourImage"]drawInRect:CGRectMake(0, 0, 78, 35)];


}


@end

然后在mapview委托中:

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

    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
    }

    if ([annotation isKindOfClass:[Annotation class]]) {

        NSString* annotationIdentifier = @"yourAnnotation";
        AnnotationView* customAnnotationView = (AnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];

        if (!customAnnotationView) {
            customAnnotationView = [[AnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];

            customAnnotationView.canShowCallout = YES;
        }

        else{
            customAnnotationView.annotation= annotation;
        }


        return customAnnotationView;
    }
    return nil;
}

Annotation类是我的自定义注释类:

@interface Annotation : NSObject <MKAnnotation>

我希望这会有所帮助

答案 1 :(得分:0)

mapViewDelegate上有一个方法:viewForMapView。在此方法中,您返回一个视图。您可以完全自定义此视图,例如您可以添加gestureregognizers来处理旁边的其他手势。