-1

I want to be able to call the locationManager function inside each IBAction function. But I don't know how to deal with all of the locationManager parameters in a function call. How do I deal with all the parameters in a function call?

I tried to do something like this.

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) -> Double {
    ...
    return distance
    }

But I get warnings that it's not in the right form. And then, I don't know how I would call locationManager.

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    let latWork = 39.950230
    let longWork  = -75.158820
    let latHome = 40.005140
    let longHome = -75.210040

    let locationManager = CLLocationManager()

    @IBOutlet weak var distanceTraveledLabel: UILabel!

    @IBOutlet weak var distanceRemainingLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.requestWhenInUseAuthorization()

        locationManager.delegate = self
        locationManager.desiredAccuracy = 
kCLLocationAccuracyHundredMeters
        locationManager.startUpdatingLocation()
    }

    func locationManager(_ manager: CLLocationManager, 
didUpdateLocations locations: [CLLocation]) {
        let location = locations.last!
        let distance = location.distance(from: CLLocation(latitude: 
CLLocationDegrees(latWork), longitude: CLLocationDegrees(longWork)))
        print(distance)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func homeTapped(_ sender: Any) {
        //function that happens when home button is tapped
        print("you tapped home")
        //call locationManager function
    }

@IBAction func workTapped(_ sender: Any) {
        //function that happens when work button is tapped
        print("you tapped work")
        //call locationManager function
    }
}

1 Answers1

0

What you can do is; have two variables to hold latitude and longitude as double (or you can have one variable as location too):

var latitude: Double?
var longitude: Double?

Then update these variables every time location changes in didUpdateLocations method:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if let location = locations.last {
        latitude = location.coordinate.latitude
        longitude = location.coordinate.longitude
    }
}

Then in your action methods, get the location information from those lat, long values like:

//you can access the current location from lat long values, and then calculate distances inside action buttons if you like.
@IBAction func homeTapped(_ sender: Any) {
    //latitude, longitude will be updated with user's current location.

}

@IBAction func workTapped(_ sender: Any) {
    //latitude, longitude will be updated with user's current location.       
}
emrepun
  • 2,496
  • 2
  • 15
  • 33