如何声明MKPolygon

时间:2010-12-03 07:41:19

标签: iphone ios mkmapview

呃伙计们,这看似简单的问题,但显然对我来说太复杂了。我正在尝试创建一个MKPolygon实例并且它不太顺利。这是代码:

MKMapPoint point1 = {38.53607,-121.765793};
 MKMapPoint point2 = {38.537606,-121.768379};
 MKMapPoint point3 = {38.53487,-121.770578};
 NSArray *mapPointArr = [[NSArray alloc] initWithObjects:point1,point2,point3,nil count:3]; //errors here

 MKPolygon *polygon = [MKPolygon polygonWithPoints:mapPointArr count:3];

我在初始化数组的行上遇到了一堆错误(incompatible type for argument 1 ...)。知道什么是错的吗?提前谢谢!

1 个答案:

答案 0 :(得分:5)

MKMapPoint是一个简单的c结构,你不能直接将它添加到objective-c容器中。

在您的情况下,您不需要这样做,因为+polygonWithPoints:不需要NSArray,而是c-array作为第一个参数。创建多边形的正确方法是:

MKMapPoint points[3] = {{38.53607,-121.765793}, {38.537606,-121.768379}, {38.53487,-121.770578}};
MKPolygon *polygon = [MKPolygon polygonWithPoints:points count:3];