gpt4 book ai didi

ios - 在参数 Swift 中传递当前位置

转载 作者:行者123 更新时间:2023-11-28 13:41:32 25 4
gpt4 key购买 nike

我正在尝试在参数中传递当前用户位置以获得所需的结果。我在我的 viewDidLoad 方法中得到这样的当前位置:-

self.locationManager.requestAlwaysAuthorization()

self.locationManager.requestWhenInUseAuthorization()

if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}

之后,我调用了 didUpdateLocations 方法来获取当前位置坐标,并创建了两个变量来存储当前位置。

var latitude: String = ""
var longitude: String = ""

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
print("locations = \(locValue.latitude) \(locValue.longitude)")
self.latitude = "\(locValue.latitude)" ; self.longitude = "\(locValue.latitude)"
print(self.latitude, self.longitude)
}

但是当我试图在参数中传递我的当前位置时,我没有得到任何值。

let param: [String : Any] =
["lat": self.latitude,
"lng": self.longitude,
"truck_type": "2",
"title": "home",
"job_type": "Jobs"


]
print(param)
CommonUtils.showHudWithNoInteraction(show: true)

Alamofire.request("api", method: .post, parameters: param, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

CommonUtils.showHudWithNoInteraction(show: false)
switch(response.result) {
case .success(_):
if let json = response.result.value as? [String:Any] {
print(json)

DispatchQueue.main.async {
self.fleetOwnerTableView.reloadData()
}
}

break

case .failure(_):
print("Error")
break

}
}

enter image description here我不知道我做错了什么。如何将当前位置传递给参数?

最佳答案

更新didUpdateLocations方法调用api并传递当前位置

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
callAPI(location: location.coordinate)
}
}

你的api调用方式

func callAPI(location:CLLocationCoordinate2D) {
let param: [String : Any] =
["lat": "\(location.latitude)",
"lng": "\(location.longitude)",
"truck_type": "2",
"title": "home",
"job_type": "Jobs"
]
//Alamofire.request
}

关于ios - 在参数 Swift 中传递当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55967029/

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