2017-03-19 28 views
1

我想用CLLocation来获取经度和纬度,然后使用Alamofire的经度和纬度来获取天气。每到这时,经度和纬度不会停止更新和气象数据将不打印(如果你想看看这里的数据的一个例子链接:http://forecast.weather.gov/MapClick.php?lat=37.33233141&lon=-122.0312186&FcstType=jsonCLLocation +天气(Alamofire)问题

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)") 
} 
} 

感谢。

回答

0

你真的不应该在您的位置代理中运行您的天气请求。相反,请在didUpdateLocations委托中获取您的位置并将其保存到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 

    } 

} 
+0

谢谢+1!几乎所有的东西都在工作 - 甚至与经度和纬度的联系都在印刷,但alamofire本身没有任何返回...... @rmp – zander

+0

是否有可能在该位置没有天气数据?或者你得到一个错误? – rmp

+0

没关系 - 我得到了alamofire的工作。谢谢! – zander

0

设置一个标志来指示您何时开始获取天气信息,如果设置了该标志,则不要致电Alamofire获取天气信息。例如,你将宣布类似的东西,你声明startLocation行后:

var isFetchingWeather = false 

然后,在locationManagerdidUpdateLocations首先检查isFetchingWeather是假的。如果没有,返回。否则,获取天气信息。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    if isFetchingWeather { 
     return 
    } 
    isFetchingWeather = true 
    // Do the actual weather fetching 
} 

当然,你可能想要做天气的实际取你已经得到了从最初的那些少数的位置更新后,可能不准确:)

+0

我试过,并在其他部分的if语句里面,我把一个打印说,这是假的。我跑了应用程序和控制台打印不停打印。 @Fahim – zander

+0

您是否在第一次执行提取操作时将'isFetchingWeather'设置为'true'?你能提供一个代码示例,以便我能看到你在做什么吗?知道你的代码在没有看到的情况下执行起来有点困难:) – Fahim

+0

是的 - 抱歉花了这么长时间。不,我没有。让我用我运行@Fahim的代码更新我的问题中的代码。 – zander