Swift中的MapKit,第2部分
发布时间:2020-12-14 04:34:52 所属栏目:百科 来源:网络整理
导读:我正在尝试使用 Swift中的Map Kit.我尝试在地图上显示区域,一个引脚(MKPinAnnotationView)和当前位置.一切都很好.我尝试添加Disclosure Button并拦截它. Disclosure Button已添加,但无法拦截攻丝. 函数pinPressed方法calloutAccessoryControlTapped不起作用
我正在尝试使用
Swift中的Map Kit.我尝试在地图上显示区域,一个引脚(MKPinAnnotationView)和当前位置.一切都很好.我尝试添加Disclosure Button并拦截它. Disclosure Button已添加,但无法拦截攻丝.
函数pinPressed方法calloutAccessoryControlTapped不起作用…. 这是一个示例代码: import UIKit import MapKit import CoreLocation class ViewController: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate { @IBOutlet weak var mainMapView: MKMapView! var locationManager = CLLocationManager() override func viewDidLoad() { super.viewDidLoad() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.requestAlwaysAuthorization() locationManager.startUpdatingLocation() var objectLatitude = 53.204526 var objectLongitude = 50.111751 var currentLatitude = 53.203715 var currentLongitude = 50.160374 var latDelta = 0.05 var longDelta = 0.05 var currentLocationSpan: MKCoordinateSpan = MKCoordinateSpanMake(latDelta,longDelta) var currentLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(locationManager.location.coordinate.latitude,locationManager.location.coordinate.longitude) var currentRegion: MKCoordinateRegion = MKCoordinateRegionMake(currentLocation,currentLocationSpan) self.mainMapView.setRegion(currentRegion,animated: true) var objectLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(objectLatitude,objectLongitude) var objectAnnotation = MKPointAnnotation() objectAnnotation.coordinate = objectLocation objectAnnotation.title = "St. George's Church" objectAnnotation.subtitle = "Church of the Great Martyr St. George" self.mainMapView.addAnnotation(objectAnnotation) } func mapView(mapView: MKMapView!,viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { if annotation is MKUserLocation { //return nil so map view draws "blue dot" for standard user location return nil } let reuseId = "pin" var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView if pinView == nil { pinView = MKPinAnnotationView(annotation: annotation,reuseIdentifier: reuseId) pinView!.canShowCallout = true pinView!.animatesDrop = true pinView!.pinColor = .Purple pinView!.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as UIButton } else { pinView!.annotation = annotation } return pinView } func pinPressed(mapView: MKMapView!,annotationView: MKAnnotationView,calloutAccessoryControlTapped control: UIControl) { if control == annotationView.rightCalloutAccessoryView { println("Disclosure Pressed!") } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } 解决方法
calloutAccessoryControlTapped委托方法必须命名为mapView(annotationView:calloutAccessoryControlTapped :).
您不能使用自己的名称,如pinPressed(…). 这适用于任何委托方法,并由协议规定. 所以它应该是: func mapView(mapView: MKMapView!,calloutAccessoryControlTapped control: UIControl) { if control == annotationView.rightCalloutAccessoryView { println("Disclosure Pressed!") } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |