2016-02-28 39 views
1

我有以下的代碼,其獲取的GPS從用戶座標,並把紅色標記在目前他的地方:爲什麼我的MKAnnotationPoint不可拖動,我該如何改變它?

class ViewController: UIViewController, CLLocationManagerDelegate { 

    @IBOutlet var mapView: MKMapView! 
    var locationManager: CLLocationManager? 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     locationManager = CLLocationManager() 
     locationManager!.delegate = self 

     if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse { 
      locationManager!.startUpdatingLocation() 
     } else { 
      locationManager!.requestWhenInUseAuthorization() 
     } 
    } 

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 


     switch status { 
     case .NotDetermined: 
      print("NotDetermined") 
     case .Restricted: 
      print("Restricted") 
     case .Denied: 
      print("Denied") 
     case .AuthorizedAlways: 
      print("AuthorizedAlways") 
     case .AuthorizedWhenInUse: 
      print("AuthorizedWhenInUse") 
      locationManager!.startUpdatingLocation() 
     } 
    } 

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

     let location = locations.first! 
     let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 500, 500) 
     mapView.setRegion(coordinateRegion, animated: true) 
     locationManager?.stopUpdatingLocation() 

     let annotation = MKPointAnnotation() 
     annotation.coordinate = location.coordinate 


     longitude = location.coordinate.longitude 
     latitude = location.coordinate.latitude 


     mapView.addAnnotation(annotation) 


     locationManager = nil 
    } 

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { 
     print("Failed to initialize GPS: ", error.description) 
    } 
} 

這偉大工程,你可以看到,我使用這些線路獲取用戶的lat和長時間以後對其進行處理:

 longitude = location.coordinate.longitude 
     latitude = location.coordinate.latitude 

現在我想添加其他功能 - 用戶看到地圖爲紅色圖釘他目前的位置,並可以離開它,因爲它是或 - 新功能 - 他可以將紅色傢伙拖到其他地方並獲得新的經度和緯度。

我一直期待在這裏和那裏的拖動功能,我已經決定將此代碼添加到現有的一個:

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 
} 

但它並沒有這樣的伎倆,銷仍然是不可拖動。每次用戶抓住並移動它時,我如何解決這個問題並(現在 - 打印到控制檯)插入新的位置?

回答

1

如果您看到的是紅色標記,而不是您在代碼示例中指定的紫色標記,則表明viewForAnnotation未被調用。如果您忽略指定地圖視圖的代表,則會發生這種情況。您可以在IB中通過控制從IB中的「連接檢查器」中的delegate插座中進行編程,或者通過編程方式執行編程,例如控制。在viewDidLoad

mapView.delegate = self 

無關的,我建議你檢查viewForAnnotation的方法簽名(現在不使用隱式展開自選)。我也建議離隊的註釋,如:

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

      pinAnnotationView?.pinTintColor = UIColor.purpleColor() // use pinTintColor if targeting iOS 9 and later 
      pinAnnotationView?.draggable = true 
      pinAnnotationView?.canShowCallout = true 
      pinAnnotationView?.animatesDrop = true 
     } else { 
      pinAnnotationView?.annotation = annotation 
     } 

     return pinAnnotationView 
    } 

    return nil 
} 
+0

感謝@Rob,還有一兩件事,雖然 - 當我把'mapView.delegate = self'然後它會導致一個錯誤'無法指定類型的值<< NameOfMyClass >>鍵入MkMapViewDelegate?'。有沒有修復它的方法?到目前爲止,mapView只是我的類中的嵌入式地圖視圖,它繼承了:'class << NameOfMyClass:UIViewController,UINavigationControllerDelegate,UIImagePickerControllerDelegate,CLLocationManagerDelegate {' – user3766930

+0

是的,您必須將'MKMapViewDelegate'添加到協議列表中。 – Rob

+0

我把你的代碼粘貼到我的類中,我也包含了'MKMapViewDelegate',並且該針是紫色的,所以這很好,但我無法移動它。還有什麼我可能會錯過的? – user3766930

相關問題