2013-10-09 30 views
25

我做了一个演示项目(移动-MKAnnotationView在github上演示)用于在地图上的下行驶的汽车是其链接汽车(译注)动画(如Uber应用)不工作

https://github.com/pratikbhiyani/Moving-MKAnnotationView

编辑我的代码基于给定答案的vinaut,但仍然问题是,当我们缩放或滚动地图动画时,我们缩放或滚动地图注释集合到原始角度一段时间,在ios 7和ios 6中分散注意力。

下面是我的演示项目

enter image description here

下面的屏幕截图是一些代码,我改变

- (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应用。

+0

那么问题是什么?移动地图时,注释消失了吗? –

+2

是的。并没有显示出它在ios 6中显示的流畅动画; –

+0

尝试使用翻译变换而不是重新定位... – k06a

回答

13

似乎与CLCoordinate2D/MKMapPoint/CGPoint转换功能改变的东西......

Detecting a point in a MKPolygon broke with iOS7 (CGPathContainsPoint)

注释消失,因为beetween MkMapPoints和CGIPoints转换不工作了,如果您登录CALayer的“位置”,你将会看到点外的景象。不知道为什么它在做触摸事件时有效。

如果更改的功能:

- (void) setPosition : (id) posValue; 
{ 
    //extract the mapPoint from this dummy (wrapper) CGPoint struct 
    MKMapPoint mapPoint = *(MKMapPoint*)[(NSValue*)posValue pointerValue]; 
    CLLocationCoordinate2D coord = MKCoordinateForMapPoint(mapPoint); 

    CGPoint toPos = [self.mapView convertCoordinate:coord toPointToView:self.mapView]; 


    if (MKMapRectContainsPoint(self.mapView.visibleMapRect, mapPoint)) { 

     CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; 

     animation.fromValue = [NSValue valueWithCGPoint:self.center]; 
     animation.toValue = [NSValue valueWithCGPoint:toPos]; 
     animation.duration = 0.8; 
     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; 


} 

应该可以正常工作了。

+0

嘿谢谢你的回答,但是你的代码的问题是当地图移动时,注释会从路径中分散注意力。 –

+0

我不知道它应该如何在iOS 6中表现出来。我指出了它没有显示注释的原因,您应该能够根据您认为合适的方式修改代码。例如,如果您不希望在平移或缩放时更新注释,只需使用一个属性来跟踪当前的mapView.visibleRect并将其与setPosition中的新值进行比较,并且如果已更改,则不要运行该动画。给它一个镜头,让我知道你是否成功。 – vinaut

+0

在ios 6中,它在平移或缩放时不会分散路径。我无法阻止动画。它负责顺利驾驶。 –

14

我是Moving-MKAnnotationView(https://github.com/100grams/Moving-MKAnnotationView.git)的原始贡献者。这个组件最初是使用iOS4.3编写的,并且自那以后发生了很大变化。 :-)

这里的根本原因是从MKMapPoint到CGPoint(屏幕坐标)的转换。虽然代码工作之前,它打破了对iOS7,我用这个来转换固定它的纬度/经度坐标到屏幕坐标:

convertCoordinate:toPointToView: 

我已经提交了此修复程序,与其他一些更新,以https://github.com/100grams/Moving-MKAnnotationView.git所以它现在可以在iOS7/Xcode5上运行。

+0

感谢您的回复。它非常好的演示。但问题是,当Group.duration = 0.2,当我将持续时间增加到1.0以获得流畅的动画时,注释会在缩小或缩放地图时从路径分散注意力。 –

+0

是的,这确实是一个警告。我想你应该在地图平移/缩放时中止/停止任何动画,以避免这种副作用。 – 100grams

+0

是的,我试图删除动画,但不幸的是它显示相同的分心:( –

1

缩放/滚动地图时分散汽车的问题。实际上,通过向注释添加动画是无法实现的。我发现了Interpolate function,我可以通过它获取“From”和“To”坐标之间的位置并将其设置为注释(以毫秒为单位的设置注释坐标)将看起来像动画。

这不是iOS或Map的问题,如果您将动画添加到注释中,它将添加到注释层而不是关注地图点。