gpt4 book ai didi

swift - 核心位置委托(delegate)闭包中的值(value)

转载 作者:行者123 更新时间:2023-11-28 06:27:05 25 4
gpt4 key购买 nike

您好,我已经在 Swift 3 中实现了核心位置管理器,我想构建一个方法来从它的委托(delegate)中捕获位置值并返回闭包中的值,但无法这样做。任何帮助将不胜感激。

我的代码:

import Foundation
import CoreLocation

class LocationManager : NSObject, CLLocationManagerDelegate{

var location: CLLocation?
var locationManager : CLLocationManager?
var currentLocation : CLLocationCoordinate2D?
var latitude : String?
var longitude : String?
var lat : Float?
var lon : Float?

// var locationManagerCallback : (Float?,Float?,String?,String?) ->()


static let sharedInstance : LocationManager = {
let instance = LocationManager()
return instance
}()


// MARK: - CLLocation Fetch Methods

func fetchLocationWithCompletionHandler(completion : @escaping(Float?,Float?,String?,String?) -> ()) -> Void {

if locationManager != nil {

locationManager?.stopUpdatingLocation()
locationManager?.delegate = nil
locationManager = nil
}
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.distanceFilter = kCLLocationAccuracyNearestTenMeters
locationManager?.desiredAccuracy = kCLLocationAccuracyBest

let requestWhenInUseSelector = NSSelectorFromString("requestWhenInUseAuthorization")
let requestAlwaysSelector = NSSelectorFromString("requestAlwaysAuthorization")

if (locationManager?.responds(to: requestWhenInUseSelector))! {

locationManager?.requestWhenInUseAuthorization()

}else if (locationManager?.responds(to: requestAlwaysSelector))!{

locationManager?.requestAlwaysAuthorization()
}
locationManager?.startUpdatingLocation()


}

func requestWhenInUseAuthorization() -> Void {

let status : CLAuthorizationStatus = CLLocationManager.authorizationStatus()
if status == CLAuthorizationStatus.denied || status == CLAuthorizationStatus.restricted {
// TODO : Handle Denial Here
}else if status == CLAuthorizationStatus.notDetermined{
locationManager?.requestWhenInUseAuthorization()
}
}
func requestAlwaysAuthorization() -> Void {

let status : CLAuthorizationStatus = CLLocationManager.authorizationStatus()
if status == CLAuthorizationStatus.denied || status == CLAuthorizationStatus.restricted {
// TODO : Handle Denial Here
}else if status == CLAuthorizationStatus.notDetermined{
locationManager?.requestAlwaysAuthorization()
}

}


func reveseGeocode(currentLocation : CLLocation, completionCallback : @escaping(CLPlacemark?,Error?) -> Swift.Void) -> Void {

let reverseGeocoder : CLGeocoder = CLGeocoder()
reverseGeocoder.reverseGeocodeLocation(currentLocation) { (placemarkArray, error) in

let placemark : CLPlacemark = (placemarkArray?.first)!
completionCallback(placemark,error)
}
}


// MARK: - CLLocation Delegate Methods

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

self.location = locations[0]
currentLocation = self.location?.coordinate
lat = Float((currentLocation?.latitude)!)
lon = Float((currentLocation?.longitude)!)
latitude = String(format: "%lf",lat!)
longitude = String(format : "%lf",lon!)

UserDefaults.standard.set(lat, forKey: CURR_LAT_VAL)
UserDefaults.standard.set(lon, forKey: CURR_LON_VAL)
UserDefaults.standard.set(latitude, forKey: CURR_LAT_STR)
UserDefaults.standard.set(longitude, forKey: CURR_LON_STR)
UserDefaults.standard.synchronize()

print("\(lat)\(lon)\(latitude)\(longitude)")

// locManager(lat,lon,latitude,longitude)

}

func locationManagerDidPauseLocationUpdates(_ manager: CLLocationManager) {
print("location manager did pause")
}

func locationManagerDidResumeLocationUpdates(_ manager: CLLocationManager) {
print("location manager did resume")
}

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

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
print("Authorization Changed")
}

}

最佳答案

您需要将 block 变量保存在您的 LocationManager 类中,如下所示:

1。在你的类中添加变量:

var locationManagerCallback: ((Float?,Float?,String?,String?) -> ())?

2。保存开始请求时的完成 block /关闭:

func fetchLocationWithCompletionHandler(completion: @escaping(Float?,Float?,String?,String?) -> ()) -> Void {
locationManagerCallback = completion

...
}

3。从委托(delegate)方法调用闭包:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
locationManagerCallback?(lat, lon, latitude, longitude)
}

下面是如何在其他类中使用它:

LocationManager.sharedInstance.fetchLocationWithCompletionHandler { (lat, long, latitude, longitude) in
print("\(lat)\(long)\(latitude)\(longitude)")
}

希望对您有所帮助。

关于swift - 核心位置委托(delegate)闭包中的值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41542393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com