斯威夫特 - 试图获取当前位置

时间:2016-05-11 06:04:35

标签: ios swift core-location

我正在尝试获取用户位置的长和lat,我已完成以下操作:

导入核心位置

import CoreLocation

添加核心位置代表

class ViewController: UIViewController, CLLocationManagerDelegate, AVCaptureMetadataOutputObjectsDelegate, DTDeviceDelegate {

定义了这个变量:

var locationManager: CLLocationManager!

然后添加了这个方法:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let userLocation:CLLocation = locations[0] 
        let long = userLocation.coordinate.longitude;
        let lat = userLocation.coordinate.latitude;

        print(long, lat)

        //Do What ever you want with it
    }

但该位置没有打印,我的方法甚至没有得到它。

我添加项目以使用Core Location到我的plist,应用程序让我在第一次运行时使用位置服务。但现在位置正在印刷......我做错了什么?

5 个答案:

答案 0 :(得分:4)

也许您忘记为delegate

设置值

viewDidLoad()添加此内容:

if CLLocationManager.locationServicesEnabled() {
    locationManager.requestAlwaysAuthorization()
    locationManager.requestWhenInUseAuthorization()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
    mapView.showsUserLocation = true
}

答案 1 :(得分:2)

使用此代码,

声明locationManager

var locationManager = CLLocationManager()

在viewDidLoad中设置Delegate,如下所示代码

override func viewDidLoad() {

    super.viewDidLoad()

    locationManager.delegate = self

    if CLLocationManager.authorizationStatus() == .NotDetermined {
        self. locationManager.requestWhenInUseAuthorization()
    }

    locationManager.distanceFilter = kCLDistanceFilterNone
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
}

答案 2 :(得分:1)

你需要初始化locationManageger并在xcode中启用位置标志,它位于xcode的底部

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

if #available(iOS 8.0, *) {

    if  Bundle.main.infoDictionary?["NSLocationAlwaysUsageDescription"] != nil {

        self.locationManager.requestAlwaysAuthorization()
    } 
    else {

        self.locationManager.requestWhenInUseAuthorization()
    }    
}

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.startUpdatingLocation()

答案 3 :(得分:1)

在viewdidload中添加它,

if (CLLocationManager.locationServicesEnabled())
{
     locationManager.delegate = self
     locationManager.desiredAccuracy = kCLLocationAccuracyBest
     if ((UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8)
     {
          locationManager.requestWhenInUseAuthorization()
     }

     locationManager.startUpdatingLocation()
}
else
{
     #if debug
     println("Location services are not enabled");
     #endif           
}

然后添加这两个委托方法来获取位置

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!)
{
    locationManager.stopUpdatingLocation()
    if ((error) != nil)
    {
        print(error)
    }
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
{
    var locationArray = locations as NSArray
    var locationObj = locationArray.lastObject as! CLLocation
    var coord = locationObj.coordinate
    print(coord.latitude)
    print(coord.longitude)

}

这样您就可以获得当前位置。

答案 4 :(得分:0)

您可以将此代码添加到viewDidLoad()方法中:

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

如果您的应用可以使用位置服务,这将调用locationManagers didUpdate委托方法