老师,请问我为什么一直显示地理位置信息不可用啊?
import UIKit
import CoreLocation
class WeatherViewController:UIViewController, CLLocationManagerDelegate {
let locationManger = CLLocationManager()
@IBOutlet weak var location: UILabel!
@IBOutlet weak var icon: UIImageView!
@IBOutlet weak var temperature: UILabel!
@IBOutlet weak var loading: UILabel!
@IBOutlet weak var loadingIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
locationManger.delegate = self
locationManger.desiredAccuracy = kCLLocationAccuracyBest
self.loadingIndicator.startAnimating()
let backgroud = UIImage(named: "background")
self.view.backgroundColor = UIColor(patternImage: backgroud!)
if(iOS8()) {
locationManger.requestAlwaysAuthorization()
}
locationManger.startUpdatingLocation()
}
func iOS8() -> Bool {
if(UIDevice.currentDevice().systemVersion == "8.0") || (UIDevice.currentDevice().systemVersion == "8.0.1") || (UIDevice.currentDevice().systemVersion == "8.0.2") || (UIDevice.currentDevice().systemVersion == "8.1") || (UIDevice.currentDevice().systemVersion == "8.1.1") || (UIDevice.currentDevice().systemVersion == "8.1.2") || (UIDevice.currentDevice().systemVersion == "8.1.3") {
return true
}
else {
return false
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var location: CLLocation = locations[locations.count - 1] as CLLocation
if(location.horizontalAccuracy > 0) {
println(location.coordinate.latitude)
println(location.coordinate.longitude)
updateWeatherInfo(location.coordinate.latitude, longitude: location.coordinate.longitude)
locationManger.stopUpdatingLocation()
}
}
func updateWeatherInfo(latitude: CLLocationDegrees, longitude: CLLocationDegrees) {
let manager = AFHTTPRequestOperationManager()
let url = "http://api.openweathermap.org/data/2.5/weather"
let params = ["lat": latitude, "lon": longitude, "cnt": 0]
manager.GET(url, parameters: params,
success: {(operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in println("JSON: " + responseObject.description!);self.updateUISuccess(responseObject as NSDictionary!)
},
failure: {(operation: AFHTTPRequestOperation!, error: NSError!) in println("Error:" + error.localizedDescription)
})
}
func updateUISuccess(jsonResult: NSDictionary!) {
self.loadingIndicator.hidden = true
self.loadingIndicator.stopAnimating()
self.loading.text = nil
if let tempResult = jsonResult["main"]?["temp"]? as? Double {
var temperature: Double
if(jsonResult["sys"]?["country"]? as String == "US") {
temperature = round(((tempResult - 273.15) * 1.8) + 32)
}
else {
temperature = round(tempResult - 273.15)
}
self.temperature.text = "\(temperature)˚"
var name = jsonResult["name"]? as String
self.location.text = "\(name)"
var condition = (jsonResult["weather"]? as NSArray)[0]["id"]? as Int
var sunrise = jsonResult["sys"]?["sunrise"]? as Double
var sunset = jsonResult["sys"]?["sunset"]? as Double
var nightTime = false
var now = NSDate().timeIntervalSince1970
if(now < sunrise || now > sunset) {
nightTime = true
}
self.updateWeatherIcon(condition, nightTime: nightTime)
}
else {
self.loading.text = "天气信息不可用"
}
}
func updateWeatherIcon(condition: Int, nightTime: Bool) {
if(condition < 300) {
if nightTime {
self.icon.image = UIImage(named: "tstorm1_night")
}
else {
self.icon.image = UIImage(named: "tsorm1")
}
}
else if(condition < 500) {
self.icon.image = UIImage(named: "light_rain")
}
else if(condition < 600) {
self.icon.image = UIImage(named: "shower3")
}
else if(condition < 700) {
self.icon.image = UIImage(named: "snow4")
}
else if(condition < 771) {
if nightTime {
self.icon.image = UIImage(named: "fog_night")
}
else {
self.icon.image = UIImage(named: "fog")
}
}
else if(condition < 800) {
self.icon.image = UIImage(named: "tstorm3")
}
else if(condition == 800) {
if nightTime {
self.icon.image = UIImage(named: "sunny_night")
}
else {
self.icon.image = UIImage(named: "sunny")
}
}
else if(condition < 804) {
if nightTime {
self.icon.image = UIImage(named: "cloudy2_night")
}
else {
self.icon.image = UIImage(named: "cloudy2")
}
}
else if(condition == 804) {
self.icon.image = UIImage(named: "overcast")
}
else if((condition >= 900) || (condition > 904 && condition < 1000)) {
self.icon.image = UIImage(named: "tstorm3")
}
else if(condition == 903) {
self.icon.image = UIImage(named: "snow5")
}
else if(condition == 904) {
self.icon.image = UIImage(named: "sunny")
}
else {
self.icon.image = UIImage(named: "dunno")
}
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
self.loading.text = "地理位置信息不可用"
}
}