【问题标题】:Swift - Trying to get current location [duplicate]Swift - 试图获取当前位置[重复]
【发布时间】:2016-09-06 08:37:32
【问题描述】:

我正在尝试获取用户位置的经纬度,我已完成以下操作:

导入的核心位置

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
    }

但是位置没有被打印出来,我的方法甚至没有得到它。

我将使用核心位置的项目添加到我的 plist 中,并且应用程序在我第一次运行它时要求我使用位置服务。但是现在位置正在打印....我做错了什么?

【问题讨论】:

标签: ios swift core-location


【解决方案1】:

也许您忘记为 delegate 设置值

在您的viewDidLoad() 中添加:

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

【讨论】:

    【解决方案2】:

    使用此代码,

    声明位置管理器

    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()
    }
    

    【讨论】:

      【解决方案3】:

      在 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】:

        你需要初始化 locationManager 并在 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()
        

        【讨论】:

          【解决方案5】:

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

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

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

          【讨论】:

            猜你喜欢
            • 2012-12-22
            • 1970-01-01
            • 2012-10-26
            • 2021-10-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-07-30
            • 1970-01-01
            相关资源
            最近更新 更多