2014-11-18 5 views
7

Ich würde gerne wissen, ob jemand mir sagen kann, wie ich einen Stift auf dem map in Form von MKPointAnnotations berühren kann.MKPointAnnotations Touch-Ereignis in swift

Ich mag würde die pin auf den map klicken und die variables der pin, die ich vorgegeben zu einer anderen Ansicht zu gehen, indem habe zurück.

Kann mir jemand dieses Ding in Swift erklären?

dank

Bearbeiten mit Code:

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) 
    } 
} 
} 
+0

Wenn die Titel der Annotation leer ist, callout wird nicht angezeigt, wenn Sie darauf tippen. – Anna

+0

Ich habe die Annotation hinzugefügt, und ein geschriebener Text, das Popup kommt richtig raus, tut aber nichts. Weißt du warum? –

+0

Möglicherweise ist der Delegiertenauslass der Kartenansicht nicht verbunden/festgelegt. – Anna

Antwort

20

Mehrere Schritte sind notwendig, hier sind einige Code-Schnipsel Sie den Start.

Zuerst benötigen Sie eine benutzerdefinierte Klasse für Ihre Pin Annotation, die die Daten enthält, mit denen Sie arbeiten möchten.

import MapKit 
import Foundation 
import UIKit 

class PinAnnotation : NSObject, MKAnnotation { 
    private var coord: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0) 

    var coordinate: CLLocationCoordinate2D { 
     get { 
      return coord 
     } 
    } 

    var title: String = "" 
    var subtitle: String = "" 

    func setCoordinate(newCoordinate: CLLocationCoordinate2D) { 
     self.coord = newCoordinate 
    }   
} 

Dann benötigen Sie eine benutzerdefinierte Klasse für Ihre MKMapView die das MKMapViewDelegate Protokoll entspricht. Implementieren Sie die Methode viewForAnnotation dort:

import MapKit 
import CLLocation 
import Foundation 
import UIKit 

class MapViewController: UIViewController, MKMapViewDelegate { 

    ... 

    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 { 
      mapView.removeAnnotation(annotation) 
     } 
    } 

Dass Sie so etwas wie dieses gibt:

enter image description here

Um eine neue Anmerkung zu Ihrer Karte hinzufügen verwenden Sie diesen irgendwo im Code:

let pinAnnotation = PinAnnotation() 
pinAnnotation.setCoordinate(location) 

mapView.addAnnotation(pinAnnotation) 
+0

Hallo, in viewDidLoad um den Punkt auf der Karte als Septum zu laden? Ich kann nicht herausfinden, wie man der Karte den Pin hinzufügen kann –

+0

Ich habe meine Antwort bearbeitet, um zu zeigen, wie man einen neuen Pin zur Karte hinzufügt. – zisoft

+0

Ich habe meine Frage mit meinem Code bearbeitet, jetzt sehe ich den Pin auf der Karte, aber wenn ich nichts klicke, erscheint kein Tooltip, wie repariere ich? –

3

Greate Arbeit !!! ABER .. Ich kopiere nur vorbei und ich musste ein paar Änderungen hinzufügen. Ich werde diese Veränderungen mit euch teilen.

import MapKit 
import Foundation 
import UIKit 

class PinAnnotation : NSObject, MKAnnotation { 
private var coord: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0) 
private var _title: String = String("") 
private var _subtitle: String = String("") 

var title: String? { 
    get { 
     return _title 
    } 
    set (value) { 
     self._title = value! 
    } 
} 

var subtitle: String? { 
    get { 
     return _subtitle 
    } 
    set (value) { 
     self._subtitle = value! 
    } 
} 

var coordinate: CLLocationCoordinate2D { 
    get { 
     return coord 
    } 
} 

func setCoordinate(newCoordinate: CLLocationCoordinate2D) { 
    self.coord = newCoordinate 
} 
} 

Ich hoffe, dass es helfen wird: D