如果您有包含坐标的对象的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
属性设置)。