将 CLLocation 对象转换为 MKMapPoint 结构
我想创建一个 MKPolygon
来显示在 MKMapView
上。我的问题是我不知道该怎么做。
我知道要创建一个 MKPolygon
,我必须创建一堆 MKMapPoint
结构并将它们放入一个数组中,然后调用类方法 polygonWithPoints
。
我的问题是,我有一个包含 CLLocation
对象的 NSArray
,该对象具有 coordinate.latitude
和 coordinate.longitude
属性。
如何将其一一转换为 MKMapPoint
结构体?
I would like to create a MKPolygon
to display on a MKMapView
. My problem is that I'm not able to figure out how to do it.
I know that to create an MKPolygon
I have to create a bunch of MKMapPoint
structs and put them into an array and call the class method polygonWithPoints
.
My problem is that I have an NSArray
containing CLLocation
objects that have coordinate.latitude
and coordinate.longitude
properties.
How do I convert it one by one to an MKMapPoint
struct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有包含坐标的对象的
NSArray
,则使用polygonWithCooperatives:count:
方法而不是polygonWithPoints:count:
会更容易。polygonWithCoordinates:count:
方法接受CLLocationCooperative2D
结构的 C 数组。CLLocation
对象中的coordinate
属性也是一个CLLocationCooperative2D
。如果您仍想使用
polygonWithPoints:count:
,可以使用MKMapPointForCooperative
函数转换CLLocation<中的
coefficient
属性/code> 到MKMapPoint
。使用任一方法,您首先创建适当结构的 C 数组,循环遍历 NSArray 以设置 C 数组中的每个项目。然后调用
polygonWithCooperatives
或polygonWithPoints
。此答案有一个使用
polygonWithCooperatives
的代码示例。在该示例中,您可以将for
循环中的两行更改为:不要忘记实现
viewForOverlay
委托方法(并确保地图视图的delegate
属性已设置)。If you have an
NSArray
of objects containing coordinates, it will be easier to use thepolygonWithCoordinates:count:
method instead ofpolygonWithPoints:count:
.The
polygonWithCoordinates:count:
method accepts a C array ofCLLocationCoordinate2D
structs. Thecoordinate
property in aCLLocation
object is also aCLLocationCoordinate2D
.If you still want to use
polygonWithPoints:count:
, you can use theMKMapPointForCoordinate
function to convert thecoordinate
property in theCLLocation
to anMKMapPoint
.With either method, you first create a C array of the appropriate struct, loop through the
NSArray
to set each item in the C array. Then callpolygonWithCoordinates
orpolygonWithPoints
.This answer has a code example using
polygonWithCoordinates
. In that example, you would change the two lines in thefor
loop to:Don't forget to implement the
viewForOverlay
delegate method (and make sure the map view'sdelegate
property is set).