如何在 IOS 上管理 MKAnnotationView 的拖放?

发布于 2024-09-28 12:18:40 字数 228 浏览 1 评论 0原文

我在没有 InterfaceBuilder 的情况下工作。

我有一个 MKAnnotationView 实例,YES 上有 setDraggable, 在我的 MKMapView 中,显示了我的注释视图,我可以拖放它。

执行放置操作时如何执行方法? 在这种方法中,我需要注释视图的新坐标。

I'm working without InterfaceBuilder.

I've an instance of MKAnnotationView with setDraggable on YES,
In My MKMapView my annotation view is displayed and I can drag and drop it.

How can I execute an method when the drop action is performed?
In this method I need the new coordonates of my annotation view.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

反目相谮 2024-10-05 12:18:40

如果您已使用 setCoordinate 方法正确设置 MKAnnotation 对象,则在 didChangeDragState 方法中,新坐标应该已位于注释对象中:

- (void)mapView:(MKMapView *)mapView 
        annotationView:(MKAnnotationView *)annotationView 
        didChangeDragState:(MKAnnotationViewDragState)newState 
        fromOldState:(MKAnnotationViewDragState)oldState 
{
    if (newState == MKAnnotationViewDragStateEnding)
    {
        CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
        NSLog(@"dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
    }
}

有关参考,请参阅 此处的文档。如果您的应用需要在 4.x 之前的操作系统中运行,则拖动需要更多的手动工作。文档中的链接还提供了一个示例,说明如何在需要时执行此操作。

If you've setup your MKAnnotation object with a setCoordinate method properly, then in the didChangeDragState method, the new coordinate should already be in the annotation object:

- (void)mapView:(MKMapView *)mapView 
        annotationView:(MKAnnotationView *)annotationView 
        didChangeDragState:(MKAnnotationViewDragState)newState 
        fromOldState:(MKAnnotationViewDragState)oldState 
{
    if (newState == MKAnnotationViewDragStateEnding)
    {
        CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
        NSLog(@"dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
    }
}

For reference, see the "Marking Your Annotation View as Draggable" section in the docs here. If your app needs to work in an OS earlier than 4.x, the dragging requires more manual work. The link in the docs also points to an example of how to do that if you need to.

单身情人 2024-10-05 12:18:40

您可能还需要添加以下内容:

if (newState == MKAnnotationViewDragStateStarting) {
    annotationView.dragState = MKAnnotationViewDragStateDragging;
}
else if (newState == MKAnnotationViewDragStateEnding || newState == MKAnnotationViewDragStateCanceling) {
    annotationView.dragState = MKAnnotationViewDragStateNone;
}

因为 MKAnnotationView 不会准确地更改其拖动状态(即使在停止拖动后,这也可能使您的地图与注释一起平移)

you may also want to add the following:

if (newState == MKAnnotationViewDragStateStarting) {
    annotationView.dragState = MKAnnotationViewDragStateDragging;
}
else if (newState == MKAnnotationViewDragStateEnding || newState == MKAnnotationViewDragStateCanceling) {
    annotationView.dragState = MKAnnotationViewDragStateNone;
}

since MKAnnotationView doesn't change its drag state accurately (which could make your map pan with your annotation even after you stop dragging)

热风软妹 2024-10-05 12:18:40

快速解决方案:

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, didChange newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState)
    {
        if (newState == MKAnnotationViewDragState.ending)
        {
            let droppedAt = view.annotation?.coordinate
            print("dropped at : ", droppedAt?.latitude ?? 0.0, droppedAt?.longitude ?? 0.0);
            view.setDragState(.none, animated: true)    
        }
        if (newState == .canceling )
        {
            view.setDragState(.none, animated: true)           
        }
    }

Swift solution :

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, didChange newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState)
    {
        if (newState == MKAnnotationViewDragState.ending)
        {
            let droppedAt = view.annotation?.coordinate
            print("dropped at : ", droppedAt?.latitude ?? 0.0, droppedAt?.longitude ?? 0.0);
            view.setDragState(.none, animated: true)    
        }
        if (newState == .canceling )
        {
            view.setDragState(.none, animated: true)           
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文