MKT上的Swift/iOS MapKit功能
我有一张地图,我已经为它添加了12个注释,我想要的是,当有人点击这些注释时,它会在注释右侧有一个信息按钮,点击该按钮时,会在地图应用程序中显示注释的方向。我编写了一个函数,用所需的坐标打开“地图”应用程序。我只是不知道如何将“信息”按钮添加到注释中,并使其在点击时执行该函数MKT上的Swift/iOS MapKit功能,ios,swift,ios8,mapkit,mapkitannotation,Ios,Swift,Ios8,Mapkit,Mapkitannotation,我有一张地图,我已经为它添加了12个注释,我想要的是,当有人点击这些注释时,它会在注释右侧有一个信息按钮,点击该按钮时,会在地图应用程序中显示注释的方向。我编写了一个函数,用所需的坐标打开“地图”应用程序。我只是不知道如何将“信息”按钮添加到注释中,并使其在点击时执行该函数 编辑:我要求用Swift完成此操作。您可以使用VIEWFORMAPVIEW的注释方法 如下 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotati
编辑:我要求用Swift完成此操作。您可以使用VIEWFORMAPVIEW的注释方法 如下
- (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;
}
通过这种方式,您可以导航到信息按钮上的方向
已更新Swift代码
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!,
calloutAccessoryControlTapped control: UIControl!) {
if control == view.rightCalloutAccessoryView {
println("Disclosure Pressed! \(view.annotation.subtitle)"
}
}
您可以将其用于swift语言
请将此代码添加到ViewFor注释:
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
}
确保已为Swift 2添加了“MKMapViewDelegate”,我是这样做的:
let buttonType = UIButtonType.InfoDark
barAnnotView?.rightCalloutAccessoryView = UIButton(type: buttonType)
谢谢,但我相信上面的代码是针对Obj-C的,我正在寻找swift。我已经添加了此代码,它可以工作,但我看不到任何更改或信息按钮?好的,我现在收到一个“使用未声明的类型‘CustomPointAnnotation’”错误。只是确认一下,我的类是:类位置:UIViewController,MKMapViewDelegate{//code here}
let buttonType = UIButtonType.InfoDark
barAnnotView?.rightCalloutAccessoryView = UIButton(type: buttonType)