4

I've tried following simple CoreLocation examples using Swift, but I'm unable to get things working. I've extracted most of the code out, so it's just barebones in order to focus the question, and I'm hoping the answer jumps out quickly to someone.

import UIKit

import CoreLocation


class ViewController: UIViewController,CLLocationManagerDelegate {
@IBOutlet weak var txtLatitude: UITextField!
@IBOutlet weak var txtLongitude: UITextField!

@IBOutlet weak var cmdLocateMe: UIButton!
@IBOutlet weak var slider: UISlider!
@IBOutlet weak var txtSlider: UITextField!
@IBOutlet weak var power: UISwitch!
var locationManager: CLLocationManager!


override func viewDidLoad() {
    locationManager = CLLocationManager();
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation();
    super.viewDidLoad()


    // Do any additional setup after loading the view, typically from a nib.
}

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


@IBAction func valueChanged(sender: UISlider) {
    if power.on{
    var val = "\(slider.value)";
    txtSlider.text = val;
    }
}


@IBAction func cmdLocateMeClicked(sender: UIButton) {


}

//Deprecated
@IBAction func cmdLocateMePressed(sender: AnyObject) {
}

//CLLocationManagerDelegate
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    var location:CLLocation = locations[locations.count-1] as CLLocation

    println("locations = \(locations)")
    txtLatitude.text = "\(location.coordinate.latitude)";
    txtLongitude.text = "\(location.coordinate.longitude)";
}

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    println(error)
    txtLatitude.text = "Can't get your location!"
}

}

marlinspike
  • 185
  • 2
  • 2
  • 11
  • possible duplicate of [CLLocation Manager in Swift to get Location of User](http://stackoverflow.com/questions/24063798/cllocation-manager-in-swift-to-get-location-of-user) – Paulw11 Oct 04 '14 at 00:39

1 Answers1

2

Figured it out -- I had to give permissions to my app to receive Location Information on the iOS Simulator (Settings -> Privacy -> Location). Once I did that, things worked perfectly.

marlinspike
  • 185
  • 2
  • 2
  • 11