Swift中的MapKit,第2部分

编程入门 行业动态 更新时间:2024-10-27 21:16:55
本文介绍了Swift中的MapKit,第2部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用Swift中的Map Kit。我尝试在地图上显示区域,一个引脚(MKPinAnnotationView)和当前位置。一切都很好。我尝试添加Disclosure Button并拦截它。添加了Disclosure Button,但拦截攻击不起作用。

I'm trying work with Map Kit in Swift. I try to display the area on the map, one pin (MKPinAnnotationView) and the current position. All display fine. I try to add Disclosure Button and intercept tapping on it. Disclosure Button added, but does not work intercept tapping.

函数 pinPressed 方法 calloutAccessoryControlTapped 无法工作....

Function pinPressed with method calloutAccessoryControlTapped not work....

这是一个示例代码:

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 :)。

The calloutAccessoryControlTapped delegate method must be named mapView(annotationView:calloutAccessoryControlTapped:).

您不能使用自己的名字,如 pinPressed(...)。

You can't use your own name like pinPressed(...).

这适用于任何委托方法,并由协议规定。

This applies to any delegate method and is dictated by the protocol.

所以它应该be:

func mapView(mapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { if control == annotationView.rightCalloutAccessoryView { println("Disclosure Pressed!") } }

更多推荐

Swift中的MapKit,第2部分

本文发布于:2023-10-19 15:09:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1507893.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Swift   MapKit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!