0

I need to put custom annotation views to mapview, and they need to be clickable. i need rather than classic callout, i will tap on the annotation and it will perform some functions.

I tried many things but still not achieved.

Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
erdemgc
  • 1,701
  • 3
  • 23
  • 43

2 Answers2

2

I found a really good implementation for you:

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

there you can add buttons to calloutView with actions

or..

in mapView delegate you can customize the annotation, and the callout view:

viewForAnnotation: Returns the annotation view associated with the specified annotation object, if any.

  • (MKAnnotationView *)viewForAnnotation:(id < MKAnnotation >)annotation

there is an example:

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

EDITED ANSWER FOR CUSTOM PIN:

make a MKAnnotationView subclass

.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

then in the mapview delegate:

- (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 class is my custom annotation class:

@interface Annotation : NSObject <MKAnnotation>

I hope this helps

Magyar Miklós
  • 4,182
  • 2
  • 24
  • 42
  • i do not want custom callout, i really need is custom annotationview, pinview – erdemgc Sep 09 '13 at 07:41
  • let me clear myself, i have done what you already explained, but my problem is it is not touchable, and also my offset is problematic. – erdemgc Sep 09 '13 at 08:04
  • if you implement a mapview and annotations on it, it s automaticly touchable i think, make sure your mapView.delegate = self; and customAnnotationView.canShowCallout = YES; I ve never met with this problem – Magyar Miklós Sep 09 '13 at 08:22
  • centerOffset The offset (in pixels) at which to display the view. @property (nonatomic) CGPoint centerOffset Discussion By default, the center point of an annotation view is placed at the coordinate point of the associated annotation. You can use this property to reposition the annotation view as needed. This x and y offset values are measured in pixels. Positive offset values move the annotation view down and to the right, while negative values move it up and to the left. – Magyar Miklós Sep 09 '13 at 08:26
  • Why not point to the offial Apple Sample Code? https://developer.apple.com/library/ios/samplecode/MapCallouts/Introduction/Intro.html – Joride Sep 09 '13 at 08:27
0

There is a method on the mapViewDelegate: viewForMapView. In this method, you return a view. This view can be completely customized by you, e.g. you can add gestureregognizers to it to handle additional gestures beside tapping.

Joride
  • 3,722
  • 18
  • 22
  • i could not find such method, in class reference ; https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html#//apple_ref/occ/intfm/MKMapViewDelegate/mapView:viewForOverlay: – erdemgc Sep 09 '13 at 07:20
  • Sorry, its full name is mapView:viewForAnnotation: – Joride Sep 09 '13 at 08:25