我想创建一个MKPolygon
显示在MKMapView
. 我的问题是我无法弄清楚如何去做。
我知道要创建一个MKPolygon
我必须创建一堆MKMapPoint
结构并将它们放入一个数组并调用类方法polygonWithPoints
。
我的问题是我有一个NSArray
包含CLLocation
具有 coordinate.latitude
和coordinate.longitude
属性的对象。
如何将其一一转换为MKMapPoint
结构?
我想创建一个MKPolygon
显示在MKMapView
. 我的问题是我无法弄清楚如何去做。
我知道要创建一个MKPolygon
我必须创建一堆MKMapPoint
结构并将它们放入一个数组并调用类方法polygonWithPoints
。
我的问题是我有一个NSArray
包含CLLocation
具有 coordinate.latitude
和coordinate.longitude
属性的对象。
如何将其一一转换为MKMapPoint
结构?
如果您有一个NSArray
包含坐标的对象,则使用该polygonWithCoordinates:count:
方法而不是polygonWithPoints:count:
.
该polygonWithCoordinates:count:
方法接受一个 CCLLocationCoordinate2D
结构数组。对象中的coordinate
属性CLLocation
也是CLLocationCoordinate2D
.
如果还想使用polygonWithPoints:count:
,可以使用MKMapPointForCoordinate
函数将 中的coordinate
属性转换CLLocation
为MKMapPoint
.
使用任一方法,您首先创建一个适当结构的 C 数组,循环NSArray
设置 C 数组中的每个项目。然后调用polygonWithCoordinates
或polygonWithPoints
。
这个答案有一个使用的代码示例polygonWithCoordinates
。在该示例中,您可以将for
循环中的两行更改为:
CLLocation *coordObj = (CLLocation *)[coordinateData objectAtIndex:i];
coords[i] = coordObj.coordinate;
不要忘记实现viewForOverlay
委托方法(并确保delegate
已设置地图视图的属性)。