PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/XM_Bike/Controllers/SearchControllers/SearchBaseControllers/TipViewController.m

https://gitlab.com/dingchuandong1/XM_Bick
Objective C | 410 lines | 297 code | 94 blank | 19 comment | 52 complexity | 80c1ad88ebe93b6abe8d546d25eacc3f MD5 | raw file
  1. //
  2. // GeoViewController.m
  3. // SearchV3Demo
  4. //
  5. // Created by songjian on 13-8-14.
  6. // Copyright (c) 2013年 songjian. All rights reserved.
  7. //
  8. #import "TipViewController.h"
  9. #import "GeoDetailViewController.h"
  10. #import "CommonUtility.h"
  11. #import "AMapTipAnnotation.h"
  12. #import "BusStopAnnotation.h"
  13. #import "POIAnnotation.h"
  14. #import "BusStopDetailViewController.h"
  15. #import "PoiDetailViewController.h"
  16. #import "TipDetailViewController.h"
  17. #define TipPlaceHolder @"名称"
  18. #define BusLinePaddingEdge 20
  19. @interface TipViewController ()<UISearchBarDelegate, UISearchDisplayDelegate, UITableViewDataSource, UITableViewDelegate>
  20. @property (nonatomic, strong) UISearchBar *searchBar;
  21. @property (nonatomic, strong) UISearchDisplayController *displayController;
  22. @property (nonatomic, strong) NSMutableArray *tips;
  23. @property (nonatomic, strong) NSMutableArray *busLines;
  24. @end
  25. @implementation TipViewController
  26. @synthesize tips = _tips;
  27. @synthesize searchBar = _searchBar;
  28. @synthesize displayController = _displayController;
  29. #pragma mark - Utility
  30. /* 输入提示 搜索.*/
  31. - (void)searchTipsWithKey:(NSString *)key
  32. {
  33. if (key.length == 0)
  34. {
  35. return;
  36. }
  37. AMapInputTipsSearchRequest *tips = [[AMapInputTipsSearchRequest alloc] init];
  38. tips.keywords = key;
  39. tips.city = @"北京";
  40. // tips.cityLimit = YES; 是否限制城市
  41. [self.search AMapInputTipsSearch:tips];
  42. }
  43. /* 清除annotations & overlays */
  44. - (void)clear
  45. {
  46. [self.mapView removeAnnotations:self.mapView.annotations];
  47. [self.mapView removeOverlays:self.mapView.overlays];
  48. }
  49. - (void)clearAndShowAnnotationWithTip:(AMapTip *)tip
  50. {
  51. /* 清除annotations & overlays */
  52. [self clear];
  53. if (tip.uid != nil && tip.location != nil) /* 可以直接在地图打点 */
  54. {
  55. AMapTipAnnotation *annotation = [[AMapTipAnnotation alloc] initWithMapTip:tip];
  56. [self.mapView addAnnotation:annotation];
  57. [self.mapView setCenterCoordinate:annotation.coordinate];
  58. [self.mapView selectAnnotation:annotation animated:YES];
  59. }
  60. else if (tip.uid != nil && tip.location == nil)/* 公交路线,显示出来*/
  61. {
  62. AMapBusLineIDSearchRequest *request = [[AMapBusLineIDSearchRequest alloc] init];
  63. request.city = @"北京";
  64. request.uid = tip.uid;
  65. request.requireExtension = YES;
  66. [self.search AMapBusLineIDSearch:request];
  67. }
  68. else if(tip.uid == nil && tip.location == nil)/* 品牌名,进行POI关键字搜索 */
  69. {
  70. AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];
  71. request.keywords = tip.name;
  72. request.city = @"010";
  73. request.requireExtension = YES;
  74. [self.search AMapPOIKeywordsSearch:request];
  75. }
  76. }
  77. /* 展示公交线路 */
  78. - (void)presentCurrentBusLine
  79. {
  80. AMapBusLine *busLine = [self.busLines firstObject];
  81. if (busLine == nil)
  82. {
  83. return;
  84. }
  85. NSMutableArray *busStopAnnotations = [NSMutableArray array];
  86. [busLine.busStops enumerateObjectsUsingBlock:^(AMapBusStop *busStop, NSUInteger idx, BOOL *stop) {
  87. BusStopAnnotation *annotation = [[BusStopAnnotation alloc] initWithBusStop:busStop];
  88. [busStopAnnotations addObject:annotation];
  89. }];
  90. [self.mapView addAnnotations:busStopAnnotations];
  91. MAPolyline *polyline = [CommonUtility polylineForBusLine:busLine];
  92. [self.mapView addOverlay:polyline];
  93. [self.mapView setVisibleMapRect:polyline.boundingMapRect edgePadding:UIEdgeInsetsMake(BusLinePaddingEdge, BusLinePaddingEdge, BusLinePaddingEdge, BusLinePaddingEdge) animated:YES];
  94. }
  95. - (void)gotoDetailForTip:(AMapTip *)tip
  96. {
  97. if (tip != nil)
  98. {
  99. TipDetailViewController *tipDetailViewController = [[TipDetailViewController alloc] init];
  100. tipDetailViewController.tip = tip;
  101. [self.navigationController pushViewController:tipDetailViewController animated:YES];
  102. }
  103. }
  104. - (void)gotoDetailForBusStop:(AMapBusStop *)busStop
  105. {
  106. if (busStop != nil)
  107. {
  108. BusStopDetailViewController *busStopDetailViewController = [[BusStopDetailViewController alloc] init];
  109. busStopDetailViewController.busStop = busStop;
  110. [self.navigationController pushViewController:busStopDetailViewController animated:YES];
  111. }
  112. }
  113. - (void)gotoDetailForPoi:(AMapPOI *)poi
  114. {
  115. if (poi != nil)
  116. {
  117. PoiDetailViewController *detail = [[PoiDetailViewController alloc] init];
  118. detail.poi = poi;
  119. [self.navigationController pushViewController:detail animated:YES];
  120. }
  121. }
  122. #pragma mark - MAMapViewDelegate
  123. - (void)mapView:(MAMapView *)mapView annotationView:(MAAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
  124. {
  125. if ([view.annotation isKindOfClass:[AMapTipAnnotation class]])
  126. {
  127. [self gotoDetailForTip:[(AMapTipAnnotation *)view.annotation tip]];
  128. }
  129. else if ([view.annotation isKindOfClass:[BusStopAnnotation class]])
  130. {
  131. [self gotoDetailForBusStop:[(BusStopAnnotation*)view.annotation busStop]];
  132. }
  133. else if ([view.annotation isKindOfClass:[POIAnnotation class]])
  134. {
  135. [self gotoDetailForPoi:[(POIAnnotation *)view.annotation poi]];
  136. }
  137. }
  138. - (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
  139. {
  140. if ([annotation isKindOfClass:[AMapTipAnnotation class]])
  141. {
  142. static NSString *tipIdentifier = @"tipIdentifier";
  143. MAPinAnnotationView *poiAnnotationView = (MAPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:tipIdentifier];
  144. if (poiAnnotationView == nil)
  145. {
  146. poiAnnotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:tipIdentifier];
  147. }
  148. poiAnnotationView.canShowCallout = YES;
  149. poiAnnotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
  150. return poiAnnotationView;
  151. }
  152. else if ([annotation isKindOfClass:[BusStopAnnotation class]])
  153. {
  154. static NSString *busStopIdentifier = @"busStopIdentifier";
  155. MAPinAnnotationView *poiAnnotationView = (MAPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:busStopIdentifier];
  156. if (poiAnnotationView == nil)
  157. {
  158. poiAnnotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation
  159. reuseIdentifier:busStopIdentifier];
  160. }
  161. poiAnnotationView.canShowCallout = YES;
  162. poiAnnotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
  163. return poiAnnotationView;
  164. }
  165. else if ([annotation isKindOfClass:[POIAnnotation class]])
  166. {
  167. static NSString *poiIdentifier = @"poiIdentifier";
  168. MAPinAnnotationView *poiAnnotationView = (MAPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:poiIdentifier];
  169. if (poiAnnotationView == nil)
  170. {
  171. poiAnnotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:poiIdentifier];
  172. }
  173. poiAnnotationView.canShowCallout = YES;
  174. poiAnnotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
  175. return poiAnnotationView;
  176. }
  177. return nil;
  178. }
  179. - (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id<MAOverlay>)overlay
  180. {
  181. if ([overlay isKindOfClass:[MAPolyline class]])
  182. {
  183. MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:overlay];
  184. polylineRenderer.lineWidth = 4.f;
  185. polylineRenderer.strokeColor = [UIColor magentaColor];
  186. return polylineRenderer;
  187. }
  188. return nil;
  189. }
  190. #pragma mark - AMapSearchDelegate
  191. /* 输入提示回调. */
  192. - (void)onInputTipsSearchDone:(AMapInputTipsSearchRequest *)request response:(AMapInputTipsSearchResponse *)response
  193. {
  194. [self.tips setArray:response.tips];
  195. [self.displayController.searchResultsTableView reloadData];
  196. }
  197. /* 公交搜索回调. */
  198. - (void)onBusLineSearchDone:(AMapBusLineBaseSearchRequest *)request response:(AMapBusLineSearchResponse *)response
  199. {
  200. if (response.buslines.count != 0)
  201. {
  202. [self.busLines setArray:response.buslines];
  203. [self presentCurrentBusLine];
  204. }
  205. }
  206. /* POI 搜索回调. */
  207. - (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
  208. {
  209. if (response.pois.count == 0)
  210. {
  211. return;
  212. }
  213. NSMutableArray *poiAnnotations = [NSMutableArray arrayWithCapacity:response.pois.count];
  214. [response.pois enumerateObjectsUsingBlock:^(AMapPOI *obj, NSUInteger idx, BOOL *stop) {
  215. [poiAnnotations addObject:[[POIAnnotation alloc] initWithPOI:obj]];
  216. }];
  217. /* 将结果以annotation的形式加载到地图上. */
  218. [self.mapView addAnnotations:poiAnnotations];
  219. /* 如果只有一个结果,设置其为中心点. */
  220. if (poiAnnotations.count == 1)
  221. {
  222. [self.mapView setCenterCoordinate:[poiAnnotations[0] coordinate]];
  223. }
  224. /* 如果有多个结果, 设置地图使所有的annotation都可见. */
  225. else
  226. {
  227. [self.mapView showAnnotations:poiAnnotations animated:NO];
  228. }
  229. }
  230. #pragma mark - UISearchBarDelegate
  231. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  232. {
  233. NSString *key = searchBar.text;
  234. /* 按下键盘enter, 搜索poi */
  235. AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];
  236. request.keywords = key;
  237. request.city = @"010";
  238. request.requireExtension = YES;
  239. [self.search AMapPOIKeywordsSearch:request];
  240. [self.displayController setActive:NO animated:NO];
  241. self.searchBar.placeholder = key;
  242. }
  243. #pragma mark - UISearchDisplayDelegate
  244. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  245. {
  246. [self searchTipsWithKey:searchString];
  247. return YES;
  248. }
  249. #pragma mark - UITableViewDataSource
  250. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  251. {
  252. return self.tips.count;
  253. }
  254. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  255. {
  256. static NSString *tipCellIdentifier = @"tipCellIdentifier";
  257. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tipCellIdentifier];
  258. if (cell == nil)
  259. {
  260. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
  261. reuseIdentifier:tipCellIdentifier];
  262. cell.imageView.image = [UIImage imageNamed:@"locate"];
  263. }
  264. AMapTip *tip = self.tips[indexPath.row];
  265. if (tip.location == nil)
  266. {
  267. cell.imageView.image = [UIImage imageNamed:@"search"];
  268. }
  269. cell.textLabel.text = tip.name;
  270. cell.detailTextLabel.text = tip.address;
  271. return cell;
  272. }
  273. #pragma mark - UITableViewDelegate
  274. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  275. {
  276. AMapTip *tip = self.tips[indexPath.row];
  277. [self clearAndShowAnnotationWithTip:tip];
  278. [self.displayController setActive:NO animated:NO];
  279. self.searchBar.placeholder = tip.name;
  280. }
  281. #pragma mark - Initialization
  282. - (void)initSearchBar
  283. {
  284. self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 44)];
  285. self.searchBar.barStyle = UIBarStyleBlack;
  286. self.searchBar.translucent = YES;
  287. self.searchBar.delegate = self;
  288. self.searchBar.placeholder = TipPlaceHolder;
  289. self.searchBar.keyboardType = UIKeyboardTypeDefault;
  290. [self.view addSubview:self.searchBar];
  291. }
  292. - (void)initSearchDisplay
  293. {
  294. self.displayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
  295. self.displayController.delegate = self;
  296. self.displayController.searchResultsDataSource = self;
  297. self.displayController.searchResultsDelegate = self;
  298. self.displayController.searchContentsController.edgesForExtendedLayout = UIRectEdgeNone;
  299. }
  300. #pragma mark - Life Cycle
  301. - (id)init
  302. {
  303. if (self = [super init])
  304. {
  305. self.tips = [NSMutableArray array];
  306. self.busLines = [NSMutableArray array];
  307. }
  308. return self;
  309. }
  310. - (void)viewDidLoad
  311. {
  312. [super viewDidLoad];
  313. [self initSearchBar];
  314. [self initSearchDisplay];
  315. }
  316. @end