swift怎么实现在线天气预
发表于:2024-10-11 作者:千家信息网编辑
千家信息网最后更新 2024年10月11日,本篇内容主要讲解"swift怎么实现在线天气预",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"swift怎么实现在线天气预"吧!实现功能简单,但知识点用到很
千家信息网最后更新 2024年10月11日swift怎么实现在线天气预
本篇内容主要讲解"swift怎么实现在线天气预",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"swift怎么实现在线天气预"吧!
实现功能简单,但知识点用到很多,比如微信搜索地理位置等。
// ViewController.swift// WeatherApp// 文启领航培训// bjflexedu.com// qq:376610000import UIKitimport CoreLocationclass ViewController: UIViewController,CLLocationManagerDelegate { @IBOutlet var cityName : UILabel @IBOutlet var icon : UIImageView @IBOutlet var tempTxt : UILabel @IBOutlet var busy : UIActivityIndicatorView @IBOutlet var messageInfo : UILabel let locationManage:CLLocationManager = CLLocationManager() override func viewDidLoad() { super.viewDidLoad() busy.startAnimating() let img = UIImage(named: "background.jpg") self.view.backgroundColor = UIColor(patternImage: img) locationManage.delegate = self locationManage.desiredAccuracy = kCLLocationAccuracyBest locationManage.requestAlwaysAuthorization() locationManage.startUpdatingLocation() } func locationManager(manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!){ var location = locations[locations.count-1] as CLLocation if location.horizontalAccuracy > 0{ let latitude = location.coordinate.latitude let longitude = location.coordinate.longitude self.updateWeatherData(latitude,longitude:longitude) locationManage.stopUpdatingLocation() } } func updateWeatherData(latitude:CLLocationDegrees,longitude:CLLocationDegrees){ let url = "http://api.openweathermap.org/data/2.5/weather" let params = ["lat":latitude,"lon":longitude,"cnt":0] let manage = AFHTTPRequestOperationManager() let success = { (operation:AFHTTPRequestOperation!, response:AnyObject!) -> Void in //println(response) self.busy.stopAnimating() self.busy.hidden = true self.messageInfo.text = nil //name let jsonResult:NSDictionary = response as NSDictionary self.cityName.text = jsonResult["name"]? as String var tempNum:Double if let tempValue = jsonResult["main"]?["temp"]? as? Double{ tempNum = round(tempValue - 273.15) self.tempTxt.text = "\(tempNum) " } let idCon = (jsonResult["weather"]? as NSArray)[0]?["id"]? as? Int let nowTime = NSDate().timeIntervalSince1970 let sunrise = jsonResult["sys"]?["sunrise"]? as? Double let sunset = jsonResult["sys"]?["sunset"]? as? Double var nightTime = false//true 表示黑夜;false表示白天 if nowTime < sunrise || nowTime > sunset{ nightTime = true }else{ nightTime = false } self.updateIcon(idCon!,nightTime:nightTime) } let failure = { (operation:AFHTTPRequestOperation!, error:NSError!) -> Void in println("\(error)") self.messageInfo.text = "远程数据取不到" } manage.GET(url, parameters: params, success: success, failure: failure) } func updateIcon(condition:Int,nightTime:Bool){ if (condition < 300) { if nightTime { self.icon.p_w_picpath = UIImage(named: "tstorm1_night") } else { self.icon.p_w_picpath = UIImage(named: "tstorm1") } } // Drizzle else if (condition < 500) { self.icon.p_w_picpath = UIImage(named: "light_rain") } // Rain / Freezing rain / Shower rain else if (condition < 600) { self.icon.p_w_picpath = UIImage(named: "shower3") } // Snow else if (condition < 700) { self.icon.p_w_picpath = UIImage(named: "snow4") } // Fog / Mist / Haze / etc. else if (condition < 771) { if nightTime { self.icon.p_w_picpath = UIImage(named: "fog_night") } else { self.icon.p_w_picpath = UIImage(named: "fog") } } // Tornado / Squalls else if (condition < 800) { self.icon.p_w_picpath = UIImage(named: "tstorm3") } // Sky is clear else if (condition == 800) { if (nightTime){ self.icon.p_w_picpath = UIImage(named: "sunny_night") // sunny night? } else { self.icon.p_w_picpath = UIImage(named: "sunny") } } // few / scattered / broken clouds else if (condition < 804) { if (nightTime){ self.icon.p_w_picpath = UIImage(named: "cloudy2_night") } else{ self.icon.p_w_picpath = UIImage(named: "cloudy2") } } // overcast clouds else if (condition == 804) { self.icon.p_w_picpath = UIImage(named: "overcast") } // Extreme else if ((condition >= 900 && condition < 903) || (condition > 904 && condition < 1000)) { self.icon.p_w_picpath = UIImage(named: "tstorm3") } // Cold else if (condition == 903) { self.icon.p_w_picpath = UIImage(named: "snow5") } // Hot else if (condition == 904) { self.icon.p_w_picpath = UIImage(named: "sunny") } // Weather condition is not available else { self.icon.p_w_picpath = UIImage(named: "dunno") } } func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!){ self.messageInfo.text = "地理位置信息找不到" println("地理位置信息找不到\(error)") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }}
到此,相信大家对"swift怎么实现在线天气预"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
天气
在线
位置
地理
地理位置
信息
内容
学习
实用
更深
兴趣
功能
实用性
实际
操作简单
数据
方法
更多
朋友
知识
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技企业有什么区别吗
车辆网络安全2020 车灯
新服务器预约满了怎么办
aopa 飞行考核软件开发
计算科学软件开发
江西教育软件开发
北京社交app软件开发
网络安全法 实施日期
法连接任何密钥管理服务器
构建网络技术监管体系
软件开发组织方案
江西省高考投档线博雅数据库
浪潮服务器进去虚拟化界面
美国防攻击服务器
壹佰软件开发小组
再说网络安全
贵港网络安全培训
座位预约系统软件开发公司
阿里买家数据库
怎么查看服务器的网口ip
房产小区立体式数据库软件
编程培训 软件开发
中国台湾餐饮软件开发视频
mysql数据库图形连接工具
网络安全防护网安全阀
数据库原理与sql指什么
精睿网络安全实验室
淮安软件开发得多少钱
中国省市数据库
哪年设立网络安全日的