如何使用CLLocationManager-Swift获取当前经纬度

问题描述 投票:0回答:9

我想使用 Swift 获取某个位置的当前经度和纬度,并通过标签显示它们。我尝试这样做,但标签上没有显示任何内容。

import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate{

    @IBOutlet weak var longitude: UILabel!
    @IBOutlet weak var latitude: UILabel!
    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        if (CLLocationManager.locationServicesEnabled()) {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.requestWhenInUseAuthorization()
            locationManager.startUpdatingLocation()
        } else {
            println("Location services are not enabled");
        }
    }

    // MARK: - CoreLocation Delegate Methods

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
         locationManager.stopUpdatingLocation()
         removeLoadingView()
         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
        longitude.text = coord.longitude
        latitude.text = coord.latitude
        longitude.text = "\(coord.longitude)"
        latitude.text = "\(coord.latitude)"
    }
}
ios swift location core-location cllocationmanager
9个回答
86
投票

恕我直言,当您正在寻找的解决方案非常简单时,您的代码就过于复杂了。

我已经使用以下代码完成了它:

首先创建

CLLocationManager
的实例并请求授权

var locManager = CLLocationManager()
locManager.requestWhenInUseAuthorization()

然后检查用户是否允许授权。

var currentLocation: CLLocation!

if 
   CLLocationManager.authorizationStatus() == .authorizedWhenInUse ||
   CLLocationManager.authorizationStatus() ==  .authorizedAlways
{         
    currentLocation = locManager.location        
}

要使用它,只需这样做

label1.text = "\(currentLocation.coordinate.longitude)"
label2.text = "\(currentLocation.coordinate.latitude)"

您将它们设置为

label.text
的想法是正确的,但是我能想到的唯一原因是用户没有授予您权限,这就是为什么您当前的位置数据为零。

但是您需要调试并告诉我们。

CLLocationManagerDelegate
也不是必需的。

希望这有帮助。有疑问就问吧。


36
投票

对于 Swift 3:

首先您需要在

info.plist
中设置接收用户GPS的限额。

设置:

NSLocationWhenInUseUsageDescription
使用随机字符串。 和/或:
NSLocationAlwaysUsageDescription
带有随机字符串。

然后:

import UIKit
import MapKit

class ViewController: UIViewController {

    var locManager = CLLocationManager()
    var currentLocation: CLLocation!

    override func viewDidLoad() {
        super.viewDidLoad()
        locManager.requestWhenInUseAuthorization()

        if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse ||
            CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways){
            guard let currentLocation = locManager.location else {
                return
            }
            print(currentLocation.coordinate.latitude)
            print(currentLocation.coordinate.longitude)
        }
    }
}

完成。


16
投票

尽管有其他建议,您应该使用

CLLocationManagerDelegate
来安全地检索位置(如果不使用它,当位置管理器没有足够的时间更新时,您可能会得到空位置)。我强烈建议将位置管理器代码包装在静态共享帮助程序中(沿着这些思路):

class Locator: NSObject, CLLocationManagerDelegate {
    enum Result <T> {
      case .Success(T)
      case .Failure(ErrorType)
    }

    static let shared: Locator = Locator()

    typealias Callback = (Result <Locator>) -> Void

    var requests: Array <Callback> = Array <Callback>()

    var location: CLLocation? { return sharedLocationManager.location  }

    lazy var sharedLocationManager: CLLocationManager = {
        let newLocationmanager = CLLocationManager()
        newLocationmanager.delegate = self
        // ...
        return newLocationmanager
    }()

    // MARK: - Authorization

    class func authorize() { shared.authorize() }
    func authorize() { sharedLocationManager.requestWhenInUseAuthorization() }

    // MARK: - Helpers

    func locate(callback: Callback) {
        self.requests.append(callback)
        sharedLocationManager.startUpdatingLocation()
    }

    func reset() {
        self.requests = Array <Callback>()
        sharedLocationManager.stopUpdatingLocation()
    }

    // MARK: - Delegate

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        for request in self.requests { request(.Failure(error)) }
        self.reset()
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: Array <CLLocation>) {
        for request in self.requests { request(.Success(self)) }
        self.reset()
    }

}

然后在视图中加载(或在其他任何您需要获取当前位置的地方)运行:

Locator.shared.locate { result in
  switch result {
  case .Success(locator):
    if let location = locator.location { /* ... */ }
  case .Failure(error):
    /* ... */
  }
}

3
投票

在斯威夫特

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    //Labels outlets

    @IBOutlet var localityTxtField: UITextField!
    @IBOutlet var postalCodeTxtField: UITextField!
    @IBOutlet var aAreaTxtField: UITextField!
    @IBOutlet var countryTxtField: UITextField!

    let locationManager = CLLocationManager()

    //View Didload

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

     //Button Location

    @IBAction func findMyLocation(_ sender: AnyObject) {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in

            if (error != nil) {
                print("Reverse geocoder failed with error" + (error?.localizedDescription)!)
                return
            }

            if (placemarks?.count)! > 0 {

                print("placemarks",placemarks!)
                let pm = placemarks?[0]
                self.displayLocationInfo(pm)
            } else {
                print("Problem with the data received from geocoder")
            }
        })
    }

    func displayLocationInfo(_ placemark: CLPlacemark?) {
        if let containsPlacemark = placemark {

            print("your location is:-",containsPlacemark)
            //stop updating location to save battery life
            locationManager.stopUpdatingLocation()
            let locality = (containsPlacemark.locality != nil) ? containsPlacemark.locality : ""
            let postalCode = (containsPlacemark.postalCode != nil) ? containsPlacemark.postalCode : ""
            let administrativeArea = (containsPlacemark.administrativeArea != nil) ? containsPlacemark.administrativeArea : ""
            let country = (containsPlacemark.country != nil) ? containsPlacemark.country : ""

            localityTxtField.text = locality
            postalCodeTxtField.text = postalCode
            aAreaTxtField.text = administrativeArea
            countryTxtField.text = country
        }

    }


    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
          print("Error while updating location " + error.localizedDescription)
    }
}

3
投票

在当前线程中提出了一个没有委托的解决方案,但在模拟器中的 Xcode 9.1 测试中它不起作用,位置为零。此代码有效:

 import UIKit
 import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate {

var locationManager: CLLocationManager!

override func viewDidLoad() {
    super.viewDidLoad()

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

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{

    let location = locations.last! as CLLocation

    /* you can use these values*/
    let lat = location.coordinate.latitude
    let long = location.coordinate.longitude
}

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


}

1
投票

我是初级但我是这样解决的: 我已经创建了继承 CLLocationManagerDelegate 的类的扩展并执行以下步骤:

1.将 CoreLocation 导入到你的 ViewController

import CoreLocation

2.然后在 ViewController 中初始化位置管理器和位置变量。

var locManager = CLLocationManager()
var currentUserLocation: CLLocation!
  1. viewDidLoad()内部请求位置初始化委托和requestUsageDescription

     locManager.requestWhenInUseAuthorization()
     locManager.delegate = self
     locManager.requestLocation()
    
  2. 然后我刚刚为继承 CLLocationManagerDelegate 的 viewController 创建了扩展

     extension theNameOfYourViewController: CLLocationManagerDelegate{
    
     func locationManager(_ manager: CLLocationManager, didFailWithError error: Swift.Error) {
     print(error)
     }
    
     func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
     // .requestLocation will only pass one location to the locations array
     // hence we can access it by taking the first element of the array
     if let location = locations.first {
     print(location.coordinate.latitude)
     print(location.coordinate.longitude)
    
       }
      }
     }
    

只要记住根据您的需要更改名称,每当您需要位置时只需使用功能请求位置

    locManager.requestLocation()

0
投票

我同意上面的 Kevin 的观点,但是如果您正在寻找更少的代码来实现更简单的东西,那么以下内容就足够了: 请务必使用

CLLocationManagerDelegate

斯威夫特 4:

在viewDidLoad中你可以添加以下内容

 locationManager.requestWhenInUseAuthorization()


if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse) || (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways) {

            currentLocation = locationManager.location
            print(currentLocation.coordinate.latitude)
            print(currentLocation.coordinate.longitude)

        }

    }

对于第一个请求,一旦用户授予或拒绝许可,就会做出响应:

 func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {

            if status == .authorizedWhenInUse {

                locationManager.requestLocation()
                currentLocation = locationManager.location
                print(currentLocation.coordinate.latitude)
                print(currentLocation.coordinate.longitude)
                //Process location information and update.

    }

0
投票

确保将以下键添加到 Info.plist:

隐私 - 使用时的位置 使用说明 隐私 - 位置始终和使用时使用说明

创建用户类:

import Foundation
import CoreLocation
import MapKit

class User: NSObject, ObservableObject {
    
    @Published var position =  CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
    
    let manager = CLLocationManager()

    override init() {
        super.init()
        manager.delegate = self
        manager.requestWhenInUseAuthorization()
        manager.requestLocation()
    }
    
}

extension User: CLLocationManagerDelegate {
    
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        print("Location services authorization request")
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("User location updated")
        print("Updated position: \(locations.first?.coordinate.latitude ?? 00)")
        
        if let location = locations.first {
            self.position = location.coordinate
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Failed to find user's location: \(error.localizedDescription)")
    }

    
}

0
投票

更新

Swift 5+
Xcode 13+

在信息plist中添加这些权限

<key>NSLocationWhenInUseUsageDescription</key>
    <string>This app needs your location to show nearby services</string>

    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>This app needs your location to show nearby services</string>

    <key>NSLocationAlwaysUsageDescription</key>
    <string>This app needs your location to show nearby services</string>

将其导入到视图控制器中

import CoreLocation

在viewDidLoad()中

override func viewDidLoad() {
        locationManager.requestWhenInUseAuthorization()
        locationManager.requestLocation()
}

像这样创建扩展

extension RegisterViewController : CLLocationManagerDelegate {

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
         print("error:: \(error.localizedDescription)")
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse {
            locationManager.requestLocation()
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let locationSafe = locations.last {
            locationManager.stopUpdatingLocation()
            let latitude = locationSafe.coordinate.latitude
            let longitude = locationSafe.coordinate.longitude
            self.currentLatitude = latitude
            self.currentLongitude = longitude
            print(" Lat \(latitude) ,  Longitude \(longitude)")
            
        }
        if locations.first != nil {
            print("location:: \(locations[0])")
        }

    }

}

运行并检查这个

© www.soinside.com 2019 - 2024. All rights reserved.