2013-10-09 31 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的問題,如果您將動畫添加到註釋中,它將添加到註釋層而不是關注地圖點。