2014-03-19 46 views
4

如何添加弧形視覺元素併爲其設置動畫效果,我已經在mapkit中創建了在MapKit中沿着一條弧線創建視覺元素

以下代碼將在兩點之間創建一個不錯的弧。想象一下,一個動畫視覺將代表一架沿着這條弧線飛行的飛機。

-(void)addArc 
{ 
    CLLocationCoordinate2D sanFrancisco = { 37.774929, -122.419416 }; 
    CLLocationCoordinate2D newYork = { 40.714353, -74.005973 }; 
    CLLocationCoordinate2D pointsArc[] = { sanFrancisco, newYork }; 
    // 
    MKGeodesicPolyline *geodesic; 
    geodesic = [MKGeodesicPolyline polylineWithCoordinates:&pointsArc[0] 
                count:2]; 
    // 
    [self.mapView addOverlay:geodesic]; 
} 

enter image description here

+0

這什麼都沒有做地圖包。你可以從這裏開始閱讀:[核心動畫編程指南](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html) – Desdenova

+2

實際上它的確如此,動畫應該綁定到mapview,當用戶放大和縮小動畫時,應該是真實的地圖上的路徑......我想到的是一個註釋,它經常用一個定時器來改變,但它看起來雜亂無章。無論我不希望擁有與地圖本身斷開連接的動畫的頂層。 – chewy

+0

你需要頂層,你會手動綁定到地圖上。沒有方便的方法可以在地圖工具包中使用。 – Desdenova

回答

5

註釋可能是實際上最好的選擇。使用可指定的座標屬性定義註記類(或使用MKPointAnnotation)。

令人驚訝的是,該MKGeodesicPolyline類是實物足以供應其計算通過points財產(給MKMapPoint S)或getCoordinates:range:方法(給CLLocationCoordinate2D S)來創建弧各個點。

(實際上,該屬性和方法都在MKMultiPointMKPolyline是的子類,MKGeodesicPolylineMKPolyline一個子類。)

上的計時器和地圖視圖只要更新所述註釋的coordinate屬性將自動移動註釋。

注意:對於這麼長的弧線,會有數千個點。

這裏是一個非常簡單的,原油使用points財產例子,performSelector:withObject:afterDelay:(更容易比getCoordinates:range:方法使用):

//declare these ivars: 
MKGeodesicPolyline *geodesic; 
MKPointAnnotation *thePlane; 
int planePositionIndex; 

//after you add the geodesic overlay, initialize the plane: 
thePlane = [[MKPointAnnotation alloc] init]; 
thePlane.coordinate = sanFrancisco; 
thePlane.title = @"Plane"; 
[mapView addAnnotation:thePlane]; 

planePositionIndex = 0; 
[self performSelector:@selector(updatePlanePosition) withObject:nil afterDelay:0.5]; 

-(void)updatePlanePosition 
{ 
    //this example updates the position in increments of 50... 
    planePositionIndex = planePositionIndex + 50; 

    if (planePositionIndex >= geodesic.pointCount) 
    { 
     //plane has reached end, stop moving 
     return; 
    } 

    MKMapPoint nextMapPoint = geodesic.points[planePositionIndex]; 

    //convert MKMapPoint to CLLocationCoordinate2D... 
    CLLocationCoordinate2D nextCoord = MKCoordinateForMapPoint(nextMapPoint); 

    //update the plane's coordinate... 
    thePlane.coordinate = nextCoord; 

    //schedule the next update...  
    [self performSelector:@selector(updatePlanePosition) withObject:nil afterDelay:0.5]; 
}