MKAnnotation水龙头上的Swift/iOS MapKit功能

问题描述:

我有一张地图,我已经完美地添加了12条注解,我想要的是当有人点击这些注解时,它会在注释右边有一个信息按钮点击时会显示地图应用程序中注释的路线。我已经写了函数来打开地图应用程序与所需的坐标我只是不知道如何去添加信息按钮的注释,并使其在点击时执行该功能。MKAnnotation水龙头上的Swift/iOS MapKit功能

编辑:我需要这个在Swift中完成。

可以使用ViewForAnnotation方法MapView的 ,如下

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

这样你就可以浏览到信息按钮方向add方法。

更新了雨燕代码

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”

+0

谢谢,但我相信上面的代码是用于Obj-C的,我很快就在寻找。 – user4011770

+0

检查更新的代码 – BKjadav

+0

我已经添加了这段代码,它可以工作,但是我看不到任何更改或信息按钮? – user4011770

对于斯威夫特2我就是这么做的:

let buttonType = UIButtonType.InfoDark 
barAnnotView?.rightCalloutAccessoryView = UIButton(type: buttonType)