2012-02-16 63 views
1

我想創建一個MKPolygon以顯示在MKMapView上。我的問題是我無法弄清楚如何去做。將CLLocation對象轉換爲MKMapPoint結構

我知道要創建一個MKPolygon我必須創建一堆MKMapPoint結構並將它們放入一個數組中,並調用類方法polygonWithPoints

我的問題是,我有一個NSArray包含CLLocation對象具有coordinate.latitudecoordinate.longitude屬性。

如何將它逐個轉換爲MKMapPoint結構體?

回答

2

如果您有包含座標的對象的NSArray,則使用polygonWithCoordinates:count:方法而不是polygonWithPoints:count:會更容易。

polygonWithCoordinates:count:方法接受一個CLLocationCoordinate2D結構的C數組。 CLLocation對象中的coordinate屬性也是CLLocationCoordinate2D

如果你仍然想使用polygonWithPoints:count:,您可以使用MKMapPointForCoordinate功能在CLLocationcoordinate財產轉換爲MKMapPoint

使用這兩種方法之一,首先創建一個適當結構的C數組,循環遍歷NSArray來設置C數組中的每個項。然後致電polygonWithCoordinatespolygonWithPoints

This answer有一個使用polygonWithCoordinates的代碼示例。在這個例子中,你會改變兩行for環路:

CLLocation *coordObj = (CLLocation *)[coordinateData objectAtIndex:i]; 
coords[i] = coordObj.coordinate; 

不要忘了執行viewForOverlay委託方法(並確保地圖視圖的delegate屬性設置)。