将 CLLocation 对象转换为 MKMapPoint 结构

发布于 2025-01-06 08:16:22 字数 404 浏览 1 评论 0原文

我想创建一个 MKPolygon 来显示在 MKMapView 上。我的问题是我不知道该怎么做。

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

我的问题是,我有一个包含 CLLocation 对象的 NSArray ,该对象具有 coordinate.latitudecoordinate.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安穩 2025-01-13 08:16:22

如果您有包含坐标的对象的 NSArray,则使用 polygonWithCooperatives:count: 方法而不是 polygonWithPoints:count: 会更容易。

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

如果您仍想使用polygonWithPoints:count:,可以使用MKMapPointForCooperative函数转换CLLocation<中的coefficient属性/code> 到 MKMapPoint

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

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

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

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

If you have an NSArray of objects containing coordinates, it will be easier to use the polygonWithCoordinates:count: method instead of polygonWithPoints:count:.

The polygonWithCoordinates:count: method accepts a C array of CLLocationCoordinate2D structs. The coordinate property in a CLLocation object is also a CLLocationCoordinate2D.

If you still want to use polygonWithPoints:count:, you can use the MKMapPointForCoordinate function to convert the coordinate property in the CLLocation to an MKMapPoint.

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 call polygonWithCoordinates or polygonWithPoints.

This answer has a code example using polygonWithCoordinates. In that example, you would change the two lines in the for loop to:

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

Don't forget to implement the viewForOverlay delegate method (and make sure the map view's delegate property is set).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文