gpt4 book ai didi

swift - CLLocation + 天气(Alamofire)问题

转载 作者:行者123 更新时间:2023-11-28 08:14:37 27 4
gpt4 key购买 nike

我正在尝试使用 CLLocation 来捕获经度和纬度,然后使用 Alamofire 中的经度和纬度来获取天气。每次,经度和纬度都不会停止更新,也不会打印天气数据(如果你想查看这里是数据的示例链接:http://forecast.weather.gov/MapClick.php?lat=37.33233141&lon=-122.0312186&FcstType=json)

class SampleViewController: UIViewController, CLLocationManagerDelegate {
var locationManager:CLLocationManager!
var startLocation: CLLocation!
var isFetchingWeather = false

override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
}

override func viewDidAppear(_ animated: Bool) {
getCurrentLocation()
}
func getCurrentLocation(){
if CLLocationManager.locationServicesEnabled(){
locationManager.startUpdatingLocation()

}
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
var userLocation:CLLocation = locations[0]
if isFetchingWeather != false{
print("user latitude = \(userLocation.coordinate.latitude)")
print("user longitude = \(userLocation.coordinate.longitude)")
let requestLink = "http://forecast.weather.gov/MapClick.php?lat=\(userLocation.coordinate.latitude)&lon=\(userLocation.coordinate.longitude)&FcstType=json"
print(requestLink)
Alamofire.request(requestLink).validate().responseJSON
{ response in
switch response.result {
case .success(let data):
let json = JSON(data)
self.weatherData = json["data"].arrayValue
for weather in self.weatherData{
let temp = weather["weather"].stringValue
self.weatherString.append(temp)
}
print (self.weatherString)
if self.startLocation == nil {
self.startLocation = userLocation as! CLLocation
self.locationManager.stopUpdatingLocation()
}


case .failure(let error):
print(error)
}
}
}
else{
print("is fetching weather is false")
}

}


func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
print("Error \(error)")
}
}

谢谢。

最佳答案

你真的不应该在你的位置委托(delegate)中运行你的天气请求。相反,在 didUpdateLocations 委托(delegate)中获取您的位置并将其保存到 var。接下来,调用 stopUpdatingLocation(),然后调用一个单独的函数来发出天气请求。像这样:

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

let newLocation = locations.last

//check accuracy and timestamp of location to make sure its not a cached/old location (if you don't care about accuracy or time, you can remove this check)
let timeDiff = newLocation?.timestamp.timeIntervalSinceNow

if timeDiff < 5.0 && (newLocation?.horizontalAccuracy)!<=self.accuracyNeeded{

//stop updating location
self.locationManager.stopUpdatingLocation()

//set currentUserLocation
self.myLocation=newLocation?.coordinate


//call function to get weather

//remove delegate
self.locationManager.delegate = nil

}

}

关于swift - CLLocation + 天气(Alamofire)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42893851/

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