how to add custom callout view in mapview
Asked Answered
K

4

9

I am new to mapkit in objective-c. I am able to add custom annotation in mapview.

i need to place custom callout view like below image

like this.

But i didn't get how can i design callout view like this.

i know i need to add callout in view for annotation method.

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *AnnotationViewID = @"annotationViewID";

    MKAnnotationView *annotationView = (MKAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

    if (annotationView == nil)
    {
        annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
    }

    annotationView.image = [UIImage imageNamed:@"blue_without_pin.png"];
    annotationView.annotation = annotation;


    return annotationView;
}
Kosel answered 1/5, 2012 at 7:25 Comment(7)
shawnsbits.com/blog/2011/04/12/custom-map-pins-for-mapkit/… here..Sofiasofie
thanks for your replay nit,but link which you posted is not working,it shows no results foundKosel
shawnsbits.com/blog/2010/12/23/…Endora
How about these two questions? MKAnnotationView - Lock custom annotation view to pin on location updates & Customize the MKAnnotationView calloutPreternatural
jakeri.net/2009/12/… this is a link with a working codeMetallophone
Just ran into a similar problem. After a week of searching, I've settled with SMCallout (available as a Pod). It comes with an example project and is fairly straight forward to use ! github.com/nfarina/calloutviewHolography
See https://mcmap.net/q/400187/-customize-mkannotation-callout-view and https://mcmap.net/q/409315/-custom-mkannotation-callout-bubble-with-button.Fayola
K
0

Basically what you want to do is add a new custom view by implementing the following method in your MKMapViewDelegate instance :

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
    CGPoint annotationCenter = [mapView convertCoordinate:view.annotation.coordinate toPointToView:mapView];
    [self displayCustomCalloutForAnnotation:view.annotation center:annotationCenter];
}
Kipp answered 5/6, 2012 at 11:37 Comment(0)
U
0
@interface CustomAnnotaionView : MKAnnotationView




@property (strong, nonatomic) UIView *calloutView;




-(void)setSelected:(BOOL)selected animated:(BOOL)animated;




@end




@implementation CustomAnnotaionView




@synthesize calloutView = _calloutView;




-(void)setSelected:(BOOL)selected animated:(BOOL)animated





{

    [super setSelected:selected animated:animated];

    if(selected)
    {

        [self.calloutView setFrame:CGRectMake(30, 130, 250, 135)];
        [self.calloutView sizeToFit];
        self.calloutView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"popup.png"]];
        [self addSubview:self.calloutView];

    }
    else
    {

        [self.calloutView removeFromSuperview];
    }

}

-(void)didAddSubview:(UIView *)subview
{

    if([[[subview class]description]isEqualToString:@"UICalloutView"])
    {

        [subview removeFromSuperview];

    }
}

Use this customAnnotationView class in MapView delegate method,

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id 

<MKAnnotation>)annotation

{



    if([annotation isKindOfClass:[MapAnnotation class]])
    {
       CustomAnnotationView *annotationView = (CustomAnnotaionView*)[theMapView dequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotationView"];
        if(!annotationView)
        {
         MapAnnotation *annotation = (MapAnnotation *)annotation;

         annotationView = [[CustomAnnotaionView alloc]initWithAnnotation:annotation reuseIdentifier:@"CustomAnnotationView"];

            [annotationView setCanShowCallout:YES];
        }
        else
            annotationView.annotation = annotation;

        return annotationView;

    }
    return nil;

}
Usherette answered 19/4, 2013 at 13:47 Comment(0)
W
0

One solution for this would be to detect the exact coordinate of the annotation selected relative to the parent UIView and then drawing a UIView directly on top of the MKMapView.

Here's a snippet that might help:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    CGPoint pointInMap = [self.mapView convertCoordinate:buildingAnnotation.coordinate
                                           toPointToView:self.view];

    // Hide the default callout generated by MKMapView

    [mapView deselectAnnotation:view.annotation animated:NO];

    // Create a custom callout view and center the arrow on the pointInMap CGPoint
}
Windburn answered 13/11, 2015 at 23:56 Comment(1)
If you add it as a subview of the annotation view, it will move automatically as the user pans the map.Fayola
I
-3

You must implement a callout view yourself, and use

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view

this delegate to make the callout view showing at right place,

IOS have not any method to custom callout view yet

Icbm answered 22/10, 2013 at 11:22 Comment(1)
How does this differ from @pnollet's answerMesmerism

© 2022 - 2024 — McMap. All rights reserved.