2011-10-27 110 views
3

我在我的應用程序中顯示了2種註釋的地圖:位置和位置羣集。當我放大某個羣集時,羣集將展開以顯示羣集中包含的位置。將這些位置添加到映射中時,其父集羣的座標將存儲在註釋對象中。我想要做的就是製作一個動畫,以便在添加這些位置時,從他們的母集羣的位置展開。這裏是我的MapView代碼:didAddAnnotationViews:緯度/長度座標到屏幕位置的奇怪翻譯

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views 
{ 
    MKAnnotationView *aV; 
    for (aV in views) 
    { 
     if (([aV.annotation isKindOfClass:[PostLocationAnnotation class]]) && (((PostLocationAnnotation *)aV.annotation).hasParent)) 
     { 
      CLLocationCoordinate2D startCoordinate = ((PostLocationAnnotation *)aV.annotation).parentCoordinate; 

      CGPoint startPoint = [ffMapView convertCoordinate:startCoordinate toPointToView:self.view]; 

      CGRect endFrame = aV.frame; 

      aV.frame = CGRectMake(startPoint.x, startPoint.y, aV.frame.size.width, aV.frame.size.height); 

      [UIView beginAnimations:nil context:NULL]; 
      [UIView setAnimationDuration:1.00]; 
      [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
      [aV setFrame:endFrame]; 
      [UIView commitAnimations]; 

      ((PostLocationAnnotation *)aV.annotation).hasParent = NO; 
     } 
    } 
} 

這似乎使地圖點從某個位置飛到遠遠超出視圖的焦點。通過調試,我發現endFrame.origin和startPoint的值有很大差異 - 對於一個特定的aV,endFrame.origin以(32657,21781)(兩個CGFloats)和startPoint出現爲(159.756256,247.213226 )(同樣,都是CGFloats)。我假設endFrame.origin是正確的值,因爲點在我想要的地方結束,他們只是從遠處的某個地方來。我在代碼的很多其他部分使用了convertCoordinate:toPointToView:方法,並沒有任何問題。我也試着用不同的值乘以startPoint的X和Y值,但是對於startPoint的每個值,單個係數都不成立。任何想法發生了什麼?

回答

2

註解視圖的框架似乎基於當前的縮放級別,然後偏離屏幕座標。爲了彌補這一點,用annotationVisibleRect.origin抵消了startPoint

此外,當調用convertCoordinate:toPointToView:時,我認爲轉換爲地圖視圖的框架而不是self.view更安全,因爲地圖視圖的大小可能與容器視圖的大小不同。

請嘗試以下變化:

CGPoint startPoint = [ffMapView convertCoordinate:startCoordinate 
            toPointToView:ffMapView]; 
//BTW, using the passed mapView parameter instead of referencing the ivar 
//would make the above line easier to re-use. 

CGRect endFrame = aV.frame; 

aV.frame = CGRectMake(startPoint.x + mapView.annotationVisibleRect.origin.x, 
         startPoint.y + mapView.annotationVisibleRect.origin.y, 
         aV.frame.size.width, 
         aV.frame.size.height); 
+0

它的工作原理!謝謝。我一整天都被困在這裏 – benwad