【问题标题】:iOS Mapkit Annotation is not getting draggableiOS Mapkit Annotation 无法拖动
【发布时间】:2017-07-06 09:40:29
【问题描述】:

我是 iOS 开发新手,我需要在 iOS 地图中实现可拖动的注释。我的代码在下面给出。无论我做什么,我都无法获得可拖动的注释,任何人都可以帮助注释之后我需要获取引脚指向的位置的地址

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
    }
}

【问题讨论】:

    标签: ios swift mapkit mkannotation


    【解决方案1】:

    在您的情况下,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
        }
    }
    

    【讨论】:

    • 兄弟你能告诉我如何获取标记现在指向的位置地址
    • 很高兴为您提供帮助.. :)
    • 谢谢兄弟,你让我开心
    • 更新 view.dragState = .dragging 帮助我更新了自定义引脚位置,谢谢!
    【解决方案2】:

    您已通过将isDraggable 属性设置为true 来完成第一步。 现在只需要在用户停止拖拽注解时进行处理,见: how to manage drag and drop for MKAnnotationView on IOS

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

    来自Apple documentation

    将您的注释视图标记为可拖动注释视图提供 内置拖拽支持,拖拽非常方便 地图周围的注释,并确保注释数据是 相应更新。实现对拖动的最小支持:

    在您的注释对象中,实现 setCoordinate: 方法以 允许地图视图更新注解的坐标点。什么时候 创建注释视图,将其可拖动属性设置为 YES。什么时候 用户触摸并按住可拖动的注释视图,地图视图 开始对其进行拖动操作。随着拖动操作的进行, 地图视图调用 mapView:annotationView:didChangeDragState:fromOldState: 其方法 委托通知它您的视图拖动状态的更改。你 可以使用该方法来影响或响应拖拽操作。

    要在拖动操作期间为您的视图设置动画,请实现自定义 注释视图中的 dragState 方法。随着地图视图的处理 与拖动相关的触摸事件,它会更新 受影响的注释视图。实现一个自定义的 dragState 方法给出了 您有机会拦截这些更改并执行额外的 动作,例如动画视图的外观。例如, MKPinAnnotationView 类在拖动时将图钉从地图上抬起 操作开始并在结束时将图钉放回地图上。

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

    【讨论】:

      猜你喜欢
      • 2012-04-14
      • 2015-01-20
      • 1970-01-01
      • 2015-06-28
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多