Swift:获取当前用户坐标并将其存储到变量中

问题描述 投票:-1回答:1

我目前正在尝试获取用户的当前坐标,并最终将这些值存储到变量中。

我创建了以下类来定义用户的当前位置并设置函数以提取数据。

import Foundation
import CoreLocation

class MyCurrentCoordinate: NSObject {


     private var currentLocation: CLLocation!

     var myLatitude = 0.0
     var myLongitude = 0.0
     var myAltitude = 0.0

     override init() {
         super.init()
     }

     func getLat() {
         myLatitude = currentLocation.coordinate.latitude
     }

     func getLong() {
         myLongitude = currentLocation.coordinate.longitude
     }

     func getAlt() {
         myAltitude = currentLocation.altitude
     }
}

这不会显示任何错误。但是,当我去调用任何函数(getLat,getLong或getAlt)提取部分用户位置数据时,由于值nil,应用程序崩溃。是否有人对未通过实际用户纬度,经度或海拔高度有任何见解?

我已更新位置权限和info.plist,以允许用户授予位置跟踪权限。

swift swiftui coordinates core-location cllocation
1个回答
0
投票

您可以使用CLLocationManager

  1. [添加应用程序功能,您可以像打开源代码一样打开info.plist并添加:

    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>App requires allways tracking</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>App requires background tracking</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>App requires tracking when be in use</string>
    
    <key>UIBackgroundModes</key>
    <array>
        <string>fetch</string>
        <string>location</string>
        <string>remote-notification</string>
    </array>
    
  2. 询问授权,例如locationManager.requestAlwaysAuthorization()并管理是否具有正确的访问权限... CLLocationManager.authorizationStatus()== .authorizedAlways

    import CoreLocation
    
    class ViewController: UIViewController, CLLocationManagerDelegate {
        var locationManager: CLLocationManager?
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            locationManager = CLLocationManager()
            locationManager?.delegate = self
            locationManager?.requestAlwaysAuthorization()
            if CLLocationManager.authorizationStatus() == .authorizedAlways {
                locationManager.allowsBackgroundLocationUpdates = true
                locationManager.pausesLocationUpdatesAutomatically = false
                locationManager.desiredAccuracy = kCLLocationAccuracyBest
                locationManager.startUpdatingLocation()
            }
            if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
                locationManager.allowsBackgroundLocationUpdates = false
                locationManager.pausesLocationUpdatesAutomatically = true
                locationManager.desiredAccuracy = kCLLocationAccuracyBest
                locationManager.startUpdatingLocation()
            }
        }
    }
    
    extension ViewController: CLLocationManagerDelegate {
    
        public func locationManager(_: CLLocationManager, didUpdateLocations locations: [CLLocation]){
            guard let location = locations.first else { return }
            print(location)
    
        }
    
        public func locationManager(_: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
            switch status {
            case .notDetermined:
                print("Autorization status did change \(status)")
            case .authorizedWhenInUse:
                print("Autorization status did change \(status)")
            case .authorizedAlways:
                print("Autorization status did change \(status)")
            case .restricted:
                print("Autorization status did change \(status)")
            case .denied:
                print("Autorization status did change \(status)")
            @unknown default:
                fatalError()
            }
        }
    }
    
  3. 不要忘记在某个地方停止它locationManager.stopUpdatingLocation()

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