2015-10-19 63 views
2

找到這個問題的解決方案是非常令人沮喪的。我發佈了整個代碼來理解問題。請幫助如何使用Swift 2和xcode 7在MapKit中使註解點可拖動?

import UIKit 
import MapKit 
import CoreLocation 
class LocationViewController: UIViewController,MKMapViewDelegate, CLLocationManagerDelegate { 


    @IBOutlet weak var mapView: MKMapView! 

    let locationManager = CLLocationManager() 
    var annotationPoint: MKPointAnnotation! 
    var getMovedMapCenter: CLLocation! 
    var myPinView:MKPinAnnotationView! 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.locationManager.delegate = self 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     if #available(iOS 8.0, *) { 
      self.locationManager.requestWhenInUseAuthorization() 
     } else { 
      // Fallback on earlier versions 
     } 
     self.locationManager.startUpdatingLocation() 
     self.mapView.showsUserLocation = true 

     // Do any additional setup after loading the view. 
    } 

    // MARK: - Location Delegate Methods 

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
    { 
     let location = locations.last 

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

     let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1)) 

     self.mapView.setRegion(region, animated: true) 

     if(self.annotationPoint == nil) 
     { 
      self.annotationPoint = MKPointAnnotation() 
      getMovedMapCenter = CLLocation(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) 

      self.annotationPoint.coordinate = getMovedMapCenter.coordinate 

      // self.annotationPoint.title = location_value 
      //self.annotationPoint.subtitle = "" 

      // println_debug(self.annotationPoint) 

      self.mapView.addAnnotation(self.annotationPoint) 

     } 

     self.locationManager.stopUpdatingLocation() 
    } 

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) 
    { 
     print("Errors: " + error.localizedDescription) 
    } 

    //MARK:- View for Annotation 
    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 
    } 

    //MARK:- Annotation Changed State 

    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) { 

     print("hiii") 


     //  println_debug("End State coordinates: \(self.annotationPoint.coordinate.latitude),\(self.annotationPoint.coordinate.longitude)") 


     if(newState == .Ending) 
     { 


      let center = CLLocationCoordinate2D(latitude: self.annotationPoint.coordinate.latitude, longitude: self.annotationPoint.coordinate.longitude) 
      // self.currentLocationNameA(center) 

      //   dispatch_async(GlobalBackgroundQueue) 
      //    { 
      // 
      // 
      //   } 
     } 

    } 

    //MARK:- update Not Get 

    func mapView(mapView: MKMapView, 
     didFailToLocateUserWithError error: NSError) 
    { 
     // AppHelper.showALertWithTag(121, title: APP_NAME, message: "Failed to get location. Please check Location setting", delegate: nil, cancelButtonTitle: "Ok", otherButtonTitle: nil) 

    } 




} 

我被卡在這段代碼中。 Becasuse didChangeDragState委託從未調用過。請幫我找到一個更好的解決方案。我是新來的迅速;所以很難快速轉換客觀的c代碼

回答

0
pinAnnotationView.canShowCallout = false 

這行解決了我的問題。顯示「當前位置」的Popup是導致此問題的主要原因。

並且代碼中的這些更改使其平滑拖動。 Mehul和Vijay Answer可以幫助我找到這個解決方案。

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) { 
     switch (newState) { 
     case .Ending, .Canceling: 
      view.dragState = .None 
     default: break 
     } 
    } 
0
if (annotation is MKUserLocation) { 
     return nil 
    } 

    let reuseId12 = "pin12" 

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) 

    if pinView == nil { 
     pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId12) 
     pinView.image = UIImage(named:"pin-1250.png") 
     pinView.canShowCallout = false 
     pinView.draggable = true 
    } 
    else { 

     pinView.annotation = annotation 
    } 

    return pinView 

試試這個代碼。

+0

我應該在哪寫這段代碼? –

+0

didChangeDragState – vijay

+0

但是這個mehtod沒有調用。這是主要問題 –

2

拖動狀態通常會隨用戶與註釋視圖的交互而發生變化。但是,註釋視圖本身也是負責改變該狀態的 。

替換此方法並嘗試一次。可能是工作的罰款

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) { 
    switch (newState) { 
    case .Starting: 
     view.dragState = .Dragging 
    case .Ending, .Canceling: 
     view.dragState = .None 
    default: break 
    } 
} 

更換你點標註方法與這一個。

let pa = MKPointAnnotation() 
pa.coordinate = placemark.location.coordinate 
pa.title = placemark.name //or whatever title you like 
self.mapView.addAnnotation(pa) 

更多,你可以看到CodeAnother Sample Code

+0

謝謝Mehul。我正在嘗試使用示例代碼。希望它有幫助 –

+0

是啊當然.. Sourabh .. – Mehul

+0

請Upvote我的Anwer。如果你滿意的話。 – Mehul

相關問題