How to put a button on a Map in Iphone

百般思念 提交于 2019-12-24 20:16:02

问题


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,


回答1:


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:.




回答2:


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


来源:https://stackoverflow.com/questions/3303919/how-to-put-a-button-on-a-map-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!