SearchAddressController.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // EnterAddressController.m
  3. // O2O
  4. //
  5. // Created by fanwe2014 on 15/7/27.
  6. // Copyright (c) 2015年 fanwe. All rights reserved.
  7. //
  8. #import "SearchAddressController.h"
  9. #import "EnterAdrssCell.h"
  10. @interface SearchAddressController ()<UITableViewDataSource,UITableViewDelegate,QMSSearchDelegate>
  11. {
  12. NSMutableArray *_searchArray;
  13. NSMutableArray *_poiArray;
  14. NSMutableArray *_descriptionArray;
  15. NSMutableArray *_provinceArray;
  16. NSMutableArray *_cityArray;
  17. NSMutableArray *_areaArray;
  18. }
  19. @property (nonatomic, strong) QMSSearcher *mapSearcher;
  20. @end
  21. @implementation SearchAddressController
  22. - (void)viewDidLoad
  23. {
  24. [super viewDidLoad];
  25. self.mapSearcher = [[QMSSearcher alloc] initWithDelegate:self];
  26. [self innitUI];
  27. }
  28. - (void)searchCityname:(NSString *)cityname keyword:(NSString *)keyword
  29. {
  30. _searchArray = [[ NSMutableArray alloc] init];
  31. _poiArray = [[ NSMutableArray alloc] init];
  32. _descriptionArray = [[ NSMutableArray alloc] init];
  33. _provinceArray = [[ NSMutableArray alloc] init];
  34. _cityArray = [[ NSMutableArray alloc] init];
  35. _areaArray = [[ NSMutableArray alloc] init];
  36. //配置搜索参数
  37. QMSSuggestionSearchOption *suggetionOption = [[QMSSuggestionSearchOption alloc] init];
  38. [suggetionOption setKeyword:keyword];
  39. [suggetionOption setRegion:cityname];
  40. [self.mapSearcher searchWithSuggestionSearchOption:suggetionOption];
  41. }
  42. //关键字的补完与提示回调接口
  43. - (void)searchWithSuggestionSearchOption:(QMSSuggestionSearchOption *)suggestionSearchOption didReceiveResult:(QMSSuggestionResult *)suggestionSearchResult
  44. {
  45. //在此处理正常结果
  46. for (int i=0; i<suggestionSearchResult.dataArray.count; i++)
  47. {
  48. QMSSuggestionPoiData *poiData = [suggestionSearchResult.dataArray objectAtIndex:i];
  49. CLLocationCoordinate2D coor;
  50. coor = poiData.location;
  51. NSValue *value=[NSValue valueWithBytes:&coor objCType:@encode(CLLocationCoordinate2D)];//对结构体进行封装
  52. if (poiData.location.latitude || poiData.location.longitude)
  53. {
  54. if (poiData.address.length>0)
  55. {
  56. [_searchArray addObject:poiData.address];
  57. }
  58. else
  59. {
  60. [_searchArray addObject:@""];
  61. }
  62. [_poiArray addObject:value];
  63. if (poiData.district)
  64. {
  65. [_descriptionArray addObject:poiData.district];
  66. }else{
  67. [_descriptionArray addObject:@" "];
  68. }
  69. if (poiData.province.length>0)
  70. {
  71. [_provinceArray addObject:poiData.province];
  72. }else
  73. {
  74. [_provinceArray addObject:@""];
  75. }
  76. if (poiData.city.length>0)
  77. {
  78. [_cityArray addObject:poiData.city];
  79. }else
  80. {
  81. [_cityArray addObject:@""];
  82. }
  83. if (poiData.district.length>0)
  84. {
  85. [_areaArray addObject:poiData.district];
  86. }else
  87. {
  88. [_areaArray addObject:@""];
  89. }
  90. }
  91. }
  92. [self.tableView reloadData];
  93. }
  94. - (void)innitUI{
  95. CGRect mainFrme = self.view.frame;
  96. mainFrme.origin.y = 0;
  97. self.tableView.frame = mainFrme;
  98. self.tableView.backgroundColor =kBackGroundColor;
  99. self.tableView.delegate = self;
  100. self.tableView.dataSource = self;
  101. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  102. }
  103. #pragma marks ==============================table协议==============================================
  104. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  105. return 1;
  106. }
  107. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  108. {
  109. return 61.0f;
  110. }
  111. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  112. {
  113. return _searchArray.count;
  114. }
  115. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  116. {
  117. EnterAdrssCell * cell = [EnterAdrssCell cellWithTableView:self.tableView];
  118. if (_searchArray && _descriptionArray && _searchArray.count > indexPath.row && _descriptionArray.count > indexPath.row )
  119. {
  120. NSUInteger row = [indexPath row];
  121. NSString *key = [_searchArray objectAtIndex:row];
  122. NSString *description = [_descriptionArray objectAtIndex:row];
  123. [cell setCellContent:key description:description];
  124. }
  125. return cell;
  126. }
  127. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  128. {
  129. if (_poiArray.count > indexPath.row)
  130. {
  131. NSValue *a = [_poiArray objectAtIndex:indexPath.row];
  132. CLLocationCoordinate2D coor;
  133. [a getValue:&coor];
  134. if ([_delegate respondsToSelector:@selector(SelectedAdress:withPt:andProvinceStr:andCityStr:andAreaStr:)])
  135. {
  136. [_delegate SelectedAdress:[_searchArray objectAtIndex:indexPath.row] withPt:coor andProvinceStr:[_provinceArray objectAtIndex:indexPath.row] andCityStr:[_cityArray objectAtIndex:indexPath.row] andAreaStr:[_areaArray objectAtIndex:indexPath.row]];
  137. }
  138. }
  139. }
  140. @end