TencentLocationVC.m 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. //
  2. // TencentLocationVC.m
  3. // YBVideo
  4. //
  5. // Created by YunBao on 2018/7/20.
  6. // Copyright © 2018年 cat. All rights reserved.
  7. //
  8. #define SCR_H (self.view.bounds.size.height)
  9. #define SearchBarH 44
  10. #define MapViewH 300
  11. #define StatusBar_HEIGHT 20
  12. #define NavigationBar_HEIGHT 44
  13. #import "TencentLocationVC.h"
  14. #import <QMapKit/QMapKit.h>
  15. #import <QMapSearchKit/QMapSearchKit.h>
  16. #import "MJRefresh.h"
  17. #import "SearchResultView.h"
  18. #import "LocationCell.h"
  19. /// YES:使用api获取列表; NO:使用sdk获取列表
  20. /// 主要针对海外:api可以传递语言参数,国内大部分城市腾讯并未翻译
  21. static const BOOL useWebApi = NO;
  22. @interface TencentLocationVC ()<UITableViewDelegate,UITableViewDataSource,QMapViewDelegate,UISearchBarDelegate,QMSSearchDelegate> {
  23. NSString *_selAddress;
  24. NSString *_selInfo;
  25. }
  26. @property (nonatomic,strong)QMapView *mapView;
  27. @property (nonatomic,strong)UITableView *tableView;
  28. @property (nonatomic,strong)NSMutableArray <QMSPoiData*>* dataList;
  29. @property(nonatomic,strong)NSMutableArray *flagArray; //cell选中标记
  30. @property (nonatomic,strong)QMSSearcher *searcher;
  31. @property (nonatomic,strong)UIImageView *imageViewAnntation;
  32. @property (nonatomic,strong)NSObject *object;
  33. @property (nonatomic,assign)NSInteger pageIndex;
  34. @property (nonatomic,strong)NSNumber *secondPageIndex;
  35. @property (nonatomic,assign)BOOL isNeedLocation;
  36. @property (nonatomic,assign)NSInteger isSearchPage; //1:YES 2:NO
  37. @property (nonatomic,strong)QMSReGeoCodeAdInfo *currentAddressInfo;
  38. @property(nonatomic,strong)QMSReverseGeoCodeSearchOption *regeocoder;
  39. //搜索组合 取消、搜索框
  40. @property(nonatomic,strong)UIView *topMix;
  41. @property(nonatomic,strong)UIButton *cancleBtn;
  42. @property(nonatomic,strong)UISearchBar *searchBar; //搜索框
  43. @property(nonatomic,strong)SearchResultView *resultView;
  44. @end
  45. @implementation TencentLocationVC
  46. - (NSMutableArray<QMSPoiData *> *)dataList {
  47. if (!_dataList) {
  48. _dataList = [NSMutableArray array];
  49. }
  50. return _dataList;
  51. }
  52. - (void)viewWillAppear:(BOOL)animated {
  53. [super viewWillAppear:animated];
  54. self.navigationController.navigationBar.hidden = YES;
  55. self.isNeedLocation = YES;
  56. self.object = [[NSObject alloc] init];
  57. }
  58. - (void)viewWillDisappear:(BOOL)animated {
  59. [super viewWillDisappear:animated];
  60. self.isNeedLocation = NO;
  61. self.object = nil;
  62. [self.dataList removeAllObjects];
  63. [self.flagArray removeAllObjects];
  64. [self.mapView.delegate mapViewDidStopLocatingUser:self.mapView];
  65. }
  66. - (void)viewDidLoad {
  67. [super viewDidLoad];
  68. self.navigationController.navigationBar.hidden = YES;
  69. _selAddress = @"";
  70. _selInfo = @"";
  71. self.flagArray = [NSMutableArray array];
  72. self.pageIndex = 1;
  73. [self.leftBtn setImage:nil forState:0];
  74. [self.leftBtn setTitle:YZMsg(@"取消") forState:0];
  75. self.rightBtn.hidden = NO;
  76. [self.rightBtn setTitle:YZMsg(@"发送") forState:0];
  77. self.titleL.text = YZMsg(@"位置-tit");
  78. [self.leftBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  79. make.centerY.equalTo(self.titleL);
  80. make.left.equalTo(self.naviView.mas_left).offset(15);
  81. }];
  82. [self.view addSubview:self.topMix];
  83. [self setMapView];
  84. [self setMainTableView];
  85. __weak typeof (self) weakSelf = self;
  86. self.resultView.searchResultsPage = ^(NSInteger page) {
  87. weakSelf.secondPageIndex = @(page);
  88. weakSelf.isSearchPage = 1;
  89. if (useWebApi == NO) {
  90. QMSPoiSearchOption *poiSearchOption = [[QMSPoiSearchOption alloc] init];
  91. poiSearchOption.keyword = weakSelf.searchBar.text;
  92. /*
  93. NSString *boundary = [NSString stringWithFormat:@"%@%@%@",@"region(",weakSelf.currentAddressInfo.province,@",1)"];
  94. poiSearchOption.boundary = boundary; //@"region(北京,1)"
  95. */
  96. [poiSearchOption setBoundaryByNearbyWithCenterCoordinate:weakSelf.mapView.userLocation.location.coordinate radius:1000];
  97. poiSearchOption.page_size = 20;
  98. poiSearchOption.page_index = weakSelf.secondPageIndex == nil ? 1 : [weakSelf.secondPageIndex integerValue];
  99. [weakSelf.searcher searchWithPoiSearchOption:poiSearchOption];
  100. }else{
  101. /// 搜索
  102. NSInteger p_page = weakSelf.secondPageIndex == nil ? 1 : [weakSelf.secondPageIndex integerValue];
  103. [[RKLBSManager shareManager] txSearch:weakSelf.searchBar.text coordinate:weakSelf.mapView.userLocation.location.coordinate andPage:p_page complete:^(NSArray *list) {
  104. dispatch_async(dispatch_get_main_queue(), ^{
  105. [weakSelf managerDataWithArray:list];
  106. });
  107. }];
  108. }
  109. };
  110. [self.view addSubview:self.resultView];
  111. }
  112. #pragma mark - 发送位置
  113. - (void)sendCurrentLocation {
  114. [self dismissViewControllerAnimated:YES completion:nil];
  115. /*
  116. //发送位置时截图
  117. [self.mapView takeSnapshotInRect:self.mapView.bounds withCompletionBlock:^(UIImage *resultImage, CGRect rect) {
  118. //resultImage是截取好的图片
  119. //同时发送当前位置数据
  120. //self.mapView.centerCoordinate.latitude,self.mapView.centerCoordinate.longitude
  121. NSLog(@"发送=====%@----纬度:%f-----经度:%f",resultImage,self.mapView.centerCoordinate.latitude,self.mapView.centerCoordinate.longitude);
  122. NSDictionary *mix = @{@"name":_selAddress?_selAddress:YZMsg(@"火星"),
  123. @"info":_selInfo?_selInfo:@"",
  124. };
  125. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:mix options:NSJSONWritingPrettyPrinted error:nil];
  126. NSString *jsonStr = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
  127. NSDictionary *dic = @{@"latitude":@(self.mapView.centerCoordinate.latitude),
  128. @"longitude":@(self.mapView.centerCoordinate.longitude),
  129. @"address":jsonStr,
  130. };
  131. self.locationEvent(dic);
  132. }];
  133. */
  134. NSDictionary *mix = @{@"name":_selAddress?_selAddress:YZMsg(@"火星"),
  135. @"info":_selInfo?_selInfo:@"",
  136. };
  137. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:mix options:NSJSONWritingPrettyPrinted error:nil];
  138. NSString *jsonStr = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
  139. NSDictionary *dic = @{@"latitude":@(self.mapView.centerCoordinate.latitude),
  140. @"longitude":@(self.mapView.centerCoordinate.longitude),
  141. @"address":jsonStr,
  142. };
  143. self.locationEvent(dic);
  144. }
  145. #pragma mark - 复位
  146. - (void)clickResetButton {
  147. CLLocationCoordinate2D center = self.mapView.userLocation.coordinate;
  148. [self.mapView setCenterCoordinate:center animated:YES];
  149. }
  150. - (void)loadPastData {
  151. [self.tableView.mj_footer endRefreshing];
  152. QCoordinateRegion region;
  153. CLLocationCoordinate2D centerCoordinate = self.mapView.region.center;
  154. region.center= centerCoordinate;
  155. self.isSearchPage = 2;
  156. self.pageIndex ++;
  157. if (useWebApi == NO) {
  158. QMSPoiSearchOption *poiSearchOption = [[QMSPoiSearchOption alloc] init];
  159. poiSearchOption.page_size = 20;
  160. poiSearchOption.page_index = self.pageIndex;
  161. [poiSearchOption setBoundaryByNearbyWithCenterCoordinate:centerCoordinate radius:1000];
  162. [self.searcher searchWithPoiSearchOption:poiSearchOption];
  163. }else{
  164. // 周边
  165. YBWeakSelf;
  166. [[RKLBSManager shareManager] txMapPoisWithCoordinate:centerCoordinate andPage:self.pageIndex complete:^(NSArray *list) {
  167. dispatch_async(dispatch_get_main_queue(), ^{
  168. [weakSelf managerDataWithArray:list];
  169. });
  170. }];
  171. }
  172. }
  173. #pragma mark - 选中搜索位置回调
  174. - (void)refreshData:(QMSPoiData *)result {
  175. CLLocationCoordinate2D center = result.location;
  176. [self.mapView setCenterCoordinate:center animated:YES];
  177. _searchBar.text = nil;
  178. self.isSearchPage = 2;
  179. if (useWebApi == NO) {
  180. QMSPoiSearchOption *poiSearchOption = [[QMSPoiSearchOption alloc] init];
  181. poiSearchOption.page_size = 20;
  182. [poiSearchOption setBoundaryByNearbyWithCenterCoordinate:result.location radius:1000];
  183. [self.searcher searchWithPoiSearchOption:poiSearchOption];
  184. }else{
  185. // 周边
  186. YBWeakSelf;
  187. [[RKLBSManager shareManager] txMapPoisWithCoordinate:result.location andPage:1 complete:^(NSArray *list) {
  188. dispatch_async(dispatch_get_main_queue(), ^{
  189. [weakSelf managerDataWithArray:list];
  190. });
  191. }];
  192. }
  193. }
  194. #pragma mark - 触摸事件
  195. -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  196. if (_resultView.hidden==NO) {
  197. UITouch *touch = [[event allTouches] anyObject];
  198. CGPoint _touchPoint = [touch locationInView:self.view];
  199. if (YES == CGRectContainsPoint(_resultView.frame, _touchPoint)){
  200. [self clickSearchCancle];
  201. }
  202. }
  203. }
  204. #pragma mark - 搜索取消
  205. -(void)clickSearchCancle {
  206. [_searchBar resignFirstResponder];
  207. [UIView animateWithDuration:0.25 animations:^{
  208. _resultView.hidden = YES;
  209. _topMix.frame = CGRectMake(0, 64+statusbarHeight, _window_width, 44+10);
  210. _searchBar.frame = CGRectMake(0,0, _window_width,44);
  211. _cancleBtn.frame = CGRectMake(_searchBar.right, _searchBar.top, 80, 44);
  212. self.mapView.frame = CGRectMake(0,CGRectGetMaxY(_topMix.frame), self.view.bounds.size.width, MapViewH);
  213. self.tableView.frame = CGRectMake(0, CGRectGetMaxY(self.mapView.frame), self.view.bounds.size.width, self.view.bounds.size.height - MapViewH - 64-10-ShowDiff-statusbarHeight);
  214. self.imageViewAnntation.center = self.mapView.center;
  215. }];
  216. }
  217. #pragma mark - 搜索框
  218. -(void)clickClearBtn {
  219. _resultView.alpha = 0.3;
  220. _resultView.tableView.hidden = YES;
  221. }
  222. -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
  223. //一个一个字清空搜索框的时候
  224. if (searchText.length==0) {
  225. _resultView.alpha = 0.3;
  226. _resultView.tableView.hidden = YES;
  227. }else{
  228. _resultView.alpha = 1;
  229. _resultView.tableView.hidden = NO;
  230. self.isSearchPage = 1;
  231. self.resultView.pageIndex = 1;
  232. if (useWebApi == NO) {
  233. QMSPoiSearchOption *poiSearchOption = [[QMSPoiSearchOption alloc] init];
  234. poiSearchOption.keyword = _searchBar.text;
  235. /*
  236. NSString *boundary = [NSString stringWithFormat:@"%@%@%@",@"region(",self.currentAddressInfo.city,@",1)"];
  237. poiSearchOption.boundary = boundary; //@"region(北京,1)"
  238. */
  239. [poiSearchOption setBoundaryByNearbyWithCenterCoordinate:_mapView.userLocation.location.coordinate radius:1000];
  240. poiSearchOption.page_size = 20;
  241. poiSearchOption.page_index = 1;
  242. [self.searcher searchWithPoiSearchOption:poiSearchOption];
  243. }else{
  244. /// 搜索
  245. YBWeakSelf;
  246. [[RKLBSManager shareManager] txSearch:_searchBar.text coordinate:_mapView.userLocation.location.coordinate andPage:1 complete:^(NSArray *list) {
  247. dispatch_async(dispatch_get_main_queue(), ^{
  248. [weakSelf managerDataWithArray:list];
  249. });
  250. }];
  251. }
  252. }
  253. }
  254. -(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
  255. if (searchBar.text.length == 0 ) {
  256. [_resultView.dataSource removeAllObjects];
  257. [_resultView.tableView reloadData];
  258. _resultView.alpha = 0.3;
  259. _resultView.tableView.hidden = YES;
  260. }
  261. [UIView animateWithDuration:0.25 animations:^{
  262. _resultView.hidden = NO;
  263. _topMix.frame = CGRectMake(0, 0, _window_width, 64+statusbarHeight);
  264. _searchBar.frame = CGRectMake(0,20+statusbarHeight, _window_width-80,44);
  265. _cancleBtn.frame = CGRectMake(_searchBar.right, _searchBar.top, 80, 44);
  266. self.mapView.frame = CGRectMake(0,CGRectGetMaxY(self.topMix.frame), self.view.bounds.size.width, MapViewH);
  267. self.tableView.frame = CGRectMake(0, CGRectGetMaxY(self.mapView.frame), self.view.bounds.size.width, self.view.bounds.size.height - MapViewH - 64-10);
  268. self.imageViewAnntation.center = self.mapView.center;
  269. }];
  270. }
  271. -(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
  272. [searchBar resignFirstResponder];
  273. }
  274. -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
  275. _resultView.alpha = 1;
  276. _resultView.tableView.hidden = NO;
  277. self.isSearchPage = 1;
  278. self.resultView.pageIndex = 1;
  279. if (useWebApi == NO) {
  280. QMSPoiSearchOption *poiSearchOption = [[QMSPoiSearchOption alloc] init];
  281. poiSearchOption.keyword = _searchBar.text;
  282. /*
  283. NSString *boundary = [NSString stringWithFormat:@"%@%@%@",@"region(",self.currentAddressInfo.city,@",1)"];
  284. poiSearchOption.boundary = boundary; //@"region(北京,1)"
  285. */
  286. [poiSearchOption setBoundaryByNearbyWithCenterCoordinate:_mapView.userLocation.location.coordinate radius:1000];
  287. poiSearchOption.page_size = 20;
  288. poiSearchOption.page_index = 1;
  289. [self.searcher searchWithPoiSearchOption:poiSearchOption];
  290. }else{
  291. /// 搜索
  292. YBWeakSelf;
  293. [[RKLBSManager shareManager] txSearch:_searchBar.text coordinate:_mapView.userLocation.location.coordinate andPage:1 complete:^(NSArray *list) {
  294. dispatch_async(dispatch_get_main_queue(), ^{
  295. [weakSelf managerDataWithArray:list];
  296. });
  297. }];
  298. }
  299. }
  300. - (void)dealloc {
  301. [[NSNotificationCenter defaultCenter] removeObserver:self];
  302. }
  303. #pragma mark - UITabelViewDataSource / UITableViewDelegate
  304. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  305. return self.dataList.count;
  306. }
  307. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  308. return 60;
  309. }
  310. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  311. LocationCell *cell = [LocationCell cellWithTab:tableView andIndexPath:indexPath];
  312. cell.addressL.text = self.dataList[indexPath.row].title;
  313. cell.infoL.text = self.dataList[indexPath.row].address;
  314. cell.backgroundColor = CellRow_Cor;
  315. NSString *flag = [NSString stringWithFormat:@"%@",_flagArray[indexPath.row]];
  316. if ([flag isEqual:@"1"]) {
  317. cell.falgIV.hidden = NO;
  318. }else {
  319. cell.falgIV.hidden = YES;
  320. }
  321. cell.selectedBackgroundView = [[UIImageView alloc]initWithImage:[PublicObj getImgWithColor:SelCell_Col]];
  322. return cell;
  323. }
  324. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  325. [_flagArray removeAllObjects];
  326. for (int i=0; i<_dataList.count; i++) {
  327. [_flagArray addObject:@"0"];
  328. }
  329. [_flagArray replaceObjectAtIndex:indexPath.row withObject:@"1"];
  330. [_tableView reloadData];
  331. _selAddress = [NSString stringWithFormat:@"%@",self.dataList[indexPath.row].title];
  332. _selInfo = [NSString stringWithFormat:@"%@",self.dataList[indexPath.row].address];
  333. CLLocationCoordinate2D center = [self.dataList objectAtIndex:indexPath.row].location;
  334. [self.mapView setCenterCoordinate:center animated:YES];
  335. self.object = [[NSObject alloc] init];
  336. }
  337. #pragma mark - QMapViewDelegate
  338. #pragma mark - 开始定位
  339. - (void)mapViewWillStartLocatingUser:(QMapView *)mapView {
  340. NSLog(@"开始定位%f--%f--%f--%f",mapView.centerCoordinate.latitude,mapView.centerCoordinate.longitude,mapView.region.center.latitude,mapView.region.center.longitude);
  341. }
  342. #pragma mark - 结束定位
  343. - (void)mapViewDidStopLocatingUser:(QMapView *)mapView{
  344. NSLog(@"结束定位");
  345. }
  346. #pragma mark - 刷新定位,只要位置发生变化就会调用
  347. - (void)mapView:(QMapView *)mapView didUpdateUserLocation:(QUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {
  348. if (self.isNeedLocation) {
  349. if (updatingLocation) {
  350. self.regeocoder.location = [NSString stringWithFormat:@"%f,%f",mapView.region.center.latitude,mapView.region.center.longitude];
  351. [self.regeocoder setCoord_type:QMSReverseGeoCodeCoordinateTencentGoogleGaodeType];
  352. [self.searcher searchWithReverseGeoCodeSearchOption:self.regeocoder];
  353. self.isSearchPage = 2;
  354. if (useWebApi == NO) {
  355. QMSPoiSearchOption *poiSearchOption = [[QMSPoiSearchOption alloc] init];
  356. poiSearchOption.page_size = 20;
  357. [poiSearchOption setBoundaryByNearbyWithCenterCoordinate:userLocation.location.coordinate radius:1000];
  358. [self.searcher searchWithPoiSearchOption:poiSearchOption];
  359. }else{
  360. /// 周边
  361. YBWeakSelf;
  362. [[RKLBSManager shareManager] txMapPoisWithCoordinate:userLocation.location.coordinate andPage:1 complete:^(NSArray *list) {
  363. dispatch_async(dispatch_get_main_queue(), ^{
  364. [weakSelf managerDataWithArray:list];
  365. });
  366. }];
  367. }
  368. }
  369. self.isNeedLocation = NO;
  370. }
  371. }
  372. #pragma mark - 定位失败
  373. - (void)mapView:(QMapView *)mapView didFailToLocateUserWithError:(NSError *)error {
  374. NSLog(@"didFailToLocateUserWithError--error--%@",error);
  375. }
  376. -(void)mapViewDidFailLoadingMap:(QMapView *)mapView withError:(NSError *)error {
  377. NSLog(@"mapViewLoadingFail:%@",error);
  378. }
  379. -(void)mapView:(QMapView *)mapView regionWillChangeAnimated:(BOOL)animated {
  380. NSLog(@"regionWillChange");
  381. }
  382. #pragma mark -查询出现错误
  383. - (void)searchWithSearchOption:(QMSSearchOption *)searchOption didFailWithError:(NSError*)error {
  384. NSLog(@"searchWithSearchOption--error--%@",error);
  385. }
  386. #pragma mark - mapView移动后执行
  387. - (void)mapView:(QMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
  388. //手动滑动地图定位
  389. if (self.object == nil) {
  390. self.pageIndex = 1;
  391. QCoordinateRegion region;
  392. CLLocationCoordinate2D centerCoordinate = mapView.region.center;
  393. region.center= centerCoordinate;
  394. self.isSearchPage = 2;
  395. [self.tableView setContentOffset:CGPointMake(0, 0) animated:NO];
  396. if (useWebApi == NO) {
  397. QMSPoiSearchOption *poiSearchOption = [[QMSPoiSearchOption alloc] init];
  398. poiSearchOption.page_size = 20;
  399. [poiSearchOption setBoundaryByNearbyWithCenterCoordinate:centerCoordinate radius:1000];
  400. [self.searcher searchWithPoiSearchOption:poiSearchOption];
  401. }else{
  402. // 周边
  403. YBWeakSelf;
  404. [[RKLBSManager shareManager] txMapPoisWithCoordinate:centerCoordinate andPage:1 complete:^(NSArray *list) {
  405. dispatch_async(dispatch_get_main_queue(), ^{
  406. [weakSelf managerDataWithArray:list];
  407. });
  408. }];
  409. }
  410. } else {
  411. self.object = nil;
  412. }
  413. }
  414. #pragma mark - poi查询结果回调函数
  415. - (void)searchWithPoiSearchOption:(QMSPoiSearchOption *)poiSearchOption didReceiveResult:(QMSPoiSearchResult *)poiSearchResult {
  416. for (QMSPoiData *data in poiSearchResult.dataArray) {
  417. NSLog(@"%@-- %@-- %@",data.title,data.address,data.tel);
  418. }
  419. NSMutableArray<QMSPoiData *> *resArray = poiSearchResult.dataArray;
  420. dispatch_async(dispatch_get_main_queue(), ^{
  421. [self getRestToRefresh:resArray];
  422. });
  423. }
  424. -(void)getRestToRefresh:(NSMutableArray<QMSPoiData *> *)resArray {
  425. //根据本页地图返回的结果
  426. if (self.isSearchPage == 2) {
  427. //手滑动重新赋值数据源
  428. if (self.pageIndex == 1) {
  429. [self.dataList removeAllObjects];
  430. self.dataList = [NSMutableArray arrayWithArray:resArray];
  431. [_flagArray removeAllObjects];
  432. for (int i=0; i<self.dataList.count; i++) {
  433. [_flagArray addObject:@"0"];
  434. }
  435. if (self.dataList.count>0) {
  436. NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
  437. [self tableView:_tableView didSelectRowAtIndexPath:index];
  438. }
  439. } else {
  440. [self.dataList addObjectsFromArray:resArray];
  441. for (int i=0; i<resArray.count; i++) {
  442. [_flagArray addObject:@"0"];
  443. }
  444. }
  445. [self.tableView reloadData];
  446. }
  447. //搜索控制器根据关键词返回的结果
  448. if (self.isSearchPage == 1) {
  449. [[NSNotificationCenter defaultCenter] postNotificationName:SearchResultGetPoiSearchResult object:nil userInfo:@{@"data":resArray}];
  450. }
  451. }
  452. #pragma mark - 根据定位当前的经纬度编码出当前位置信息
  453. - (void)searchWithReverseGeoCodeSearchOption:(QMSReverseGeoCodeSearchOption *)reverseGeoCodeSearchOption didReceiveResult:(QMSReverseGeoCodeSearchResult *)reverseGeoCodeSearchResult {
  454. self.currentAddressInfo = reverseGeoCodeSearchResult.ad_info;
  455. }
  456. - (void)didReceiveMemoryWarning {
  457. [super didReceiveMemoryWarning];
  458. }
  459. #pragma mark - set/get
  460. -(SearchResultView *)resultView {
  461. if (!_resultView) {
  462. _resultView = [[SearchResultView alloc]initWithFrame:CGRectMake(0,64+statusbarHeight, _window_width, _window_height-64-statusbarHeight)];
  463. _resultView.backgroundColor = [UIColor blackColor];
  464. _resultView.alpha = 0.3;
  465. _resultView.tableView.hidden = YES;
  466. _resultView.hidden = YES;
  467. YBWeakSelf;
  468. _resultView.dismissEvent = ^(QMSPoiData *reult) {
  469. [weakSelf refreshData:reult];
  470. [weakSelf clickSearchCancle];
  471. };
  472. }
  473. return _resultView;
  474. }
  475. -(UIView *)topMix {
  476. if (!_topMix) {
  477. _topMix = [[UIView alloc]initWithFrame:CGRectMake(0, 64+statusbarHeight, _window_width, 44+10)];
  478. _topMix.backgroundColor = CellRow_Cor;
  479. _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0, _window_width,44)];
  480. _searchBar.backgroundImage = [PublicObj getImgWithColor:CellRow_Cor];
  481. _searchBar.placeholder = YZMsg(@"搜索地点");
  482. _searchBar.delegate = self;
  483. UITextField *textField ;
  484. if (@available(iOS 13.0,*)) {
  485. textField = _searchBar.searchTextField;
  486. }else {
  487. textField = [_searchBar valueForKey:@"_searchField"];
  488. }
  489. [textField setBackgroundColor:RGB_COLOR(@"#211C37", 1)];
  490. // [textField setValue:GrayText forKeyPath:@"_placeholderLabel.textColor"];
  491. // [textField setValue:[UIFont systemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
  492. UIButton *clearBtn = [textField valueForKey:@"_clearButton"];
  493. [clearBtn addTarget:self action:@selector(clickClearBtn) forControlEvents:UIControlEventTouchUpInside];
  494. textField.textColor = GrayText;
  495. textField.layer.cornerRadius = 18;
  496. textField.layer.masksToBounds = YES;
  497. [_topMix addSubview:_searchBar];
  498. _cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  499. _cancleBtn.frame = CGRectMake(_searchBar.right, _searchBar.top, 80, 44);
  500. _cancleBtn.titleLabel.font = SYS_Font(15);;
  501. [_cancleBtn setTitle:YZMsg(@"取消") forState:0];
  502. [_cancleBtn setTitleColor:Pink_Cor forState:0];
  503. [_cancleBtn addTarget:self action:@selector(clickSearchCancle) forControlEvents:UIControlEventTouchUpInside];
  504. [_topMix addSubview:_cancleBtn];
  505. }
  506. return _topMix;
  507. }
  508. - (void)setMapView {
  509. self.regeocoder = [[QMSReverseGeoCodeSearchOption alloc] init];
  510. //mapView
  511. self.mapView = [[QMapView alloc] initWithFrame:CGRectMake(0, self.topMix.bottom, self.view.bounds.size.width, MapViewH)];
  512. self.mapView.delegate = self;
  513. [self.view addSubview:self.mapView];
  514. [self.mapView setShowsUserLocation:YES];
  515. self.mapView.showsScale = YES;
  516. [self.mapView setUserTrackingMode:QUserTrackingModeFollow animated:YES];
  517. _mapView.distanceFilter = kCLLocationAccuracyBest;//kCLLocationAccuracyNearestTenMeters;
  518. //传入当前定位中心点避免QMapView定位不到位置
  519. CLLocationCoordinate2D center;
  520. center.latitude = [[cityDefault getLocationLat] doubleValue];
  521. center.longitude = [[cityDefault getLocationLng] doubleValue];
  522. [self.mapView setCenterCoordinate:center zoomLevel:16.01 animated:YES];
  523. self.mapView.userLocation.coordinate = center;
  524. self.regeocoder.location = [NSString stringWithFormat:@"%f,%f",_mapView.region.center.latitude,_mapView.region.center.longitude];
  525. [self.regeocoder setCoord_type:QMSReverseGeoCodeCoordinateTencentGoogleGaodeType];
  526. //QMSSearcher
  527. self.searcher = [[QMSSearcher alloc] init];
  528. [self.searcher setDelegate:self];
  529. [self.searcher searchWithReverseGeoCodeSearchOption:self.regeocoder];
  530. self.isSearchPage = 2;
  531. if (useWebApi == NO) {
  532. QMSPoiSearchOption *poiSearchOption = [[QMSPoiSearchOption alloc] init];
  533. poiSearchOption.page_size = 20;
  534. [poiSearchOption setBoundaryByNearbyWithCenterCoordinate:center radius:1000];
  535. [self.searcher searchWithPoiSearchOption:poiSearchOption];
  536. }else{
  537. /// 周边
  538. YBWeakSelf;
  539. [[RKLBSManager shareManager] txMapPoisWithCoordinate:center andPage:1 complete:^(NSArray *list) {
  540. dispatch_async(dispatch_get_main_queue(), ^{
  541. [weakSelf managerDataWithArray:list];
  542. });
  543. }];
  544. }
  545. UIButton *buttonReset = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 60, self.mapView.frame.size.height - 65, 50, 50)];
  546. //[buttonReset setTitle:@"复位" forState:UIControlStateNormal];
  547. [buttonReset setImage:[UIImage imageNamed:@"location_back"] forState:0];
  548. buttonReset.titleLabel.font = [UIFont systemFontOfSize:14];
  549. [buttonReset addTarget:self action:@selector(clickResetButton) forControlEvents:UIControlEventTouchUpInside];
  550. [self.mapView addSubview:buttonReset];
  551. }
  552. -(void)managerDataWithArray:(NSArray *)list {
  553. if (list.count>0) {
  554. NSMutableArray <QMSPoiData*>*m_array = [NSMutableArray array];
  555. for (NSDictionary *l_dic in list) {
  556. QMSPoiData *newData = [[QMSPoiData alloc]init];
  557. newData.title = minstr([l_dic valueForKey:@"title"]);
  558. newData.address = minstr([l_dic valueForKey:@"address"]);
  559. NSDictionary *locDic = [l_dic valueForKey:@"location"];
  560. CLLocationCoordinate2D newCoor;
  561. newCoor.latitude = [[locDic valueForKey:@"lat"] doubleValue];
  562. newCoor.longitude = [[locDic valueForKey:@"lng"] doubleValue];
  563. newData.location = newCoor;
  564. [m_array addObject:newData];
  565. }
  566. [self getRestToRefresh:m_array];
  567. }else{
  568. [self.dataList removeAllObjects];
  569. [_flagArray removeAllObjects];
  570. [self.tableView reloadData];
  571. }
  572. }
  573. - (void)setMainTableView {
  574. //self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.mapView.frame), self.view.bounds.size.width, self.view.bounds.size.height - MapViewH - SearchBarH - NavigationBar_HEIGHT - StatusBar_HEIGHT) style:UITableViewStylePlain];
  575. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.mapView.frame), self.view.bounds.size.width, self.view.bounds.size.height - MapViewH - _topMix.height-statusbarHeight-64-ShowDiff) style:UITableViewStylePlain];
  576. self.tableView.dataSource = self;
  577. self.tableView.delegate = self;
  578. self.tableView.backgroundColor = Normal_Color;
  579. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  580. [self.view addSubview:self.tableView];
  581. MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadPastData)];
  582. self.tableView.mj_footer = footer;
  583. [footer setTitle:@"" forState:MJRefreshStateIdle];
  584. [footer setTitle:@"" forState:MJRefreshStatePulling];
  585. [footer setTitle:YZMsg(@"正在刷新数据") forState:MJRefreshStateRefreshing];
  586. footer.stateLabel.font = [UIFont systemFontOfSize:14];
  587. footer.stateLabel.textColor = [UIColor blackColor];
  588. [self.tableView.mj_header beginRefreshing];
  589. self.imageViewAnntation = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
  590. self.imageViewAnntation.center = self.mapView.center;
  591. // NSString *b_path = [[NSBundle mainBundle]pathForResource:@"QMapKit.bundle" ofType:nil];
  592. // NSString *img_path = [b_path stringByAppendingPathComponent:@"images/greenPin_lift.png"];
  593. // self.imageViewAnntation.image = [UIImage imageWithContentsOfFile:img_path];
  594. self.imageViewAnntation.image = [UIImage imageNamed:@"location_current"];
  595. self.imageViewAnntation.backgroundColor = [UIColor clearColor];
  596. [self.view addSubview:self.imageViewAnntation];
  597. }
  598. #pragma mark - 导航
  599. - (void)clickNaviRightBtn {
  600. [self sendCurrentLocation];
  601. [self.navigationController popViewControllerAnimated:NO];
  602. NSLog(@"发送");
  603. }
  604. @end