2014-10-07 21 views

Antwort

8

Sie müssen eine benutzerdefinierte Klasse erstellen, die von MKMapView abgeleitet ist. Diese Klasse muss das Protokoll MKMapViewDelegate implementieren.

Dann brauchen Sie 2 Schritte: Erstellen Sie das Annotation-Objekt und erstellen Sie eine Ansicht für diese Annotation.

Annotation erstellen:

Irgendwo in Ihrem Code auf Ihre Bedürfnisse ab:

let annotation = MKPointAnnotation() 
annotation.setCoordinate(location) // your location here 
annotation.title = "My Title" 
annotation.subtitle = "My Subtitle" 

self.mapView.addAnnotation(annotation) 

die Ansicht Anmerkung erstellen

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 
    if annotation is MKPointAnnotation { 
     let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin") 

     pinAnnotationView.pinColor = .Purple 
     pinAnnotationView.draggable = true 
     pinAnnotationView.canShowCallout = true 
     pinAnnotationView.animatesDrop = true 

     return pinAnnotationView 
    } 

    return nil 
} 
+0

Vielen Dank !!! : D –

4

dies ist mein Beispielcode. Mit diesem Code können Sie lange auf die Karte drücken, um einen Punkt hinzuzufügen und diesen Punkt zu ziehen, bis Sie Ihren Finger vom Bildschirm lösen. Sehen Sie sich auch den GestenRecognizer an, den Sie in der Kartenansicht hinzufügen müssen. Hoffe, das könnte dir helfen.

class TravelLocationMapController: UIViewController, MKMapViewDelegate { 


@IBOutlet var mapView: MKMapView! 

var dragPin: MKPointAnnotation! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    mapView.delegate = self 
    let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: "addPin:") 
    gestureRecognizer.numberOfTouchesRequired = 1 

    mapView.addGestureRecognizer(gestureRecognizer) 
} 

func addPin(gestureRecognizer:UIGestureRecognizer){ 
    let touchPoint = gestureRecognizer.locationInView(mapView) 
    let newCoordinates = mapView.convertPoint(touchPoint, toCoordinateFromView: mapView) 
    if dragPin != nil { 
     dragPin.coordinate = newCoordinates 
    } 

    if gestureRecognizer.state == UIGestureRecognizerState.Began { 
     dragPin = MKPointAnnotation() 
     dragPin.coordinate = newCoordinates 
     mapView.addAnnotation(dragPin) 
    } else if gestureRecognizer.state == UIGestureRecognizerState.Ended {    
     dragPin = nil 
    } 
} 

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    if annotation is MKPointAnnotation { 
     let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin") 

     pinAnnotationView.pinTintColor = UIColor.purpleColor() 
     pinAnnotationView.animatesDrop = true 

     return pinAnnotationView 
    } 
    return nil 
} 

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) { 
    let lat = view.annotation?.coordinate.latitude 
    let long = view.annotation?.coordinate.longitude 

    print("Clic pin lat \(lat) long \(long)") 

} 
+0

Super, danke! Sie sollten versuchen, ein vorhandenes '' MKPinAnnotationView'' mit '' mapView.dequeueReusableAnnotationViewWithIdentifier ("myPin") '' in '' mapView (_: viewForAnnotation) '' zu bekommen. – brki

Verwandte Themen