如果您有包含座標的對象的NSArray
,則使用polygonWithCoordinates:count:
方法而不是polygonWithPoints:count:
會更容易。
polygonWithCoordinates:count:
方法接受一個CLLocationCoordinate2D
結構的C數組。 CLLocation
對象中的coordinate
屬性也是CLLocationCoordinate2D
。
如果你仍然想使用polygonWithPoints:count:
,您可以使用MKMapPointForCoordinate
功能在CLLocation
的coordinate
財產轉換爲MKMapPoint
。
使用這兩種方法之一,首先創建一個適當結構的C數組,循環遍歷NSArray
來設置C數組中的每個項。然後致電polygonWithCoordinates
或polygonWithPoints
。
This answer有一個使用polygonWithCoordinates
的代碼示例。在這個例子中,你會改變兩行for
環路:
CLLocation *coordObj = (CLLocation *)[coordinateData objectAtIndex:i];
coords[i] = coordObj.coordinate;
不要忘了執行viewForOverlay
委託方法(並確保地圖視圖的delegate
屬性設置)。