2012-02-16 82 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属性设置)。