【发布时间】:2014-11-18 10:02:26
【问题描述】:
我想知道是否有人可以告诉我如何以MKPointAnnotations 的形式触摸map 上的针。
我想点击map 上的pin 并通过带回我预设的pin 的variables 转到另一个视图。
任何人都可以在Swift 中向我解释这件事吗?
谢谢
用代码编辑:
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mappa: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
var location : CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 44.648590, longitude: 10.918794)
let pinAnnotation = PinAnnotation()
pinAnnotation.setCoordinate(location)
self.mappa.addAnnotation(pinAnnotation)
}
class PinAnnotation : NSObject, MKAnnotation {
private var coord: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0)
var coordinate: CLLocationCoordinate2D {
get {
return coord
}
}
var title: String = "test"
var subtitle: String = "test"
func setCoordinate(newCoordinate: CLLocationCoordinate2D) {
self.coord = newCoordinate
}
}
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is PinAnnotation {
let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin")
pinAnnotationView.pinColor = .Purple
pinAnnotationView.draggable = true
pinAnnotationView.canShowCallout = true
pinAnnotationView.animatesDrop = true
let deleteButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
deleteButton.frame.size.width = 44
deleteButton.frame.size.height = 44
deleteButton.backgroundColor = UIColor.redColor()
deleteButton.setImage(UIImage(named: "trash"), forState: .Normal)
pinAnnotationView.leftCalloutAccessoryView = deleteButton
return pinAnnotationView
}
return nil
}
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
if let annotation = view.annotation as? PinAnnotation {
self.mapView.removeAnnotation(annotation)
}
}
}
【问题讨论】:
-
如果注解的标题为空白,点击时不会显示标注。
-
我添加了注释和一个书面文本,弹出窗口正常但什么也没做..你知道为什么吗?
-
也许地图视图的代理出口没有连接/设置。
-
我确认它已连接,最重要的是,我想知道我在哪里匹配 MapView 方法,是语言新手,如果你不调用,他无法理解他如何离开这个方法。 ..谢谢
标签: swift view mapkit mkpointannotation