我尝试过使用Swift的简单CoreLocation示例,但我无法让事情发挥作用。我已经提取了大部分代码,所以它只是准系统以便集中讨论这个问题,而且我希望这个答案可以迅速传达给某人。
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!"
}
}
答案 0 :(得分:2)
想出来 - 我必须授予我的应用程序权限才能在iOS模拟器上接收位置信息(设置 - >隐私 - >位置)。一旦我做到了,事情就完美了。