【发布时间】:2017-05-09 07:26:09
【问题描述】:
我希望对 mapkit 当前位置点做出实时反应。我可以使用 LocationManager 以间隔获取位置,但是注释以更流畅的方式移动。我想捕捉这个动作。有没有办法观察点 UIView 的位置或坐标而不是反应,在我的情况下连接到 MKPolyline 端点。
【问题讨论】:
标签: swift swift2 mapkit key-value-observing
我希望对 mapkit 当前位置点做出实时反应。我可以使用 LocationManager 以间隔获取位置,但是注释以更流畅的方式移动。我想捕捉这个动作。有没有办法观察点 UIView 的位置或坐标而不是反应,在我的情况下连接到 MKPolyline 端点。
【问题讨论】:
标签: swift swift2 mapkit key-value-observing
您需要创建自己的用户位置视图,然后向其中添加观察者:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(ClubPinAnnotation *)annotation {
// If the annotation is the user location, create and return our own dot view
if ([annotation isKindOfClass:[MKUserLocation class]]) {
MKAnnotationView *userView = [[MKAnnotationView alloc] initWithFrame:CGRectMake(-5, -5, 10, 10)];
userView.layer.cornerRadius = 5;
userView.backgroundColor = [UIColor blueColor];
// Observe its on-screen position
[userView addObserver:self forKeyPath:@"layer.position" options:NSKeyValueObservingOptionNew context:nil];
return userView;
}
... continue processing the other map annotations
}
然后观察大头针在屏幕上的位置:
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
MKAnnotationView *userView = (MKAnnotationView *)object;
NSLog(@"User view:%@", userView);
}
【讨论】:
动画很可能是一个插值响应。我的猜测,因为我没有研究支持这一点。
您可以估计动画周期,并提供 MkPolyline 作为插值区域上的计算值,作为动画周期中时间的函数。
【讨论】: