Ios 来自CoreLocation swift的位置数据

Ios 来自CoreLocation swift的位置数据,ios,swift,coordinates,core-location,xcode6,Ios,Swift,Coordinates,Core Location,Xcode6,我正在尝试获取用户坐标,但我不知道如何检索它们?这是我在地图上查找它们位置的代码,我应该添加什么来获得经度/纬度的数值?它工作正常,我已将requestAuthorization密钥添加到info.plist中。我尝试使用CLLocation对象,但似乎无法使其工作 import UIKit import MapKit class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

我正在尝试获取用户坐标,但我不知道如何检索它们?这是我在地图上查找它们位置的代码,我应该添加什么来获得经度/纬度的数值?它工作正常,我已将requestAuthorization密钥添加到info.plist中。我尝试使用CLLocation对象,但似乎无法使其工作

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var Map: MKMapView!


var locationMgr = CLLocationManager()


override func viewDidLoad() {
    super.viewDidLoad()


    locationMgr.delegate = self
    locationMgr = CLLocationManager()
    locationMgr.requestAlwaysAuthorization()
     locationMgr.startUpdatingLocation()
            Map.showsUserLocation = true


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

}

实施CLLocationManager委托协议方法,具体如下:

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

您已经初始化了位置管理器两次。因此,在经理首次初始化后设置的代理为零。因此,在viewDidLoad方法中删除此
locationMgr=CLLocationManager()
。它应该可以工作

您必须实现此方法才能实际检索位置:
-func locationManager(manager:CLLocationManager,didUpdateLocations locations:AnyObject[])
感谢您的快速响应!你能不能给我一些示例代码,让它把位置打印到控制台上?函数中的println(位置)似乎不起作用,我认为它不断返回nil值?我真的是新手,这是我第一周的编码!谢谢有帮助的链接:)我现在已经让它工作了,在视图控制器中也是如此嘿!和我刚才对Lyndsey说的一样,你能不能给我一些示例代码,让它把位置打印到控制台上?func中的println(位置)似乎不起作用,我认为它不断返回零值?如果这是您编码的第一周,请首先使用Apple提供的有关Objective-C(和)的介绍材料。Swift仍处于测试阶段,因此我不建议使用Swift学习编程。无论如何,要打印出Swift使用的位置:println(\(位置)),我正在学习它,因为我喜欢x代码的实际效果,Swift只是比obj C直观得多。现在开始工作:)Swift不再是beta版,并且非常稳定,可以在这一点上学习编码。