根据中国天气网,自动定位获得当地天气(数据是JSON的)---不过这个接口貌似不更新了
来源:互联网 发布:java后端自学 编辑:程序博客网 时间:2024/04/29 14:40
#import <UIKit/UIKit.h>#import <CoreLocation/CoreLocation.h>@interface DNWCustomWeatherView : UIView <CLLocationManagerDelegate>@property (nonatomic,retain,readonly) UIImageView * weatherImage; // 天气图像@property (nonatomic,retain,readonly) UILabel * temperatureLabel; // 当地温度@property (nonatomic,retain,readonly) UILabel * loactionNameLabel; // 当地名称@property (nonatomic,retain) CLLocationManager * locationManage;@property (nonatomic,retain) NSMutableArray * allCityWeatherIDArray; // 存储所有城市的名字和天气ID,里面的对象是字典@property (nonatomic,copy) NSString * weatherInterFace; // 天气接口@end
#define WeatherPublic @"http://m.weather.com.cn/data/"#import "DNWCustomWeatherView.h"#import <AddressBook/AddressBook.h> // 导入对应库#import <CoreLocation/CoreLocation.h> // 导入对应库#import "HMTMyCustomNetRequest.h"@interface DNWCustomWeatherView (){ HMTMyCustomNetRequest * _request;}@end@implementation DNWCustomWeatherView- (void)dealloc{ RELEASE_SAFELY(_request); RELEASE_SAFELY(_locationManage); RELEASE_SAFELY(_allCityWeatherIDArray); RELEASE_SAFELY(_weatherInterFace); [super dealloc]; }- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // Initialization code [self createCustomWeaterView]; } return self;}- (void)createCustomWeaterView{#pragma mark - 天气图像 _weatherImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 42)]; _weatherImage.backgroundColor = [UIColor blackColor]; _weatherImage.userInteractionEnabled = YES; [self addSubview:_weatherImage]; [_weatherImage release]; #pragma mark - 天气温度 _temperatureLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 40, 22)]; _temperatureLabel.textAlignment = NSTextAlignmentCenter; _temperatureLabel.font = [UIFont systemFontOfSize:7.0]; [self addSubview:_temperatureLabel]; [_temperatureLabel release];#pragma mark - 地方名称 _loactionNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 22, 40, 20)]; _loactionNameLabel.textAlignment = NSTextAlignmentCenter; _loactionNameLabel.font = [UIFont systemFontOfSize:10.0]; [self addSubview:_loactionNameLabel]; [_loactionNameLabel release]; #pragma mark - 地图GPS定位 [self createGPSMap];#pragma mark - 获得所有城市天气ID数组的方法 [self getAllCityWeatherIDArray]; }- (void)getAllCityWeatherIDArray{ self.allCityWeatherIDArray = [NSMutableArray array]; // 从应用程序里面提取JSON数据 NSData * idData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cities" ofType:@"json"]]; NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:idData options:NSJSONReadingMutableContainers error:nil]; NSArray * allArray = [dic objectForKey:@"城市代码"]; for (int i = 0; i < [allArray count]; i++) { // 获得所有省的数据字典 NSDictionary * provinceDic = [allArray objectAtIndex:i]; // 获得省对应市的数组信息 NSArray * cityArray = [provinceDic objectForKey:@"市"]; for (int j = 0; j < [cityArray count]; j++) { // 获得全国各地市级的名称和天气请求ID NSDictionary * cityAndIdDic = [cityArray objectAtIndex:j]; [self.allCityWeatherIDArray addObject:cityAndIdDic]; } } }#pragma mark - 获得天气情况- (void)getWeatherInterFaceWithCityName:(NSString *)city{ // 获得请求天气的接口 NSString * cityFitJson = [city substringWithRange:NSMakeRange(0,2)]; for (int i = 0; i < [_allCityWeatherIDArray count]; i++) { NSDictionary * cityDic = [_allCityWeatherIDArray objectAtIndex:i]; NSString * weatherCity = [cityDic objectForKey:@"市名"]; if ([cityFitJson isEqualToString:weatherCity]) { NSString * cityId = [cityDic objectForKey:@"编码"]; NSString * cityIdWithHtml = [cityId stringByAppendingString:@".html"]; self.weatherInterFace = [WeatherPublic stringByAppendingString:cityIdWithHtml]; } } // 用获得的接口进行网络请求 _request = [[HMTMyCustomNetRequest alloc] init]; [_request requestForGETWithUrl:_weatherInterFace]; [_request setFinishLoadBlock:^(NSData * data){ NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSDictionary * weatherinfoDic = [dic objectForKey:@"weatherinfo"]; _temperatureLabel.text = [weatherinfoDic objectForKey:@"temp1"]; }]; }- (void)createGPSMap{ // 初始化位置服务 self.locationManage = [[CLLocationManager alloc]init]; // 要求CLLocationManager对象返回全部信息 _locationManage.distanceFilter = kCLDistanceFilterNone; // 设置定位精度 _locationManage.desiredAccuracy = kCLLocationAccuracyBest; // 设置代理 _locationManage.delegate = self; // 开始定位 [_locationManage startUpdatingLocation]; [_locationManage release]; }- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocation * newLocation = [locations lastObject]; // 停止实时定位 [_locationManage stopUpdatingLocation]; // -----------------------位置反编码------------------------ CLGeocoder * geocoder = [[CLGeocoder alloc]init]; [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { for (CLPlacemark * place in placemarks) { NSLog(@"name = %@",place.name); // 位置名 NSLog(@"thoroughfare = %@",place.thoroughfare); // 街道 NSLog(@"subAdministrativeArea = %@",place.subAdministrativeArea); // 子街道 NSLog(@"locality = %@",place.locality); // 市 NSLog(@"subLocality = %@",place.subLocality); // 区 NSLog(@"country = %@",place.country);// 国家 NSArray *allKeys = place.addressDictionary.allKeys; for (NSString *key in allKeys) { NSLog(@"key = %@, value = %@",key, place.addressDictionary[key]); }#pragma mark - 使用系统定义的字符串直接查询,记得导入AddressBook框架 NSLog(@"kABPersonAddressCityKey = %@", (NSString *)kABPersonAddressCityKey); NSLog(@"city = %@", place.addressDictionary[(NSString *)kABPersonAddressCityKey]); NSString * city = place.locality; NSString * state = place.addressDictionary[(NSString *)kABPersonAddressStateKey]; NSLog(@"state = %@",state); if(city == nil) { city = place.addressDictionary[(NSString *)kABPersonAddressStateKey]; } _loactionNameLabel.text = [city substringWithRange:NSMakeRange(0,2)]; // 获得所在地后,传入城市名 [self getWeatherInterFaceWithCityName:city]; } }]; }@end
0 0
- 根据中国天气网,自动定位获得当地天气(数据是JSON的)---不过这个接口貌似不更新了
- 获取中国天气网天气预报json数据的api接口
- 获得当地实时天气
- Android解析中国天气接口JSon数据,应用于天气查询!
- 中国天气api接口,json数据
- 根据IP获取当地天气
- 根据IP获取当地天气
- 根据天气接口获取其中的数据
- 想在自己的android应用中获得当天的天气情况,这该怎么做呢?不用担心。中国国家气象局提供了获取所在城市天气预报信息接口。通过这个接口,我们就可以获取天气信息了。
- Android解析中国天气网的Json数据
- Json获取中国天气网天气的代码
- 中国天气网的中央气象台实时数据接口
- 中国天气网的中央气象台实时数据接口
- 中国天气网的中央气象台实时数据接口
- 中国天气网的中央气象台实时数据接口
- 中国天气网的中央气象台实时数据接口之管中窥豹
- 腾讯、新浪、淘宝、搜狐的IP库接口,根据IP显示当地的天气功能
- 【汇总】中国天气网提供的天气开发api接口
- c++实现用欧几里得算法求两个整数的最大公约数
- 夏书祥-iphone手机怎么安装多个QQ
- C#调用wps
- 如何将eclipse里面的程序打包成jar
- Leetcode 树 Binary Tree Postorder Traversal
- 根据中国天气网,自动定位获得当地天气(数据是JSON的)---不过这个接口貌似不更新了
- 播放拍照声音
- 表格布局
- 【WinEdt学习笔记】第一课
- 当报酬与劳动不对等时
- QT提取文件中单个字符串
- 做好网站优化的投票排名算法
- scrollView中edittext抢焦点问题
- SP2010开发和VS2010专家"食谱"--第七章节--使用客户端对象模型(2)--Handling exceptions