// // RailSearchAddressVC.m // LekangGuard // // Created by ecell on 2023/12/9. // #import "RailSearchAddressVC.h" #import "RailSearchAddressTableViewCell.h" @interface RailSearchAddressVC () @property (nonatomic, strong) AMapSearchAPI *search; @property (nonatomic, strong) AMapInputTipsSearchRequest *currentRequest; /// 搜索栏 @property (strong, nonatomic) UISearchBar *searchBar; @property (nonatomic ,strong) CommonTableView *comTable; /** 历史搜索数据 */ @property (nonatomic, strong) NSMutableArray *historyData; @end @implementation RailSearchAddressVC - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // [self zx_addCustomTitleView:self.searchBar]; // self.zx_navCustomTitleView.backgroundColor = KKGrey143; [AMapSearchAPI updatePrivacyShow:AMapPrivacyShowStatusDidShow privacyInfo:AMapPrivacyInfoStatusDidContain]; [AMapSearchAPI updatePrivacyAgree:AMapPrivacyAgreeStatusDidAgree]; self.search = [[AMapSearchAPI alloc] init]; self.search.delegate = self; self.zx_navBar.zx_titleView.backgroundColor = KKClearColor; [self.zx_navBar.zx_titleView addSubview:self.searchBar]; [self.view addSubview:self.comTable]; [self.comTable mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view); make.top.equalTo(self.view).offset(iPhoneX_NavHeight); make.bottom.equalTo(self.view.mas_bottom); }]; } - (CommonTableView *)comTable { if (!_comTable) { _comTable = [[CommonTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain cellHeight:60 cellRow:0 isAdaptive:YES isLine:YES commonCell:RailSearchAddressTableViewCell.class createCell:^(ZZTableViewCell * _Nonnull cells, NSIndexPath * _Nonnull indexPath) { RailSearchAddressTableViewCell *cell = (RailSearchAddressTableViewCell *)cells; AMapTip * model = self.modelListArr[indexPath.row]; cell.addressStr = F(@"%@%@%@",model.district,model.address,model.name); } selectedCell:^(UITableView * _Nonnull tableView, NSIndexPath * _Nonnull indexPath) { AMapTip * model = self.modelListArr[indexPath.row]; if(self.selectAddressCallBack){ self.selectAddressCallBack(model.name, model.location); } [self.navigationController popViewControllerAnimated:YES]; } DidscrollView:^(UIScrollView * _Nonnull scrollView) { [self.searchBar resignFirstResponder]; }]; } return _comTable; } - (UISearchBar *)searchBar { if (!_searchBar) { _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-100, 38)]; _searchBar.placeholder = GJText(@"请输入搜索地址"); _searchBar.backgroundColor = KKClearColor; _searchBar.delegate = self; [_searchBar becomeFirstResponder]; } return _searchBar; } - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { NSLog(@"aasdads"); } - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { // [self.modelListArr removeAllObjects]; // // 显示 myTableView 显示历史搜索记录 //// if([UserDefaults valueForKey:SearchHistory]) //// { //// // 把字典转换成 模型类 //// NSArray *tempArr = [UserDefaults valueForKey:SearchHistory]; //// NSMutableArray *arr = [NSMutableArray array]; //// for (int i = 0 ; i < tempArr.count; i++) //// { //// AMapTip *model = [AMapTip yy_modelWithJSON:tempArr[i]]; //// [arr addObject:model]; //// } //// self.historyData = [[NSMutableArray alloc] initWithArray:@[arr]]; //// self.modelListArr = self.historyData; //// } //// self.comTable.cellRow = self.modelListArr.count; //// [self.comTable reloadData]; return YES; } - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { NSString *str = searchBar.text; NSString *resultStr = [str stringByReplacingCharactersInRange:range withString:text]; [self searchRequestString:resultStr]; return YES; } -(void)searchRequestString:(NSString *)str { //搜索的信息 AMapInputTipsSearchRequest *request = [AMapInputTipsSearchRequest new]; request.keywords = str; self.currentRequest = request; //调起检索 [self.search AMapInputTipsSearch:request]; // self.isHistory = NO; } //MARK: ---- 高德 搜索 API 代理方法 ---- - (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error { NSLog(@"Search Error: %@",error.description); } - (void)onInputTipsSearchDone:(AMapInputTipsSearchRequest *)request response:(AMapInputTipsSearchResponse *)response { if (self.currentRequest == nil || self.currentRequest != request ){ return; } if (response.count == 0){ return; } [self.modelListArr removeAllObjects]; //过滤掉location也为空的 查询结果 比如: 天津市 // NSMutableArray *tempArr = [NSMutableArray array]; for (AMapTip* item in response.tips) { if(item.location){ [tempArr addObject:item]; } } //[self.modelListArr addObjectsFromArray:@[tempArr]]; self.modelListArr = tempArr; self.comTable.cellRow = self.modelListArr.count; [self.comTable reloadData]; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end