gpt4 book ai didi

iphone - 拖动时获取MKAnnotation的坐标

转载 作者:搜寻专家 更新时间:2023-10-30 20:00:18 26 4
gpt4 key购买 nike

我正在根据用户添加的注释位置创建路径 (MKPolyline)。我想让用户通过拖动引脚来更改路径。我目前可以做到这一点,但 MKPolyline 不会更新,直到引脚被删除。

我实现了 - (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)annotationView
didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState
用于 MKAnnotationViewDragStateDragging 但它只提醒我用户正在拖动图钉而不通知新位置。

每次更改时如何获取被拖动的注释的当前位置?我希望在拖动时收到有关位置变化的通知,以便能够更新 MKPolyline 以跟随图钉移动,从而更好地反射(reflect)路径的变化。

有什么想法吗?谢谢!

最佳答案

创建 MKAnnotationView 的子类并将该子类用于您正在拖动的注释。在该子类中:

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

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

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

如果您设置了注释 View 的 centerOffset 属性,您还需要考虑到这一点:

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

关于iphone - 拖动时获取MKAnnotation的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16626641/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com