1

我想创建一个MKPolygon显示在MKMapView. 我的问题是我无法弄清楚如何去做。

我知道要创建一个MKPolygon我必须创建一堆MKMapPoint结构并将它们放入一个数组并调用类方法polygonWithPoints

我的问题是我有一个NSArray包含CLLocation具有 coordinate.latitudecoordinate.longitude属性的对象。

如何将其一一转换为MKMapPoint结构?

4

1 回答 1

3

如果您有一个NSArray包含坐标的对象,则使用该polygonWithCoordinates:count:方法而不是polygonWithPoints:count:.

polygonWithCoordinates:count:方法接受一个 CCLLocationCoordinate2D结构数组。对象中的coordinate属性CLLocation也是CLLocationCoordinate2D.

如果还想使用polygonWithPoints:count:,可以使用MKMapPointForCoordinate函数将 中的coordinate属性转换CLLocationMKMapPoint.

使用任一方法,您首先创建一个适当结构的 C 数组,循环NSArray设置 C 数组中的每个项目。然后调用polygonWithCoordinatespolygonWithPoints

这个答案有一个使用的代码示例polygonWithCoordinates。在该示例中,您可以将for循环中的两行更改为:

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

不要忘记实现viewForOverlay委托方法(并确保delegate已设置地图视图的属性)。

于 2012-02-16T12:55:41.597 回答