来自MKPolyline的纬度和经度点 [英] latitude and longitude points from MKPolyline
问题描述
我想找到一种方法来获取iOS应用上MKMapView上绘制的MKPolyline的所有纬度和经度点。
I am trying to figure out a way to get all the latitude and longitude points from a MKPolyline drawn on a MKMapView on an iOS app.
我知道MKPolyline不存储纬度和经度点,但我正在寻找一种方法来构建一个lat和long数组,MKPolyline将在地图上触摸。
I know the MKPolyline does not store latitude and longitude points, but I am looking for a way to build an array of lat and long the MKPolyline would touch on the map.
任何人都有具体可能解决方案吗?
Anybody has specific possible solutions on this?
谢谢
编辑:
看到第一个回复后(谢谢) )我想我需要更好地解释我的代码在做什么:
After seeing the first response (thank you) I think I need to explain better what my code is doing:
- 首先我打电话给 calculateDirectionsWithCompletionHandler 一个MKDirections对象
- 我找回 MKRoute 对象,该对象具有折线属性。
- 然后我在mapview上调用 addOverlay 从MKRoute传递折线对象
- first I call "calculateDirectionsWithCompletionHandler" on a MKDirections object
- I get back MKRoute object which has a "polyline" property.
- then I call "addOverlay" on the mapview passing in the polyline from the MKRoute object
这就是全部。
所以,我已经为我建了一条折线。所以我想以某种方式得到折线中找到的所有点并将它们映射到纬度和长度......
So, I already have a polyline built for me. So I would like to get all the points found in the polyline somehow and map them to a lat and long...
推荐答案
要从 MKRoute
获取折线的坐标,请使用 getCoordinates:range:
方法。
该方法位于 MKMultiPoint
类中, MKPolyline
继承自。
To get coordinates of the polyline from an MKRoute
, use the getCoordinates:range:
method.
That method is in the MKMultiPoint
class which MKPolyline
inherits from.
这也意味着这适用于任何折线 - 无论是由您创建还是由 MKDirections
创建。
That also means this works for any polyline -- whether it was created by you or by MKDirections
.
你分配一个足够大的C数组来保存你想要的坐标数并指定范围(例如从0开始的所有点)。
You allocate a C array big enough to hold the number of coordinates you want and specify the range (eg. all points starting from the 0th).
示例:
//route is the MKRoute in this example
//but the polyline can be any MKPolyline
NSUInteger pointCount = route.polyline.pointCount;
//allocate a C array to hold this many points/coordinates...
CLLocationCoordinate2D *routeCoordinates
= malloc(pointCount * sizeof(CLLocationCoordinate2D));
//get the coordinates (all of them)...
[route.polyline getCoordinates:routeCoordinates
range:NSMakeRange(0, pointCount)];
//this part just shows how to use the results...
NSLog(@"route pointCount = %d", pointCount);
for (int c=0; c < pointCount; c++)
{
NSLog(@"routeCoordinates[%d] = %f, %f",
c, routeCoordinates[c].latitude, routeCoordinates[c].longitude);
}
//free the memory used by the C array when done with it...
free(routeCoordinates);
根据路线,准备数百或数千个坐标。
Depending on the route, be prepared for hundreds or thousands of coordinates.
这篇关于来自MKPolyline的纬度和经度点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!