2017-07-06 36 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:方法