Sanchit Gupta
Sanchit Gupta

Reputation: 1293

How to add button on an MKMapView?

I'm trying to make an iPhone app where the map has buttons, how do I do that? I've already implemented MKMapView and set its longitude/latitude and centered it where I want it.

Upvotes: 2

Views: 5565

Answers (2)

Chirag Pipaliya
Chirag Pipaliya

Reputation: 1281

Try it....

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinAnnotation = nil;
    if(annotation != myMapView.userLocation) 
    {
        static NSString *defaultPinID = @"myPin";
        pinAnnotation.pinColor = MKPinAnnotationColorRed;
        pinAnnotation = (MKPinAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinAnnotation == nil )
            pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        pinAnnotation.canShowCallout = YES;

        infoButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 25, 25)];
        [infoButton setBackgroundImage:[UIImage imageNamed:@"arrow_go_map.png"] forState:UIControlStateNormal];
        pinAnnotation.rightCalloutAccessoryView = infoButton;
        [infoButton addTarget:self action:@selector(detailBtnClk:) forControlEvents:UIControlEventTouchUpInside];
    }    
    return pinAnnotation;
}

Hope i helped

Upvotes: 1

iPatel
iPatel

Reputation: 47049

You not elaborate your Question, so i guess that you want,

Use MKUserTrackingBarButtonItem

 - (void)viewDidLoad
    {
        [super viewDidLoad];    
        MKUserTrackingBarButtonItem *buttonItem = [[MKUserTrackingBarButtonItem alloc] initWithMapView:self.map];
        self.navigationItem.rightBarButtonItem = buttonItem;
    }

AMKUserTrackingBarButtonItem object is a specialized bar button item that allows the user to toggle through the user tracking modes. For example, when the user taps the button, the map view toggles between tracking the user with and without heading. The button also reflects the current user tracking mode if set elsewhere. This bar button item is associated to a single map view.

Also read this Question How to put a button on a Map in Iphone

Upvotes: 6

Related Questions