理解iOS's
位置概念完全有助于解决有关的任何问题AppleMap
.
要移动地图 Pin 或 AnnotationView ,我们可以使用CoreLocation
输出与用户当前位置相关的数据的框架。
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.startUpdatingHeading()
}
func locationManager(manager: CLLocationManager!, didUpdateHeading heading: CLHeading!) {
// This will print out the direction the device is heading
println(heading.magneticHeading) }
}
}
在上面的例子中,“heading.magneticHeading
" 将输出一个表示设备指向的方向的值。
- 0 表示北
- 90代表东
- 180意味着南
- 270意味着西
- 其间的其他一切
下一步是使用这些值并旋转您的AnnotationView
or Pin
因此。
CGAffineTransformMakeRotation
可以帮忙解决这个问题。
例如,如果您想要旋转到 imageview 以指向东北(这需要 45 度值),您的代码可能如下所示。
float degrees = 45
imageView.transform = CGAffineTransformMakeRotation(degrees * M_PI/180)
请注意CGAffineTransformMakeRotation()
需要一个弧度值,在上面的示例中,我们通过将度数乘以半圆的数量将度数转换为弧度。
以下链接确实很有帮助:
- https://stackoverflow.com/a/7634232/3400991 https://stackoverflow.com/a/7634232/3400991
终于解决了我的问题。希望这个完整的答案也能帮助其他人。