MKAnnotation tap上的Swift/iOS MapKit功能

use*_*770 2 mapkit ios mapkitannotation swift ios8

我有一张地图,我已经完美地添加了12个注释,我想要的是当有人点击这些注释时,它会在注释的右边有一个信息按钮,当点击时会显示注释的方向.地图应用.我已经编写了使用所需坐标打开地图应用程序的功能我只是不确定如何将信息按钮添加到注释并使其在点击时执行该功能.

编辑:我要求在Swift中完成此操作.

BKj*_*dav 6

您可以使用mapview的ViewForAnnotation方法,如下所示

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"loc"];
    annotationView.canShowCallout = YES;
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    return annotationView;
}
Run Code Online (Sandbox Code Playgroud)

您还可以添加呼叫附件视图的方法

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    [self performSegueWithIdentifier:@"DetailsIphone" sender:view];
}
Run Code Online (Sandbox Code Playgroud)

这样您就可以在信息按钮上导航到方向.

更新了Swift代码

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, 
             calloutAccessoryControlTapped control: UIControl!) {

    if control == view.rightCalloutAccessoryView {
        println("Disclosure Pressed! \(view.annotation.subtitle)"
    }

}
Run Code Online (Sandbox Code Playgroud)

你可以用它来快速语言.

请为viewforAnnotation添加此代码:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
        if !(annotation is CustomPointAnnotation) {
            return nil
        }

        let reuseId = "test"

        var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
        if anView == nil {
            anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            anView.canShowCallout = true

            anView.rightCalloutAccessoryView = UIButton.buttonWithType(.InfoDark) as UIButton

        }
        else {
            anView.annotation = annotation
        }



        let cpa = annotation as CustomPointAnnotation
        anView.image = UIImage(named:cpa.imageName)

        return anView
    }
Run Code Online (Sandbox Code Playgroud)

确保你已经添加了"MKMapViewDelegate"