2

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

编辑:我要求在 Swift 中完成。

4

2 回答 2

7

您可以使用 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;
}

您也可以添加调用附件视图的方法

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

这样,您可以导航到信息按钮上的方向。

更新了 Swift 代码

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

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

}

您可以将其用于快速语言。

请为 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
    }

确保您已添加“MKMapViewDelegate”

于 2015-07-03T14:11:31.890 回答
2

对于 Swift 2,我是这样做的:

let buttonType = UIButtonType.InfoDark
barAnnotView?.rightCalloutAccessoryView = UIButton(type: buttonType)
于 2015-12-30T13:16:22.280 回答