Swift 当用户驾驶汽车时,速度标签值不变
我不熟悉斯威夫特。。我已经实现了谷歌地图sdk,我还想显示用户开始移动或驾驶其汽车时的速度 我正在使用CLLocationSpeed并将其存储在变量中 现在,当用户单击开始按钮进行导航时,我得到了速度值,但它不会随着用户的移动而改变。 我想让它更有活力 我已经附上了相同标签的代码和图像 这是使其更具动态性的正确方法,还是有任何方法可以在用户移动时更改速度标签 感谢您的帮助Swift 当用户驾驶汽车时,速度标签值不变,swift,xcode,cllocationmanager,gmsmapview,Swift,Xcode,Cllocationmanager,Gmsmapview,我不熟悉斯威夫特。。我已经实现了谷歌地图sdk,我还想显示用户开始移动或驾驶其汽车时的速度 我正在使用CLLocationSpeed并将其存储在变量中 现在,当用户单击开始按钮进行导航时,我得到了速度值,但它不会随着用户的移动而改变。 我想让它更有活力 我已经附上了相同标签的代码和图像 这是使其更具动态性的正确方法,还是有任何方法可以在用户移动时更改速度标签 感谢您的帮助 感谢并问候无需创建单独的CLLocationSpeed变量来获取速度更新 你可以这样做 class VC: UIViewC
感谢并问候无需创建单独的
CLLocationSpeed
变量来获取速度
更新
你可以这样做
class VC: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var speedlabel: UILabel!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let speed = manager.location?.speed {
self.speedlabel.text = String(describing: speed)
}
}
}
上面是示例代码。根据您的要求修改它。无需创建单独的
CLLocationSpeed
变量来获取speed
更新
func getUserSpeed() {
var speed: CLLocationSpeed = CLLocationSpeed()
guard let userSpeed = locationManager.location?.speed else { return }
speed = userSpeed
if speed < 0 { speed = 0 }
speedLabel.text = String(format: "%.0f km/h", speed * 3.6)
}
你可以这样做
class VC: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var speedlabel: UILabel!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let speed = manager.location?.speed {
self.speedlabel.text = String(describing: speed)
}
}
}
上面是示例代码。根据需要修改它。func getUserSpeed(){
func getUserSpeed() {
var speed: CLLocationSpeed = CLLocationSpeed()
guard let userSpeed = locationManager.location?.speed else { return }
speed = userSpeed
if speed < 0 { speed = 0 }
speedLabel.text = String(format: "%.0f km/h", speed * 3.6)
}
变量速度:CLLocationSpeed=CLLocationSpeed()
guard let userSpeed=locationManager.location?.speed else{return}
速度=用户速度
如果速度<0{speed=0}
speedLabel.text=字符串(格式:“%.0f km/h”,速度*3.6)
}
func getUserSpeed(){
变量速度:CLLocationSpeed=CLLocationSpeed()
guard let userSpeed=locationManager.location?.speed else{return}
速度=用户速度
如果速度<0{speed=0}
speedLabel.text=字符串(格式:“%.0f km/h”,速度*3.6)
}
timerspeed声明在哪里?在速度标签下面..查看我的答案。timerspeed声明在哪里?在速度标签下面..查看我的答案。它会保持刷新吗??随着用户的移动?或者它会保持静态??您好,我尝试了您的代码示例,但当我单击退出按钮并再次启动导航时,标签没有显示,这意味着速度变量设置为零。为什么您首先要创建速度变量?它会保持刷新吗??随着用户的移动?还是会保持静态?您好,我尝试了您的代码示例,但当我单击退出按钮并再次启动导航时,标签未显示,这意味着速度变量设置为零。为什么您首先要创建速度变量?虽然此代码可能解决了问题,如何以及为什么解决这个问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。虽然此代码可能会解决此问题,但如何以及为什么解决此问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。