EditSaveAddressVC.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. //
  2. // EditSaveAddressVC.m
  3. // yunbaolive
  4. //
  5. // Created by ybRRR on 2020/2/24.
  6. // Copyright © 2020 cat. All rights reserved.
  7. //
  8. #import "EditSaveAddressVC.h"
  9. @import CoreLocation;
  10. @interface EditSaveAddressVC ()<UIPickerViewDelegate, UIPickerViewDataSource,CLLocationManagerDelegate>
  11. {
  12. UIView *cityPickBack;
  13. UIPickerView *cityPicker;
  14. //省市区-数组
  15. NSArray *province;
  16. NSArray *city;
  17. NSArray *district;
  18. //省市区-字符串
  19. NSString *provinceStr;
  20. NSString *cityStr;
  21. NSString *districtStr;
  22. NSDictionary *areaDic;
  23. NSString *selectedProvince;
  24. NSString *normalProvince;
  25. NSString *normalCity;
  26. NSString *normalDistrict;
  27. CLLocationManager *_lbsManager;
  28. }
  29. @property (nonatomic, strong)MyTextView *nameText;
  30. @property (nonatomic, strong)MyTextView *phoneText;
  31. @property (nonatomic, strong)MyTextView *areaText;
  32. @property (nonatomic, strong)MyTextView *addressText;
  33. @end
  34. @implementation EditSaveAddressVC
  35. -(void)initData{
  36. provinceStr = self.addModel.receiver_province;
  37. cityStr = self.addModel.receiver_city;
  38. districtStr = self.addModel.receiver_area;
  39. normalProvince = @"";
  40. normalCity = @"";
  41. normalDistrict = @"";
  42. NSBundle *bundle = [NSBundle mainBundle];
  43. NSString *plistPath = [bundle pathForResource:@"area" ofType:@"plist"];
  44. areaDic = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
  45. NSArray *components = [areaDic allKeys];
  46. NSArray *sortedArray = [components sortedArrayUsingComparator: ^(id obj1, id obj2) {
  47. if ([obj1 integerValue] > [obj2 integerValue]) {
  48. return (NSComparisonResult)NSOrderedDescending;
  49. }
  50. if ([obj1 integerValue] < [obj2 integerValue]) {
  51. return (NSComparisonResult)NSOrderedAscending;
  52. }
  53. return (NSComparisonResult)NSOrderedSame;
  54. }];
  55. NSMutableArray *provinceTmp = [[NSMutableArray alloc] init];
  56. for (int i=0; i<[sortedArray count]; i++) {
  57. NSString *index = [sortedArray objectAtIndex:i];
  58. NSArray *tmp = [[areaDic objectForKey: index] allKeys];
  59. [provinceTmp addObject: [tmp objectAtIndex:0]];
  60. }
  61. //---> //rk_3-7 修复首次加载问题
  62. province = [[NSArray alloc] initWithArray: provinceTmp];
  63. NSString *index = [sortedArray objectAtIndex:0];
  64. //NSString *selected = [province objectAtIndex:0];
  65. selectedProvince = [province objectAtIndex:0];
  66. NSDictionary *proviceDic = [NSDictionary dictionaryWithDictionary: [[areaDic objectForKey:index]objectForKey:selectedProvince]];
  67. NSArray *cityArray = [proviceDic allKeys];
  68. NSDictionary *cityDic = [NSDictionary dictionaryWithDictionary: [proviceDic objectForKey: [cityArray objectAtIndex:0]]];
  69. //city = [[NSArray alloc] initWithArray: [cityDic allKeys]];
  70. NSArray *citySortedArray = [cityArray sortedArrayUsingComparator: ^(id obj1, id obj2) {
  71. if ([obj1 integerValue] > [obj2 integerValue]) {
  72. return (NSComparisonResult)NSOrderedDescending;//递减
  73. }
  74. if ([obj1 integerValue] < [obj2 integerValue]) {
  75. return (NSComparisonResult)NSOrderedAscending;//上升
  76. }
  77. return (NSComparisonResult)NSOrderedSame;
  78. }];
  79. NSMutableArray *m_array = [[NSMutableArray alloc] init];
  80. for (int i=0; i<[citySortedArray count]; i++) {
  81. NSString *index = [citySortedArray objectAtIndex:i];
  82. NSArray *temp = [[proviceDic objectForKey: index] allKeys];
  83. [m_array addObject: [temp objectAtIndex:0]];
  84. }
  85. city = [NSArray arrayWithArray:m_array];
  86. //<-----------
  87. NSString *selectedCity = [city objectAtIndex: 0];
  88. district = [[NSArray alloc] initWithArray: [cityDic objectForKey: selectedCity]];
  89. [self location];
  90. }
  91. - (UIStatusBarStyle)preferredStatusBarStyle {
  92. if (@available(iOS 13.0,*)) {
  93. return UIStatusBarStyleDarkContent;
  94. }
  95. return UIStatusBarStyleDefault;
  96. }
  97. - (void)viewDidLoad {
  98. [super viewDidLoad];
  99. self.titleL.text =YZMsg(@"编辑收货地址");
  100. // self.view.backgroundColor = Normal_BackColor;
  101. self.view.backgroundColor = RGB(250, 250, 250);
  102. self.subNavi.backgroundColor = UIColor.whiteColor;
  103. self.titleL.textColor = UIColor.blackColor;
  104. [self.leftBtn setImage:[UIImage imageNamed:@"pub_back_black"] forState:0];
  105. self.naviLine.hidden = NO;
  106. self.naviLine.backgroundColor = RGB(245, 245, 245);
  107. [self initData];
  108. [self createUI];
  109. UIButton *saveBtn = [UIButton buttonWithType:0];
  110. saveBtn.frame = CGRectMake(12, _window_height-60, _window_width-24, 40);
  111. [saveBtn setBackgroundColor:Pink_Cor];
  112. [saveBtn setTitle:YZMsg(@"保存") forState:0];
  113. [saveBtn setTitleColor:[UIColor whiteColor] forState:0];
  114. saveBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  115. saveBtn.layer.cornerRadius = 5;
  116. saveBtn.layer.masksToBounds = YES;
  117. [saveBtn addTarget:self action:@selector(saveBtnClick) forControlEvents:UIControlEventTouchUpInside];
  118. [self.view addSubview:saveBtn];
  119. }
  120. -(void)createUI{
  121. UIView *backView = [[UIView alloc]init];
  122. backView.frame = CGRectMake(0, 64+statusbarHeight, _window_width, 250);
  123. backView.backgroundColor = UIColor.whiteColor;
  124. [self.view addSubview:backView];
  125. for (int i = 0; i < 4; i ++) {
  126. if (i <= 2) {
  127. MyTextView *mt = [[MyTextView alloc]init];
  128. mt.frame = CGRectMake(12, 10+i *50, _window_width-24, 30);
  129. mt.text = [NSString stringWithFormat:@"%@%i%@",YZMsg(@"这是第"),i,YZMsg(@"行")];
  130. mt.font = [UIFont systemFontOfSize:14];
  131. mt.textColor = [UIColor blackColor];
  132. mt.backgroundColor = [UIColor clearColor];
  133. [backView addSubview:mt];
  134. UILabel *line = [[UILabel alloc]init];
  135. line.frame = CGRectMake(12, mt.bottom+10, _window_width-24, 1);
  136. line.backgroundColor = RGB(245, 245, 245);
  137. [backView addSubview:line];
  138. switch (i) {
  139. case 0:
  140. self.nameText = mt;
  141. self.nameText.text = self.addModel.receiver;
  142. break;
  143. case 1:
  144. self.phoneText = mt;
  145. self.phoneText.text = self.addModel.receiver_phone;
  146. break;
  147. case 2:{
  148. self.areaText = mt;
  149. self.areaText.userInteractionEnabled = NO;
  150. self.areaText.text = [NSString stringWithFormat:@"%@ %@ %@",self.addModel.receiver_province,self.addModel.receiver_city,self.addModel.receiver_area];
  151. UIImageView *rightImg = [[UIImageView alloc]init];
  152. rightImg.frame = CGRectMake(_window_width-30, 17+i *50, 16, 16);
  153. rightImg.image = [UIImage imageNamed:@"shop_right"];
  154. [backView addSubview:rightImg];
  155. UIButton *selectAdd = [UIButton buttonWithType:0];
  156. selectAdd.frame = mt.frame;
  157. [selectAdd addTarget:self action:@selector(selectAddressClick) forControlEvents:UIControlEventTouchUpInside];
  158. [backView addSubview:selectAdd];
  159. }
  160. break;
  161. default:
  162. break;
  163. }
  164. }else{
  165. MyTextView *mt = [[MyTextView alloc]init];
  166. mt.frame = CGRectMake(12, 10+i *50, _window_width-24, 80);
  167. mt.font = [UIFont systemFontOfSize:14];
  168. mt.backgroundColor = UIColor.whiteColor;
  169. mt.textColor = [UIColor blackColor];
  170. [backView addSubview:mt];
  171. self.addressText = mt;
  172. self.addressText.text = self.addModel.receiver_address;
  173. }
  174. }
  175. }
  176. -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  177. {
  178. [self.view endEditing: YES];
  179. cityPickBack.hidden = YES;
  180. }
  181. #pragma mark------保存--------
  182. -(void)saveBtnClick{
  183. NSString *url = [purl stringByAppendingFormat:@"?service=Seller.upReceiverAddress"];
  184. NSDictionary *signdic = @{@"uid":[Config getOwnID],@"token":[Config getOwnToken], @"time":[NSNumber numberWithLong: (long)[[NSDate date] timeIntervalSince1970]]};
  185. NSString *sign = [PublicObj sortString:signdic];
  186. NSDictionary *dic = @{
  187. @"uid":[Config getOwnID],
  188. @"token":[Config getOwnToken],
  189. @"receiver":self.nameText.text,
  190. @"receiver_phone":self.phoneText.text,
  191. @"receiver_province":provinceStr,
  192. @"receiver_city":cityStr,
  193. @"receiver_area":districtStr,
  194. @"receiver_address":self.addressText.text,
  195. @"time":[NSNumber numberWithLong: (long)[[NSDate date] timeIntervalSince1970]],
  196. @"sign":sign
  197. };
  198. [YBNetworking postWithUrl:@"Seller.upReceiverAddress" Dic:dic Suc:^(int code, id info, NSString *msg) {
  199. if (code ==0) {
  200. if (self.sellAddressEvent) {
  201. self.sellAddressEvent();
  202. }
  203. [MBProgressHUD showError:msg];
  204. [self.navigationController popViewControllerAnimated:YES];
  205. }else{
  206. [MBProgressHUD showError:msg];
  207. }
  208. } Fail:^(id fail) {
  209. }];
  210. }
  211. #pragma mark------选择地址--------
  212. -(void)selectAddressClick{
  213. [self.view endEditing:YES];
  214. if (!cityPickBack) {
  215. cityPickBack = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _window_width, _window_height)];
  216. cityPickBack.backgroundColor = RGB_COLOR(@"#000000", 0.3);
  217. [self.view addSubview:cityPickBack];
  218. UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(15, _window_height-240, _window_width-30, 40)];
  219. titleView.backgroundColor = [UIColor whiteColor];
  220. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:titleView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
  221. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  222. maskLayer.frame = titleView.bounds;
  223. maskLayer.path = maskPath.CGPath;
  224. titleView.layer.mask = maskLayer;
  225. [cityPickBack addSubview:titleView];
  226. [PublicObj lineViewWithFrame:CGRectMake(0,titleView.height-1, titleView.width, 1) andColor:RGB(240, 240, 240) andView:titleView];
  227. UIButton *cancleBtn = [UIButton buttonWithType:0];
  228. cancleBtn.frame = CGRectMake(10, 0, 80, 40);
  229. cancleBtn.tag = 100;
  230. [cancleBtn setTitle:YZMsg(@"取消") forState:0];
  231. cancleBtn.titleLabel.font = [UIFont systemFontOfSize:15];
  232. [cancleBtn setTitleColor:[UIColor blackColor] forState:0];
  233. [cancleBtn addTarget:self action:@selector(cityCancleOrSure:) forControlEvents:UIControlEventTouchUpInside];
  234. [titleView addSubview:cancleBtn];
  235. UIButton *sureBtn = [UIButton buttonWithType:0];
  236. sureBtn.frame = CGRectMake(titleView.width-90, 0, 80, 40);
  237. sureBtn.tag = 101;
  238. [sureBtn setTitle:YZMsg(@"确定") forState:0];
  239. sureBtn.titleLabel.font = [UIFont systemFontOfSize:15];
  240. [sureBtn setTitleColor:Pink_Cor forState:0];
  241. [sureBtn addTarget:self action:@selector(cityCancleOrSure:) forControlEvents:UIControlEventTouchUpInside];
  242. [titleView addSubview:sureBtn];
  243. UILabel *titleLb = [[UILabel alloc]initWithFrame:CGRectMake(titleView.width/2-90, 0, 180, 40)];
  244. titleLb.font = [UIFont systemFontOfSize:15];
  245. titleLb.textColor = [UIColor blackColor];
  246. titleLb.textAlignment = NSTextAlignmentCenter;
  247. titleLb.text = YZMsg(@"选择地区");
  248. [titleView addSubview:titleLb];
  249. cityPicker = [[UIPickerView alloc]initWithFrame:CGRectMake(15, _window_height-200, _window_width-30, 190)];
  250. cityPicker.backgroundColor = [UIColor whiteColor];
  251. cityPicker.delegate = self;
  252. cityPicker.dataSource = self;
  253. cityPicker.showsSelectionIndicator = YES;
  254. [cityPicker selectRow: 0 inComponent: 0 animated: YES];
  255. [cityPickBack addSubview:cityPicker];
  256. UIBezierPath *maskPath2 = [UIBezierPath bezierPathWithRoundedRect:cityPicker.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
  257. CAShapeLayer *maskLayer2 = [[CAShapeLayer alloc] init];
  258. maskLayer2.frame = cityPicker.bounds;
  259. maskLayer2.path = maskPath2.CGPath;
  260. cityPicker.layer.mask = maskLayer2;
  261. [self setLocationAddress];
  262. }else{
  263. cityPickBack.hidden = NO;
  264. }
  265. }
  266. - (void)cityCancleOrSure:(UIButton *)button{
  267. if (button.tag == 100) {
  268. //return;
  269. }else{
  270. NSInteger provinceIndex = [cityPicker selectedRowInComponent: 0];
  271. NSInteger cityIndex = [cityPicker selectedRowInComponent: 1];
  272. NSInteger districtIndex = [cityPicker selectedRowInComponent: 2];
  273. provinceStr = [province objectAtIndex: provinceIndex];
  274. cityStr = [city objectAtIndex: cityIndex];
  275. districtStr = [district objectAtIndex:districtIndex];
  276. NSString *dizhi = [NSString stringWithFormat:@"%@ %@ %@",provinceStr,cityStr,districtStr];
  277. self.areaText.text = dizhi;
  278. }
  279. cityPickBack.hidden = YES;
  280. }
  281. #pragma mark- Picker Data Source Methods
  282. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
  283. if (pickerView == cityPicker) {
  284. return 3;
  285. }
  286. return 0;
  287. }
  288. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
  289. if (pickerView == cityPicker) {
  290. if (component == 0) {
  291. return [province count];
  292. }
  293. else if (component == 1) {
  294. return [city count];
  295. }
  296. else {
  297. return [district count];
  298. }
  299. }else{
  300. return 100;
  301. }
  302. }
  303. #pragma mark- Picker Delegate Methods
  304. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
  305. if (pickerView == cityPicker) {
  306. if (component == 0) {
  307. return [province objectAtIndex: row];
  308. }
  309. else if (component == 1) {
  310. return [city objectAtIndex: row];
  311. }
  312. else {
  313. return [district objectAtIndex: row];
  314. }
  315. }else{
  316. return nil;
  317. }
  318. }
  319. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
  320. if (pickerView == cityPicker) {
  321. if (component == 0) {
  322. selectedProvince = [province objectAtIndex: row];
  323. NSDictionary *tmp = [NSDictionary dictionaryWithDictionary: [areaDic objectForKey: [NSString stringWithFormat:@"%ld", row]]];
  324. NSDictionary *dic = [NSDictionary dictionaryWithDictionary: [tmp objectForKey: selectedProvince]];
  325. NSArray *cityArray = [dic allKeys];
  326. NSArray *sortedArray = [cityArray sortedArrayUsingComparator: ^(id obj1, id obj2) {
  327. if ([obj1 integerValue] > [obj2 integerValue]) {
  328. return (NSComparisonResult)NSOrderedDescending;//递减
  329. }
  330. if ([obj1 integerValue] < [obj2 integerValue]) {
  331. return (NSComparisonResult)NSOrderedAscending;//上升
  332. }
  333. return (NSComparisonResult)NSOrderedSame;
  334. }];
  335. NSMutableArray *array = [[NSMutableArray alloc] init];
  336. for (int i=0; i<[sortedArray count]; i++) {
  337. NSString *index = [sortedArray objectAtIndex:i];
  338. NSArray *temp = [[dic objectForKey: index] allKeys];
  339. [array addObject: [temp objectAtIndex:0]];
  340. }
  341. city = [[NSArray alloc] initWithArray: array];
  342. NSDictionary *cityDic = [dic objectForKey: [sortedArray objectAtIndex: 0]];
  343. district = [[NSArray alloc] initWithArray: [cityDic objectForKey: [city objectAtIndex: 0]]];
  344. [cityPicker selectRow: 0 inComponent: 1 animated: YES];
  345. [cityPicker selectRow: 0 inComponent: 2 animated: YES];
  346. [cityPicker reloadComponent: 1];
  347. [cityPicker reloadComponent: 2];
  348. } else if (component == 1) {
  349. NSString *provinceIndex = [NSString stringWithFormat: @"%ld", [province indexOfObject: selectedProvince]];
  350. NSDictionary *tmp = [NSDictionary dictionaryWithDictionary: [areaDic objectForKey: provinceIndex]];
  351. NSDictionary *dic = [NSDictionary dictionaryWithDictionary: [tmp objectForKey: selectedProvince]];
  352. NSArray *dicKeyArray = [dic allKeys];
  353. NSArray *sortedArray = [dicKeyArray sortedArrayUsingComparator: ^(id obj1, id obj2) {
  354. if ([obj1 integerValue] > [obj2 integerValue]) {
  355. return (NSComparisonResult)NSOrderedDescending;
  356. }
  357. if ([obj1 integerValue] < [obj2 integerValue]) {
  358. return (NSComparisonResult)NSOrderedAscending;
  359. }
  360. return (NSComparisonResult)NSOrderedSame;
  361. }];
  362. NSDictionary *cityDic = [NSDictionary dictionaryWithDictionary: [dic objectForKey: [sortedArray objectAtIndex: row]]];
  363. NSArray *cityKeyArray = [cityDic allKeys];
  364. district = [[NSArray alloc] initWithArray: [cityDic objectForKey: [cityKeyArray objectAtIndex:0]]];
  365. [cityPicker selectRow: 0 inComponent: 2 animated: YES];
  366. [cityPicker reloadComponent: 2];
  367. }
  368. }else{
  369. }
  370. }
  371. - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
  372. if (component == 0) {
  373. return 80;
  374. }
  375. else if (component == 1) {
  376. return 100;
  377. }
  378. else {
  379. return 115;
  380. }
  381. }
  382. - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
  383. UILabel *myView = nil;
  384. if (pickerView == cityPicker) {
  385. if (component == 0) {
  386. myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, _window_width/3, 30)];
  387. myView.textAlignment = NSTextAlignmentCenter;
  388. myView.text = [province objectAtIndex:row];
  389. myView.font = [UIFont systemFontOfSize:14];
  390. myView.backgroundColor = [UIColor clearColor];
  391. }
  392. else if (component == 1) {
  393. myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, _window_width/3, 30)];
  394. myView.textAlignment = NSTextAlignmentCenter;
  395. myView.text = [city objectAtIndex:row];
  396. myView.font = [UIFont systemFontOfSize:14];
  397. myView.backgroundColor = [UIColor clearColor];
  398. }
  399. else {
  400. myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, _window_width/3, 30)];
  401. myView.textAlignment = NSTextAlignmentCenter;
  402. myView.text = [district objectAtIndex:row];
  403. myView.font = [UIFont systemFontOfSize:14];
  404. myView.backgroundColor = [UIColor clearColor];
  405. }
  406. }
  407. return myView;
  408. }
  409. -(void)setLocationAddress{
  410. int provinceIndex = 0;
  411. int cityIndex = 0;
  412. NSArray *components = [areaDic allKeys];
  413. NSArray *sortedArray = [components sortedArrayUsingComparator: ^(id obj1, id obj2) {
  414. if ([obj1 integerValue] > [obj2 integerValue]) {
  415. return (NSComparisonResult)NSOrderedDescending;
  416. }
  417. if ([obj1 integerValue] < [obj2 integerValue]) {
  418. return (NSComparisonResult)NSOrderedAscending;
  419. }
  420. return (NSComparisonResult)NSOrderedSame;
  421. }];
  422. NSMutableArray *provinceTmp = [[NSMutableArray alloc] init];
  423. for (int i=0; i<[sortedArray count]; i++) {
  424. NSString *index = [sortedArray objectAtIndex:i];
  425. NSArray *tmp = [[areaDic objectForKey: index] allKeys];
  426. [provinceTmp addObject: [tmp objectAtIndex:0]];
  427. }
  428. if (normalProvince.length > 0) {
  429. selectedProvince = normalProvince;
  430. for (int i = 0; i < province.count; i ++) {
  431. if ([normalProvince isEqual:province[i]]) {
  432. provinceIndex = i;
  433. NSString *index = [sortedArray objectAtIndex:i];
  434. NSDictionary *proviceDic = [NSDictionary dictionaryWithDictionary: [[areaDic objectForKey:index]objectForKey:normalProvince]];
  435. NSArray *cityArray = [proviceDic allKeys];
  436. // NSDictionary *cityDic = [NSDictionary dictionaryWithDictionary: [proviceDic objectForKey: [cityArray objectAtIndex:i]]];
  437. NSArray *citySortedArray = [cityArray sortedArrayUsingComparator: ^(id obj1, id obj2) {
  438. if ([obj1 integerValue] > [obj2 integerValue]) {
  439. return (NSComparisonResult)NSOrderedDescending;//递减
  440. }
  441. if ([obj1 integerValue] < [obj2 integerValue]) {
  442. return (NSComparisonResult)NSOrderedAscending;//上升
  443. }
  444. return (NSComparisonResult)NSOrderedSame;
  445. }];
  446. NSMutableArray *m_array = [[NSMutableArray alloc] init];
  447. for (int i=0; i<[citySortedArray count]; i++) {
  448. NSString *index = [citySortedArray objectAtIndex:i];
  449. NSArray *temp = [[proviceDic objectForKey: index] allKeys];
  450. [m_array addObject: [temp objectAtIndex:0]];
  451. }
  452. NSArray *cityArr = [NSArray arrayWithArray:m_array];
  453. city =[NSArray arrayWithArray:m_array];
  454. for (int j = 0; j < cityArr.count; j ++) {
  455. if ([normalCity isEqual:cityArr[j]]) {
  456. cityIndex = j;
  457. NSString *keys = [NSString stringWithFormat:@"%d",cityIndex];
  458. NSDictionary *dicssss = [NSDictionary dictionaryWithDictionary: [proviceDic objectForKey: keys]];
  459. NSString *selectedCity = [cityArr objectAtIndex: j];
  460. district = [[NSArray alloc] initWithArray: [dicssss objectForKey: selectedCity]];
  461. NSArray * districtArr = [[NSArray alloc] initWithArray: [dicssss objectForKey: selectedCity]];
  462. for (int k = 0; k <districtArr.count; k ++) {
  463. if ([normalDistrict isEqual:districtArr[k]]) {
  464. [cityPicker selectRow: provinceIndex inComponent: 0 animated: YES];
  465. [cityPicker reloadComponent: 1];
  466. [cityPicker selectRow: cityIndex inComponent: 1 animated: YES];
  467. [cityPicker reloadComponent: 2];
  468. [cityPicker selectRow: k inComponent: 2 animated: YES];
  469. }
  470. }
  471. }
  472. }
  473. }
  474. }
  475. }
  476. NSLog(@"province===:%@ \n city:%@ \ndistrict:%@",province,city,district);
  477. }
  478. -(void)location{
  479. if (!_lbsManager) {
  480. _lbsManager = [[CLLocationManager alloc] init];
  481. [_lbsManager setDesiredAccuracy:kCLLocationAccuracyBest];
  482. _lbsManager.delegate = self;
  483. // 兼容iOS8定位
  484. CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
  485. if (kCLAuthorizationStatusDenied == status || kCLAuthorizationStatusRestricted == status) {
  486. NSLog(@"请打开您的位置服务!");
  487. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  488. UIAlertController *alertContro = [UIAlertController alertControllerWithTitle:nil message:[NSString stringWithFormat:YZMsg(@"打开“定位服务”来允许“%@”确定您的位置"),[infoDictionary objectForKey:@"CFBundleDisplayName"]] preferredStyle:UIAlertControllerStyleAlert];
  489. UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:YZMsg(@"设置") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  490. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
  491. }];
  492. [alertContro addAction:cancleAction];
  493. [cancleAction setValue:Normal_Color forKey:@"_titleTextColor"];
  494. UIAlertAction *sureAction = [UIAlertAction actionWithTitle:YZMsg(@"取消") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  495. }];
  496. [alertContro addAction:sureAction];
  497. [sureAction setValue:[UIColor blackColor] forKey:@"_titleTextColor"];
  498. [[[YBBaseAppDelegate sharedAppDelegate]topViewController]presentViewController:alertContro animated:YES completion:nil];
  499. }else{
  500. SEL requestSelector = NSSelectorFromString(@"requestWhenInUseAuthorization");
  501. if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined && [_lbsManager respondsToSelector:requestSelector]) {
  502. [_lbsManager requestWhenInUseAuthorization]; //调用了这句,就会弹出允许框了.
  503. } else {
  504. [_lbsManager startUpdatingLocation];
  505. }
  506. }
  507. }
  508. }
  509. - (void)stopLbs {
  510. [_lbsManager stopUpdatingHeading];
  511. _lbsManager.delegate = nil;
  512. _lbsManager = nil;
  513. }
  514. - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
  515. if (status == kCLAuthorizationStatusRestricted || status == kCLAuthorizationStatusDenied) {
  516. [self stopLbs];
  517. } else {
  518. [_lbsManager startUpdatingLocation];
  519. }
  520. }
  521. - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
  522. {
  523. [self stopLbs];
  524. }
  525. - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
  526. {
  527. CLLocation *newLocatioin = locations[0];
  528. //zl---待修改
  529. // liveCity *cityU = [cityDefault myProfile];
  530. // cityU.lat = [NSString stringWithFormat:@"%f",newLocatioin.coordinate.latitude];
  531. // cityU.lng = [NSString stringWithFormat:@"%f",newLocatioin.coordinate.longitude];
  532. CLGeocoder *geocoder = [[CLGeocoder alloc] init];
  533. [geocoder reverseGeocodeLocation:newLocatioin completionHandler:^(NSArray *placemarks, NSError *error) {
  534. if (!error)
  535. {
  536. CLPlacemark *placeMark = placemarks[0];
  537. NSString *addr = [NSString stringWithFormat:@"%@%@%@",placeMark.administrativeArea,placeMark.locality,placeMark.subLocality];
  538. NSLog(@"hhhhhhhh----:%@", addr);
  539. normalProvince =placeMark.administrativeArea;
  540. normalCity = placeMark.locality;
  541. normalDistrict = placeMark.subLocality;
  542. }
  543. }];
  544. [self stopLbs];
  545. }
  546. @end