2017-07-26 26 views
0

根據文檔GMSMutablePath是:沖銷GMSMutablePath陣列

GMSMutablePath is a dynamic (resizable) array of CLLocationCoordinate2D. [Google Documentation] 

https://developers.google.com/maps/documentation/ios-sdk/reference/interface_g_m_s_mutable_path

我想扭轉的路徑座標順序。通常一個數組,我會用:

[[array123 reverseObjectEnumerator] allObjects]; 

但沒有一個NSArray的功能將在它的工作,如果我嘗試將其轉換爲一個NSArray或NSMutableArray裏我只是得到紅旗。

如何顛倒GMSMutablePath中元素的順序,或者如何正確地將其轉換爲NSArray?

+1

它不是從'NSMutableArray'繼承的對象。你不能這樣做。只需用'replaceCoordinateAtIndex:withCoordinate:'手動執行,用一個簡單的for循環:也許是這樣的:for(NSUInteger i1 = 0; i1 <[myPath count]/2; i1 ++){NSUInteger i2 = [myPath count] -i1; CLLocationCoordinate2D * coord1 = [myPath coordinateAtIndex:i1]; CLLocationCoordinate2D * coord2 = [myPath coordinateAtIndex:i2]; [myPath replaceCoordinateAtIndex:i1 withCoordinate:coord2]; [myPath replaceCoordinateAtIndex:i2 withCoordinate:coord1];}' – Larme

+0

@Larme這很有效。謝謝 – johnsonjp34

回答

1

GMSMutablePath不會從NSMutableArray繼承,因此您不能將它作爲NSMutableArray投射出去。 你可以做什麼手動。如果我們想用NSMutableArray來做,我們可以調用exchangeObjectAtIndex:withObjectAtIndex:,但由於GMSMutablePath似乎沒有提供該方法的等價物,所以我們可以更明確地做到這一點。

for (NSUInteger i1 = 0; i1 < [myPath count]/2; i1 ++) 
{ 
    NSUInteger i2 = [myPath count]-i1; 
    CLLocationCoordinate2D *coord1 = [myPath coordinateAtIndex:i1]; 
    CLLocationCoordinate2D *coord2 = [myPath coordinateAtIndex:i2]; 
    [myPath replaceCoordinateAtIndex:i1 withCoordinate:coord2]; 
    [myPath replaceCoordinateAtIndex:i2 withCoordinate:coord1]; 
}