import UIKit
import CoreLocation
class ViewController: UIViewController {
var lastLoc: CLLocation?
lazy var locationM: CLLocationManager = {
let locationM = CLLocationManager()
locationM.delegate = self
if #available(iOS 8.0, *) {
locationM.requestAlwaysAuthorization()
}
locationM.desiredAccuracy = kCLLocationAccuracyBest
return locationM
}()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
locationM.startUpdatingLocation()
}
}
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let newLocation = locations.last else {return}
if newLocation.horizontalAccuracy < 0 { return }
// 场景演示:打印当前用户的行走方向,偏离角度以及对应的行走距离,例如:”北偏东 30度 方向,移动了 8米”
// 1. 获取当前的行走航向
l
swift 地图定位(四)
最新推荐文章于 2021-10-23 18:24:21 发布
本文展示了如何使用Swift进行地图定位,跟踪用户行走方向和移动距离。通过CLLocationManager获取定位授权,设置精度,并监听位置更新。当位置改变时,计算行走方向(如北偏东、南偏西等),并输出移动的精确距离。同时处理定位失败的情况。
摘要由CSDN通过智能技术生成