在 Swift 中使用 CoreLocation 实时测量距离
Measure distance in real time with CoreLocation in Swift
我正在制作一个实时测量速度和距离的应用程序。所以我在测量距离时遇到了问题。我试图通过 return 在函数之外获取位置变量来弄清楚一些事情。但如果我这样做,速度测量就不起作用。那么如何在点击“开始”按钮时开始测量距离,然后在点击“停止”按钮时停止?
@IBAction func startStopButtonDidTouch(_ sender: UIButton) {
if isStarted { //When tapped STOP
timer?.invalidate()
isStarted = false
locationManager.stopUpdatingLocation()
startStopButton.setTitle("START", for: .normal)
} else { //When tapped START
locationManager.startUpdatingLocation()
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimeLabel), userInfo: nil, repeats: true)
isStarted = true
startStopButton.setTitle("STOP", for: .normal)
}
}
func locationManager (_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
speedLabel.text = "\(Int(location.speed * 3.6))"
}
启动位置更新,您将在 didUpdateLocations
方法中获得更新。
此方法可以很好地启动和停止位置更新。
var startLocation:CLLocation!
@IBAction func startStopButtonDidTouch(_ sender: UIButton) {
if isStarted { //When tapped STOP
timer?.invalidate()
isStarted = false
locationManager.stopUpdatingLocation()
startStopButton.setTitle("START", for: .normal)
} else { //When tapped START
locationManager.startUpdatingLocation()
isStarted = true
startStopButton.setTitle("STOP", for: .normal)
}
}
在这里你必须实时更新距离和速度的标签。
func locationManager (_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations[0]
speedLabel.text = "\(Int(location.speed * 3.6))"
if (startLocation == nil)
{
startLocation = location;
}
let endLocation = location;
//Distance measure code will be placed here.
//In KM
let distance = startLocation.distance(from: endLocation) / 1000
}
我正在制作一个实时测量速度和距离的应用程序。所以我在测量距离时遇到了问题。我试图通过 return 在函数之外获取位置变量来弄清楚一些事情。但如果我这样做,速度测量就不起作用。那么如何在点击“开始”按钮时开始测量距离,然后在点击“停止”按钮时停止?
@IBAction func startStopButtonDidTouch(_ sender: UIButton) {
if isStarted { //When tapped STOP
timer?.invalidate()
isStarted = false
locationManager.stopUpdatingLocation()
startStopButton.setTitle("START", for: .normal)
} else { //When tapped START
locationManager.startUpdatingLocation()
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimeLabel), userInfo: nil, repeats: true)
isStarted = true
startStopButton.setTitle("STOP", for: .normal)
}
}
func locationManager (_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
speedLabel.text = "\(Int(location.speed * 3.6))"
}
启动位置更新,您将在 didUpdateLocations
方法中获得更新。
此方法可以很好地启动和停止位置更新。
var startLocation:CLLocation!
@IBAction func startStopButtonDidTouch(_ sender: UIButton) {
if isStarted { //When tapped STOP
timer?.invalidate()
isStarted = false
locationManager.stopUpdatingLocation()
startStopButton.setTitle("START", for: .normal)
} else { //When tapped START
locationManager.startUpdatingLocation()
isStarted = true
startStopButton.setTitle("STOP", for: .normal)
}
}
在这里你必须实时更新距离和速度的标签。
func locationManager (_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations[0]
speedLabel.text = "\(Int(location.speed * 3.6))"
if (startLocation == nil)
{
startLocation = location;
}
let endLocation = location;
//Distance measure code will be placed here.
//In KM
let distance = startLocation.distance(from: endLocation) / 1000
}