要从 获取折线的坐标MKRoute
,请使用getCoordinates:range:
方法。
该方法在继承自的MKMultiPoint
类中。MKPolyline
这也意味着这适用于任何折线——无论它是由您创建还是由MKDirections
.
您分配一个足够大的 C 数组以容纳所需的坐标数并指定范围(例如,从第 0 个开始的所有点)。
例子:
//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);
根据路线,为数百或数千个坐标做好准备。