2016-03-22 21 views
0

私はMapKitでピンをドラッグ可能にするために何時間も努力してきましたが、ピンがとても頑固で動きたくないと思われます。ピンをドラッグ可能にして長くクリックする

これは私のコードです:

import UIKit 
import MapKit 

protocol AddCoffeeDelegate { 
    func viewController(vc: AddCoffeeViewController, didAddCoffee coffee : Coffee!) 
} 

class AddCoffeeViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { 

    @IBOutlet var mapView: MKMapView! 

    @IBOutlet weak var coffeeName: UITextField! 

    @IBOutlet weak var coffeeRating: UITextField! 

    var coffee: Coffee? 
    var delegate: AddCoffeeDelegate? 



    //////////////////////////////////////////////////// 
    var coreLocationManager = CLLocationManager() 
    var locationManager : LocationManager! 
    var savedLocation : CLLocation? 


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

      pinAnnotationView.pinTintColor = UIColor.redColor() 
      pinAnnotationView.draggable = true 
      pinAnnotationView.canShowCallout = false 
      pinAnnotationView.animatesDrop = true 

      return pinAnnotationView 
     } 

     return nil 
    } 



    func getLocation(){ 
     locationManager.startUpdatingLocationWithCompletionHandler { (latitude, longitude, status, verboseMessage, error) ->() in 
      self.displayLocation(CLLocation(latitude: latitude, longitude: longitude)) 
     } 
    } 

    func displayLocation(location: CLLocation){ 
     mapView.setRegion(MKCoordinateRegion(center: CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude), span: MKCoordinateSpanMake(0.05, 0.05)), animated: true) 


     let locationPinCoord = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude) 

     let annotation = MKPointAnnotation() 

     annotation.title = "My Title" 
     annotation.subtitle = "My Subtitle" 
     annotation.coordinate = locationPinCoord 
     mapView.addAnnotation(annotation) 

     savedLocation = location 
    } 

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 
     if status != CLAuthorizationStatus.NotDetermined || status != CLAuthorizationStatus.Denied || status != CLAuthorizationStatus.Restricted { 
      getLocation() 
     } 
    } 


    //////////////////////////////////////////////////// 


    override func viewDidLoad() { 
     super.viewDidLoad() 


     coreLocationManager.delegate = self 
     locationManager = LocationManager.sharedInstance 

     let authorizationCode = CLLocationManager.authorizationStatus() 

     if authorizationCode == CLAuthorizationStatus.NotDetermined && coreLocationManager.respondsToSelector("requestAlwaysAuthorization") || coreLocationManager.respondsToSelector("requestWhenInUseAuthorization"){ 
      if NSBundle.mainBundle().objectForInfoDictionaryKey("NSLocationAlwaysUsageDescription") != nil { 
       coreLocationManager.requestAlwaysAuthorization() 
      }else{ 
       print("no desscription provided ") 
      } 
     }else{ 
      getLocation() 
     } 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


    @IBAction func cancel(sender: AnyObject) { 
     self.dismissViewControllerAnimated(true, completion: nil) 
    } 


    @IBAction func save(sender: AnyObject) { 

     var createdCoffee = Coffee() 
     createdCoffee.name = self.coffeeName.text! 
     createdCoffee.rating = Double(self.coffeeRating.text!)! 
     createdCoffee.place = savedLocation 
     self.coffee = createdCoffee 
     self.delegate?.viewController(self, didAddCoffee: self.coffee) 
    } 




} 

は私が迅速にmapkitとすべての関連する問題を試してみましたが、問題があることができ、ピンがitself.Whereをドラッグしないであろうと思われますか?私はすでにタイトルを設定し、MKMapViewDelegateプロトコルを実装していますが、それでもドラッグする必要はありません。

答えて

0

マップビューのデリゲートをコード内またはストーリーボードを介してビューコントローラに設定しましたか?私は実現しなかった

override func viewDidLoad() { 
    super.viewDidLoad() 
    mapView.delegate = self 
    ... 
} 
+0

OMG !!!!!!!!!!!!スーパーthnx男!!!!!! –

+0

あなたは大歓迎です:) –