我制作了一个演示项目(来自 github 上的 Moving-MKAnnotationView 演示),用于在地图上移动汽车,下面是它的链接
https://github.com/pratikbhiyani/Moving-MKAnnotationView
我根据 vinaut 给出的答案编辑了我的代码,但问题仍然是,当我们缩放或滚动地图动画时,在 ios 7 和 ios 6 中,当我们将地图注释集缩放或滚动到其原始角度一段时间时会分散注意力.
下面是我的演示项目的屏幕截图
这是我更改的一些代码
- (void) setPosition : (id) posValue;
{
NSLog(@"set position");
//extract the mapPoint from this dummy (wrapper) CGPoint struct
MKMapPoint mapPoint = *(MKMapPoint*)[(NSValue*)posValue pointerValue];
CLLocationCoordinate2D coord = MKCoordinateForMapPoint(mapPoint);
CGPoint toPos;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
toPos = [self.mapView convertCoordinate:coord toPointToView:self.mapView];
}
else
{
CGFloat zoomFactor = self.mapView.visibleMapRect.size.width / self.mapView.bounds.size.width;
toPos.x = mapPoint.x/zoomFactor;
toPos.y = mapPoint.y/zoomFactor;
}
[self setTransform:CGAffineTransformMakeRotation([self getHeadingForDirectionFromCoordinate:MKCoordinateForMapPoint(previousPoint) toCoordinate: MKCoordinateForMapPoint(mapPoint)])];
if (MKMapRectContainsPoint(self.mapView.visibleMapRect, mapPoint)) {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.fromValue = [NSValue valueWithCGPoint:self.center];
animation.toValue = [NSValue valueWithCGPoint:toPos];
animation.duration = 1.0;
animation.delegate = self;
animation.fillMode = kCAFillModeForwards;
//[self.layer removeAllAnimations];
[self.layer addAnimation:animation forKey:POSITIONKEY];
//NSLog(@"setPosition ANIMATED %x from (%f, %f) to (%f, %f)", self, self.center.x, self.center.y, toPos.x, toPos.y);
}
self.center = toPos;
previousPoint = mapPoint;
}
我的目标是像在 uber 应用程序中一样移动汽车。