2016-02-28 40 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

相关问题