我正在尝试打印所有路线步骤的坐标,类似于 Google Maps SDK 的“腿”。
但是它告诉我不能使用polyline
属性来获取坐标?
最佳答案
试试这个:
for step in self.route!.steps as [MKRouteStep] {
否则它将 step
视为 AnyObject
(它没有定义 polyline
属性,因此您会收到编译器错误)。
顺便说一下,请注意
polyline.coordinate
只是给出折线或一个端点的平均中心。一条折线可以有多个线段。
如果您需要获取所有沿折线的线段和坐标,请参阅latitude and longitude points from MKPolyline ( objective-C )。
这是一种可能的 Swift 翻译(在 this answer 的帮助下):
for step in route!.steps as [MKRouteStep] {
let pointCount = step.polyline.pointCount
var cArray = UnsafeMutablePointer<CLLocationCoordinate2D>.alloc(pointCount)
step.polyline.getCoordinates(cArray, range: NSMakeRange(0, pointCount))
for var c=0; c < pointCount; c++ {
let coord = cArray[c]
println("step coordinate[\(c)] = \(coord.latitude),\(coord.longitude)")
}
cArray.dealloc(pointCount)
}
正如第一个链接答案所警告的那样,根据路线,您每一步可能会获得成百上千个坐标。
关于ios - MapKit Step折线在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28643554/