【问题标题】:How to access user coordinates如何访问用户坐标
【发布时间】:2017-11-24 23:42:28
【问题描述】:

我正在尝试使用以下方式访问用户坐标:

import MapKit
import CoreLocation

override func viewDidLoad() {
    super.viewDidLoad()
    let locationManager = CLLocationManager()
    let userCoordinates = (locationManager.location?.coordinate)!
}

但是,它在模拟器加载时崩溃。我将模拟器位置设置为 Apple,并将隐私密钥输入到 info.plist,但我不确定为什么这没有获取用户位置。

【问题讨论】:

    标签: swift location maps


    【解决方案1】:

    在您可以开始安全地使用设备的当前地理位置之前,您需要先做几件事,根据您提供的代码,我假设您可能会遗漏一些,所以这里是一个常见的设置,使用一个谷歌地图,基本上和其他任何地图一样:

    class YourViewController: UIViewController, CLLocationManagerDelegate {
    
        // properties
        var locationManager = CLLocationManager()
        var currentCoordinate: CLLocationCoordinate2D?
    
        // load view
        override func loadView() {
            addLocationManager()
        }
    
        // add location manager
        func addLocationManager() {
    
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.distanceFilter = kCLDistanceFilterNone
            locationManager.requestWhenInUseAuthorization()
            locationManager.startUpdatingLocation()
    
        }
    
        // location manager delegate: did change authorization
        func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    
            switch status {
    
            case .restricted:
                print("LOCATION ACCESS RESTRICTED")
    
            case .denied:
                print("LOCATION ACCESS DENIED")
    
            case .notDetermined:
                print("LOCATION ACCESS NOT DETERMINED")
    
            case .authorizedAlways:
                fallthrough
    
            case .authorizedWhenInUse:
                print("LOCATION STATUS GRANTED")
    
            }
    
        }
    
    
        // location manager delegate: did fail with error
        func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    
            locationManager.stopUpdatingLocation()
            print("LOCATION ACCESS ERROR: \(error)")
    
        }
    
    
        // location manager delegate: did update locations
        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    
            let lastLocation = locations.last!
    
            // unhide map when current location is available
            if mapView.isHidden {
                mapView.camera = GMSCameraPosition.camera(withLatitude: lastLocation.coordinate.latitude, longitude: lastLocation.coordinate.longitude, zoom: 18, bearing: 0, viewingAngle: 0)
                mapView.isHidden = false
            }
    
            // update current location properties
            currentCoordinate = lastLocation.coordinate
    
        }
    
    
    }
    

    如果您已导入 MapKit,则无需导入 CoreLocation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-31
      • 2021-07-12
      • 1970-01-01
      • 2020-03-06
      • 1970-01-01
      • 1970-01-01
      • 2017-07-17
      • 2021-07-21
      相关资源
      最近更新 更多