Tôi muốn biết nếu có ai có thể cho tôi biết cách tôi có thể chạm vào một pin trên map
dưới dạng MKPointAnnotations
.MKPointAnnotations sự kiện cảm ứng trong swift
Tôi muốn nhấp vào pin
trên map
và chuyển đến chế độ xem khác bằng cách mang lại variables
của số pin
mà tôi đã đặt trước.
Có ai có thể giải thích cho tôi điều này trong số Swift
không?
nhờ
Sửa với mã:
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)
}
}
}
Nếu tiêu đề của chú thích là trống, callout sẽ không hiển thị khi bạn gõ vào nó. – Anna
Tôi đã thêm chú thích và văn bản viết, cửa sổ bật lên xuất hiện chính xác nhưng không có gì .. bạn biết tại sao? –
Có thể cửa sổ đại biểu của chế độ xem bản đồ không được kết nối/đặt. – Anna