Ashutosh
Ashutosh

Reputation: 5742

How to put a button on a Map in Iphone

i am trying to pull a map in my applcation with interface builder using MKMapView but for some reason its not showing up. Also i want to add some button to this view by clicking which i can browse a file existing in my iphone.

Please provide me with the detial description as i am new to this.

Thanks,

Upvotes: 0

Views: 1983

Answers (2)

Rakesh Bhatt
Rakesh Bhatt

Reputation: 4606

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

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

    if (annotationView == nil)
    {
        annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
    }
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    annotationView.image = [UIImage imageNamed:@"s11.png"];

    annotationView.annotation = annotation;

    [annotationView setEnabled:YES];
    [annotationView setCanShowCallout:YES];

    return annotationView;
}

Upvotes: 0

Avalanchis
Avalanchis

Reputation: 4559

You'll want to add an annotation to the map, then provide a custom view for it.

To add an annotation to the map, adopt the MKAnnotation protocol in one of your objects and set its coordinate property to the appropriate lat/lon location.

Next, you'll add the annotation to the map using MKMapView addAnnotation.

Set the map's delegate property to your view controller, then implement mapView:viewForAnnotation:

When this method gets called, return a MKAnnotationView for your annotation. Set the MKAnnotationView's image property to whatever image you want the annotation to use (an image of a button perhaps?).

You can implement mapView:didSelectAnnotationView: if you want to know when the annotation was selected.

You can also set a callout accessory button on the annotation using MKAnnotationView's leftCalloutAccessoryView and rightCalloutAccessoryView properties. If you do this, you can then respond when the user selects the callout button by implementing mapView:annotationView:calloutAccessoryControlTapped:.

Upvotes: 1

Related Questions