Search code examples
iosswiftperformancedistancecllocation

Updating speed only after button tapped in Swift


I'm making an app that measures speed and distance in real time. so before did that app starts updating location when button tapped. but this method takes time after button tapped. if i will put locationManager.startUpdatingLocation in viewDidLoad, speed and distance starts measure immediately without tapping start button. How can I do like speed and distance starts measure immediately ONLY WHEN when start button tapped? here's my code.

  let locationManager = CLLocationManager()
   override func viewDidLoad() {
          super.viewDidLoad()

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    
 @IBAction func startStopButtonDidTouch(_ sender: UIButton) {
       
        if isStarted { //When tapped STOP
        locationManager.stopUpdatingLocation()
        startStopButton.setTitle("START", for: .normal)
    } else { //When tapped START
        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;

    let distance = startLocation.distance(from: endLocation) / 1000 //m->km
    distanceLabel.text = "\(String(format: "%.1f", distance)) km"
}

Solution

  • I'm not sure this is the best solution, but you might use an extra variable to make sure that when you are getting the location on app start, the speed and distance calculations should not be done. Something like this:

    let locationManager = CLLocationManager()
       override func viewDidLoad() {
             super.viewDidLoad()
    
            isToPerformCalculations = false
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.requestWhenInUseAuthorization()
            locationManager.startUpdatingLocation()
        
     @IBAction func startStopButtonDidTouch(_ sender: UIButton) {
           
         if isStarted { //When tapped STOP
           // locationManager.stopUpdatingLocation()
            startStopButton.setTitle("START", for: .normal)
        } else { //When tapped START
            isToPerformCalculations = true
            startStopButton.setTitle("STOP", for: .normal)
        }
      }
        
        func locationManager (_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])   {
        let location = locations[0]
        locationManager.stopUpdatingLocation()
        if isToPerformCalculations {
            if (startLocation == nil) {
               startLocation = location;
            }
            let endLocation = location;
    
        
             speedLabel.text = "\(Int(location.speed * 3.6))"
             let distance = startLocation.distance(from: endLocation) / 1000 //m->km
             distanceLabel.text = "\(String(format: "%.1f", distance)) km"
        }
    }
    

    I also changed where you stopUpdatingLocation, see if this works for you.