Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

location constantly updating problems iOS Swift

I am getting the users current location and dropping this as a println(). The idea is that I am going to hit a button to get the new location, however currently the app keeps updating constantly (every second) instead. I have tried moving the getLocation() function inside my IBAction but that crashed the thing. I have updated the info.plist so thats not a problem. Heres le code:

    import UIKit
import CoreLocation


class ViewController: UIViewController, CLLocationManagerDelegate{

    @IBOutlet var latitude : UILabel!
    @IBOutlet var longitude : UILabel!

    var locationManager = CLLocationManager()
    var startLocation: CLLocation!


    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager = CLLocationManager()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()

    }

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

    @IBAction func findMyLocation(sender: AnyObject){
        startLocation = nil
        locationManager.startUpdatingLocation()

    }

    func getLocation(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!){

        var userLocation:AnyObject = locations[0] as! CLLocation

        var strlat = String(format: "%.4f", userLocation.coordinate.latitude)
        var strlong = String(format: "%.4f",userLocation.coordinate.longitude)

        latitude.text = String(format: "%.4f", userLocation.coordinate.latitude)
        longitude.text = String(format: "%.4f",userLocation.coordinate.longitude)

        println("latitude: " + strlat)
        println("longitide: " + strlong)

        if startLocation == nil {
            startLocation = userLocation as! CLLocation
            locationManager.stopUpdatingLocation()
        }



    }

    func locationManager(manager: CLLocationManager!,
        didFailWithError error: NSError!) {

    }
}
like image 769
Saucepan Avatar asked Feb 09 '23 09:02

Saucepan


1 Answers

Move the locationManager.startUpdatingLocation() to your findMyLocation function. This will start the locationManager when your button is pressed and begin calling the didUpdateLocations Inside your if startLocation == nil add locationManager.stopUpdatingLocation() this will stop the locationManager after you have set your startLocation var. Every time the user presses the button the process will run again.

One additional note, you should add more code into the didUpdateLocations to check the accuracy and timestamp of the location before you use it as it may not be a valid/accurate location for what you are trying to do.

UPDATE:

Had a chance to validate and the code will work with the changes suggested. Here is what your final code should look like. I am also assuming you have set your plist entries for locations services and your simulator is set to simulate locaitons.

import UIKit
import CoreLocation


class ViewController: UIViewController, CLLocationManagerDelegate{

    @IBOutlet var latitude : UILabel!
    @IBOutlet var longitude : UILabel!

    var locationManager : CLLocationManager! = CLLocationManager()
    var startLocation: CLLocation!


    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()

    }

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

    @IBAction func findMyLocation(sender: AnyObject){
        startLocation = nil
        locationManager.startUpdatingLocation()

    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!){

        var userLocation:AnyObject = locations[0] as! CLLocation

        var strlat = String(format: "%.4f", userLocation.coordinate.latitude)
        var strlong = String(format: "%.4f",userLocation.coordinate.longitude)

        latitude.text = String(format: "%.4f", userLocation.coordinate.latitude)
        longitude.text = String(format: "%.4f",userLocation.coordinate.longitude)

        println("latitude: " + strlat)
        println("longitide: " + strlong)

        if startLocation == nil {
            startLocation = userLocation as! CLLocation
            locationManager.stopUpdatingLocation()
        }



    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {

        println("error with location manager: " + error.description)

    }


}
like image 166
rmp Avatar answered Feb 20 '23 10:02

rmp