将CLLocation对象转换为MKMapPoint结构
我想创建一个MKPolygon
以显示在MKMapView
上。我的问题是我无法弄清楚如何去做。将CLLocation对象转换为MKMapPoint结构
我知道要创建一个MKPolygon
我必须创建一堆MKMapPoint
结构并将它们放入一个数组中,并调用类方法polygonWithPoints
。
我的问题是,我有一个NSArray
包含CLLocation
对象具有coordinate.latitude
和coordinate.longitude
属性。
如何将它逐个转换为MKMapPoint
结构体?
如果您有包含坐标的对象的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
属性设置)。