php定位和天气,iOS定位和获取当前天气(示例代码)

1 #import "CHMLocationManager.h"

2 #import

3 #import "MBProgressHUD+Custom.h"

4 #import "MLNetworkingManager.h"

5 #import "NSString+Custom.h"

6 #import "CHMSeniverseResultModel.h"

7

8 #define kSeniverseAPI @"bfdvxqnyj8k2sooo"

9

10 @interface CHMLocationManager ()

11

12 @property (nonatomic, strong) CLLocationManager *locationManager;13 @property (nonatomic, strong) CLGeocoder *geocoder;14

15 @end

16

17 @implementationCHMLocationManager18

19 +(instancetype)sharedInstance {20 static CHMLocationManager *locationManager =nil;21 staticdispatch_once_t onceToken;22 dispatch_once(&onceToken, ^{23 locationManager =[[CHMLocationManager alloc] init];24 });25 returnlocationManager;26 }27

28 - (void)xm_initialize {29 self.locationManager =[[CLLocationManager alloc] init];30 self.geocoder =[[CLGeocoder alloc] init];31 self.locationManager.delegate =self;32 self.locationManager.desiredAccuracy =kCLLocationAccuracyThreeKilometers;33 self.refreshSubject =[RACSubject subject];34 }35

36 - (void)startLocation {37 if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {38 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.f) {39 [self.locationManager requestWhenInUseAuthorization];40 }41 }42

43 [self.locationManager startUpdatingLocation];44

45 if ([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] ==kCLAuthorizationStatusDenied) {46 self.currentCity = NSLocalizedString(@"打开定位", nil);47 self.currentLocationAbility =NO;48 } else{49 self.currentLocationAbility =YES;50 }51 }52

53 #pragma mark - CLLocationManagerDelegate

54 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {55 CLLocation *location =[locations lastObject];56 CLLocationCoordinate2D coordinate =location.coordinate;57

58 self.latitude =coordinate.latitude;59 self.longitude =coordinate.longitude;60 DebugLog(@"当前位置信息 %lf %lf", self.latitude, self.longitude);61

62 [manager stopUpdatingLocation];63

64 [self reverseCurrentGeocodeLocation:location];65 }66

67 - (void)reverseCurrentGeocodeLocation:(CLLocation *)location {68 @weakify(self);69 [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray * _Nullable placemarks, NSError *_Nullable error) {70 @strongify(self);71 if (placemarks.count > 0) {72 CLPlacemark *place =[placemarks firstObject];73 if (place !=nil) {74 if (place.locality && place.locality.length > 0) {75 self.currentCity =place.locality;76 } else{77 //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)

78 self.currentCity =place.administrativeArea;79 }80

81 [self.weatherDataCommand execute:@{@"location": [NSString stringWithFormat:@"%.2f:%.2f", self.latitude, self.longitude], @"language": [NSString currentLanguageForSeniverse]}];82 } else{83 DebugLog(@"CLPlacemark place 是 nil");84 self.currentCity = NSLocalizedString(@"位置未知", nil);85 }86 } else if (error == nil && placemarks.count == 0) {87 DebugLog(@"没有定位结果返回");88 self.currentCity = NSLocalizedString(@"没有定位信息", nil);89 } else{90 DebugLog(@"反地理编码失败 %@", error);91 }92 [self.refreshSubject sendNext:self.currentCity];93 }];94 }95

96 - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {97 [manager stopUpdatingLocation];98

99 switch(error.code) {100 casekCLErrorDenied:101 {102 DebugLog(@"用户拒绝了对位置或测距的访问权限");103 self.currentCity = NSLocalizedString(@"打开定位", nil);104 }105 break;106 default:107 {108 [MBProgressHUD showBriefPoint:NSLocalizedString(@"定位失败", nil)];109 self.currentCity = NSLocalizedString(@"定位失败", nil);110 }111 break;112 }113 }114

115 #pragma mark - Setter Getter

116 - (CHMSeniverseResultModel *)seniverseResultModel {117 if (!_seniverseResultModel) {118 _seniverseResultModel =[[CHMSeniverseResultModel alloc] init];119 }120 return_seniverseResultModel;121 }122

123 - (RACCommand *)weatherDataCommand {124 if (!_weatherDataCommand) {125 _weatherDataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSDictionary *dic) {126

127 DebugLog(@"请求当前的天气情况 %@", dic);128

129 return [RACSignal createSignal:^RACDisposable *(idsubscriber) {130

131 MLURLSessionTask *task = [MLNetworkingManager requestWeatherWithAPI:kSeniverseAPI location:[dic objectForKey:@"location"] language:[dic objectForKey:@"language"] unit:@"c" success:^(CHMSeniverseResultModel *response) {132 [CHMLocationManager sharedInstance].seniverseResultModel =response;133 [subscriber sendNext:response];134 [subscriber sendCompleted];135 } fail:^(NSError *error) {136 [subscriber sendError:error];137 }];138

139 return [RACDisposable disposableWithBlock:^{140 [task cancel];141 }];142 }];143 }];144 }145 return_weatherDataCommand;146 }147

148 @end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值