首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift currentLocation返回零

Swift currentLocation返回零
EN

Stack Overflow用户
提问于 2018-03-15 00:09:11
回答 1查看 209关注 0票数 0

我需要获得用户的当前位置,但是currentLocation返回alawas为零。然而,CLLocationManager.authorizationStatus是authorizedWhenInUse,在地图上,我位置的引脚是正确的。

代码语言:javascript
复制
import Foundation
import CoreLocation

class Location: NSObject, CLLocationManagerDelegate {
    var currentLocation: CLLocation? = nil
    var showUserLocation: Bool?
    var isAuthorised = false {
        didSet {
            if isAuthorised {
                locationManager.startUpdatingLocation()
                showUserLocation = true
            } else {
                locationManager.stopUpdatingLocation()
                currentLocation = nil
                showUserLocation = false
            }
        }
    }

    private let locationManager = CLLocationManager()

    override init() {
        super.init()

        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        currentLocation = locations.last
    }

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

    func distance(of position: Position) -> CLLocationDistance {
        let location = CLLocation(latitude: position.lat, longitude: position.lng)
        return currentLocation?.distance(from: location) ?? Double.infinity
    }
}

extension CLLocationCoordinate2D {
    var position:Position {
        return Position(lat: latitude, lng: longitude)
    }
}

谢谢!

编辑:我已经找到解决方案了!

在我的MapViewController中,我在插入Map之前加载了注释。因此,函数距离是在函数didUpdateLocations之前执行的。

我为失去的时间道歉..。非常感谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2018-03-15 03:38:57

我编写了完整的代码,通过LocationManager类获取当前位置。此代码位于LocationManager.swift文件中。

LocationManager.swift

代码语言:javascript
复制
import Foundation
import CoreLocation

typealias LocationHandler = (_ location: CLLocation?, _ error: Error?) -> Void

class LocationManager: NSObject {

    var locationManager: CLLocationManager?
    var locationHandler: LocationHandler?

    override init() {
        super.init()
        setupLocation()
    }

    fileprivate func setupLocation() {
        locationManager = CLLocationManager()
        locationManager?.desiredAccuracy = kCLLocationAccuracyBestForNavigation
        locationManager?.distanceFilter = kCLLocationAccuracyBest
        locationManager?.delegate = self
        locationManager?.requestWhenInUseAuthorization()
    }

    public func findCurrentLocation(_ handler: LocationHandler?) {
        if isLocationPermissionEnabled() {
            locationHandler = handler
            locationManager?.startUpdatingLocation()
        }
    }

}

extension LocationManager: CLLocationManagerDelegate {

    public func isLocationPermissionEnabled() -> Bool {
        let status = CLLocationManager.authorizationStatus()
        return (status == .denied || status == .notDetermined) ? false : true
    }

    func locationManager(_: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let currentLocation = locations.last {
            self.locationHandler?(currentLocation, nil)
        }
    }

    func locationManager(_: CLLocationManager, didFailWithError error: Error) {
        self.locationHandler?(nil, error)
    }
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49289565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档