MVP中的核心位置

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

在我的项目中,我有一个符合CLLocationManagerDelegate协议的LocationService类,以便检测当前用户的位置。

class LocationService: NSObject, CLLocationManagerDelegate {

    fileprivate let locationManager = CLLocationManager()
    var location: Location?  // Location(lat: Double, lon: Double)

    override init() {
        super.init()

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters

    }

    func getCurrentLocation() -> Location? {
        locationManager.startUpdatingLocation()
         // how can I catch a location?
        return location
    }


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.last {
            if location.horizontalAccuracy > 0 {
                locationManager.stopUpdatingLocation()
                self.location = Location(lat: location.coordinate.latitude, lon: location.coordinate.latitude)
            }
        }
    }

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

}

我希望我的WeatherPresenter在LocationService中触发位置更新,并在找到位置后立即获得结果。有没有办法做到这一点?

class WeatherPresenter {

    unowned let delegate: WeatherViewDelegate
    let weatherService = WeatherService()
    let locationService = LocationService()

    init(with delegate: WeatherViewDelegate) {
        self.delegate = delegate
    }

    func getWeatherForCurrentLocation() {
        if let location = locationService.getCurrentLocation() {
            //...
        }
    }
}
ios swift core-location mvp
1个回答
1
投票

您可以使用Delegate通知WeatherPresenter有关LocationService的更改

protocol LocationServiceDelegate: class { // Delegate protocol
    func didUpdateLocation()
}

class LocationService: NSObject, CLLocationManagerDelegate {

    weak var delegate: LocationServiceDelegate?

    fileprivate let locationManager = CLLocationManager()
    var location: Location?  // Location(lat: Double, lon: Double)

    override init() {
        super.init()

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters

    }

    func startUpdatingLocation() { // Start updating called from presenter
        locationManager.startUpdatingLocation()
    }

    func getCurrentLocation() -> Location? {
        // how can I catch a location?
        return location
    }


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.last {
            if location.horizontalAccuracy > 0 {
                locationManager.stopUpdatingLocation()
                self.location = Location(lat: location.coordinate.latitude, lon: location.coordinate.latitude)
                self.delegate?.didUpdateLocation() // Notify delegate on change
            }
        }
    }

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

}


class WeatherPresenter: LocationServiceDelegate {
    unowned let delegate: WeatherViewDelegate
    let weatherService = WeatherService()
    let locationService = LocationService()

    init(with delegate: WeatherViewDelegate) {
        self.delegate = delegate
        self.locationService.delegate = self // Set self as delegate
        self.locationService.startUpdatingLocation()  // Requests start updating location
    }

    func didUpdateLocation() { // This will be called on location change
        self.getWeatherForCurrentLocation()
    }

    func getWeatherForCurrentLocation() {
        if let location = locationService.getCurrentLocation() {
            //...
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.