代码之家  ›  专栏  ›  技术社区  ›  SHINTO JOSEPH

iOS Mapkit批注无法拖动

  •  3
  • SHINTO JOSEPH  · 技术社区  · 7 年前

    我是iOS开发新手,我需要在iOS地图中实现一个可拖动的注释。我的代码如下。无论我做什么,我都不能得到一个可拖动的注释。有人能帮我做注释吗?之后,我需要得到pin指向的位置的地址

    注册ViewController.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
            }
        }
    

    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 回复  |  直到 7 年前
        1
  •  4
  •   Dharmesh Kheni Steve Rosenberg    7 年前

    对你来说 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
    }
    

    因为 注释 pinAnnotation 类型

    我看不见 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
        }
    }
    
        2
  •  2
  •   pesch    7 年前

    通过设置 isDraggable 现在,您只需要处理用户停止拖放注释的情况,请参见: how to manage drag and drop for MKAnnotationView on IOS

    此外,pinAnnotation应该有一个可设置的坐标。

    从…起 Apple documentation

    将注释视图标记为可拖动的注释视图提供了 内置拖动支持,非常容易拖动 并确保注释数据

    在注释对象中,实现setCoordinate: 允许地图视图更新注释坐标点。什么时候 用户触摸并按住可拖动的注释视图,即地图视图 开始对其执行拖动操作。随着拖动操作的进行 mapView:annotationView:didChangeDragState:fromOldState:its方法

    要在拖动操作期间设置视图动画,请实现自定义 在注释视图中使用dragState方法。随着地图视图的处理 拖动相关的触摸事件,它更新 受影响的注释视图。实现自定义dragState方法可以 您有机会拦截这些更改并执行其他 操作开始,并在结束时将针脚放回地图上。

    mapView:annotationView:didChangeDragState:fromOldState: