#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