拖动时获取MKAnnotation的坐标 [英] Obtaining MKAnnotation's coordinate while dragging

查看:128
本文介绍了拖动时获取MKAnnotation的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据用户添加的注释的位置创建路径(MKPolyline).我想允许用户通过拖动图钉来更改路径.我目前可以做到这一点,但是MKPolyline直到销钉掉落才更新.

I'm creating a path (MKPolyline) based on the position of annotations added by the user. I want to allow the users to change the path by dragging the pins. I currently can do that, but the MKPolyline doesn't update until the pin is dropped.

我为MKAnnotationViewDragStateDragging实现了- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState,但它仅提醒我用户正在拖动图钉,而没有通知新位置.

I implemented - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState for the MKAnnotationViewDragStateDragging but it only alerts me that the user is dragging the pin and not notifing about the new position.

每次拖动时,如何获取要拖动的注释的当前位置?我希望在拖动时收到有关位置发生任何变化的通知,以便能够更新MKPolyline以跟随销的移动,从而更好地反映路径的变化.

How can I obtain the current position of the annotation being dragg, every time it changes? I want to be notified about any change in position while dragging to be able to update the MKPolyline to follow the pin as it moves, to better reflect how the path is changing.

有什么想法吗?谢谢!

推荐答案

创建MKAnnotationView的子类,并将该子类用于要拖动的注释.在该子类中:

Create a subclass of MKAnnotationView and use that subclass for the annotation you are dragging. Within that subclass:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    CLLocationCoordinate2D location = [_mapView convertPoint:self.center toCoordinateFromView:self.superview];
}

不幸的是,与MKAnnotationView关联的MKAnnotation对象在删除MKAnnotationView之前不会更新其坐标.因此,您必须使用MKAnnotationView本身的位置,并将其转换为有效坐标.

Unfortunately, the MKAnnotation object associated with the MKAnnotationView does not update its coordinate until the MKAnnotationView is dropped. So you have to use the position of the MKAnnotationView itself, and convert that to a valid coordinate.

这还需要引用已将注释添加到的MKMapView(此处为_mapView),您可以在创建视图时将其传递给视图.

This also requires a reference to the MKMapView that the annotation has been added to (_mapView here), which you could pass to the view when it is created.

如果已设置注释视图的centerOffset属性,则还需要考虑以下内容:

If you have set the centerOffset property of the annotation view, you will also need to account for that:

CLLocationCoordinate2D location = [_mapView convertPoint:CGPointMake(self.center.x - self.centerOffset.x, self.center.y - self.centerOffset.y) toCoordinateFromView:self.superview];

这篇关于拖动时获取MKAnnotation的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆