2017-07-06 60 views
2

我是iOS開發新手,需要在iOS地圖中實現可拖動的註釋。我的代碼是below.whater曾經給我做,我不能得到一個可拖動標註任何人都可以使用之後的註解,我需要獲得位置的地址,幫助其中銷指向iOS Mapkit註解不可拖動

RegistrationViewController.swift

class RegistrationViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource,MKMapViewDelegate{ 
    @IBOutlet weak var mapView: MKMapView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     var centerLocation = CLLocationCoordinate2DMake(12.964370125970357, 77.59643554937497) 
     var mapspan = MKCoordinateSpanMake(0.01, 0.01) 
     var mapregion = MKCoordinateRegionMake(centerLocation, mapspan) 
     self.mapView.setRegion(mapregion, animated: true) 

     let pin = pinAnnotation(title:"hello",subtitle:"how are you",coordinate:centerLocation) 
     mapView.addAnnotation(pin) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 
     if annotation is MKPointAnnotation { 
      let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin") 
      pinAnnotationView.pinColor = .purple 
      pinAnnotationView.isDraggable = true 
      pinAnnotationView.canShowCallout = true 
      pinAnnotationView.animatesDrop = true 

      return pinAnnotationView 
     } 

     return nil 
    } 

    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, didChange newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) { 
     switch newState { 
     case .starting: 
      view.dragState = .dragging 
     case .ending, .canceling: 
      view.dragState = .none 
     default: break 
     } 
    } 

pinAnnotation.swift

import MapKit 

class pinAnnotation:NSObject,MKAnnotation { 
    var title:String? 
    var subtitle: String? 
    var coordinate: CLLocationCoordinate2D 
    init(title:String,subtitle:String,coordinate:CLLocationCoordinate2D) { 
     self.title = title 
     self.subtitle = subtitle 
     self.coordinate = coordinate 
    } 
} 

回答

2

在你的情況annotation不是MKPointAnnotation類型。這就是爲什麼它不進入if annotation is MKPointAnnotation {的條件。

應該是這樣:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 

    if annotation is pinAnnotation { 

     let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin") 
     pinAnnotationView.pinTintColor = .purple 
     pinAnnotationView.isDraggable = true 
     pinAnnotationView.canShowCallout = true 
     pinAnnotationView.animatesDrop = true 

     return pinAnnotationView 
    } 

    return nil 
} 

因爲annotationpinAnnotation類型。我的代碼中看不到mapView.delegate = self。但是,如果你通過故事板指定它很好,否則將其添加到viewDidLoad方法。

而且你完整的代碼將是:

import UIKit 
import MapKit 

class ViewController: UIViewController,MKMapViewDelegate{ 
    @IBOutlet weak var mapView: MKMapView! 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     mapView.delegate = self 
     let centerLocation = CLLocationCoordinate2D(latitude: 12.964370125970357, longitude: 77.59643554937497) 
     let mapspan = MKCoordinateSpanMake(0.01, 0.01) 
     let mapregion = MKCoordinateRegionMake(centerLocation, mapspan) 
     self.mapView.setRegion(mapregion, animated: true) 

     let pin = pinAnnotation(title:"hello",subtitle:"how are you",coordinate:centerLocation) 
     mapView.addAnnotation(pin) 

    } 


    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 

     if annotation is pinAnnotation { 

      let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin") 
      pinAnnotationView.pinTintColor = .purple 
      pinAnnotationView.isDraggable = true 
      pinAnnotationView.canShowCallout = true 
      pinAnnotationView.animatesDrop = true 

      return pinAnnotationView 
     } 

     return nil 
    } 

    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, didChange newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) { 
     switch newState { 
     case .starting: 
      view.dragState = .dragging 
     case .ending, .canceling: 
      view.dragState = .none 
     default: break 
     } 
    } 
} 

編輯

您可以view.annotation?.coordinate

檢查下面的代碼得到新的位置:

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, didChange newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) { 
    switch newState { 
    case .starting: 
     view.dragState = .dragging 
    case .ending, .canceling: 
     //New cordinates 
     print(view.annotation?.coordinate) 
     view.dragState = .none 
    default: break 
    } 
} 
+0

其工作的兄弟感謝! –

+0

兄弟你能告訴我如何獲得標記指向的位置地址現在 –

+0

檢查更新的答案。 –

2

你必須在isDraggable屬性設置爲true完成第一步。 現在你只需要在用戶停止拖動和下降的註釋來處理,請參閱: how to manage drag and drop for MKAnnotationView on IOS

此外,您pinAnnotation應該有一個可設置的座標。

Apple documentation

弄髒您的註釋視圖可拖動註解視圖提供 內置拖動支持,這使得它非常容易拖動 標註在地圖周圍,以確保註釋數據是 相應更新。實現對拖動的最小支持:

在您的註釋對象中,實現setCoordinate:方法爲 ,允許地圖視圖更新註釋的座標點。當 創建您的註釋視圖時,將其可拖動屬性設置爲YES。當用戶觸摸並保存可拖動的註釋視圖時,地圖視圖 開始對其進行拖動操作。隨着拖動操作的進行, 地圖視圖會調用 代理的mapView:annotationView:didChangeDragState:fromOldState:方法,以通知其更改視圖的拖動狀態。您可以使用此方法影響或響應拖動操作。

要在拖動操作過程中爲您的視圖設置動畫效果,請在註釋視圖中實現自定義 dragState方法。當地圖視圖處理 與拖動相關的觸摸事件時,它會更新受影響的註記視圖的dragState屬性。實現一個自定義的dragState方法使 有機會攔截這些更改並執行其他 操作,例如動畫化視圖的外觀。例如, MKPinAnnotationView類在拖動操作開始時將地圖引出地圖,並在地圖結束時將引腳放回地圖。

你缺少mapView:annotationView:didChangeDragState:fromOldState:方法