MapViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. //
  2. // MapViewController.m
  3. // BuguLive
  4. //
  5. // Created by GuoMs on 16/8/11.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "MapViewController.h"
  9. #import <QMapKit/QMapKit.h>
  10. #import <QMapSearchKit/QMapSearchKit.h>
  11. #import "SearchAddressController.h"
  12. #import "MBProgressHUD.h"
  13. //#define kDefaultMargin 10 //边距
  14. #define kSubmitBtnHeight 45 //提交等按钮的高度
  15. #define IOS7 [[[UIDevice currentDevice]systemVersion] floatValue] >= 7.0
  16. @interface MapViewController ()<QMapViewDelegate,QMSSearchDelegate,SelectedAdressDelegate,MBProgressHUDDelegate,UITextFieldDelegate>
  17. {
  18. UIImageView *_image;
  19. SearchAddressController *_searchAddressController;//地图检索
  20. UITextField *_searchTF;
  21. UILabel *_addressLabel;
  22. UIButton *_rightTopBtn;
  23. UIButton *_submitBtn;
  24. NSString *_addressStr;
  25. MBProgressHUD *_HUD;
  26. }
  27. @property (nonatomic, strong) QMapView *myMapView;
  28. @property (nonatomic, strong) QMSSearcher *mapSearcher;
  29. @end
  30. @implementation MapViewController
  31. - (void)viewDidLoad
  32. {
  33. [super viewDidLoad];
  34. self.navigationItem.title = ASLocalizedString(@"选择位置");
  35. self.view.backgroundColor = kBackGroundColor;
  36. self.navigationItem.leftBarButtonItem=[UIBarButtonItem itemWithTarget:self action:@selector(returnToMeVc) image:@"com_arrow_vc_back" highImage:@"com_arrow_vc_back"];
  37. if (IOS7)
  38. {
  39. self.automaticallyAdjustsScrollViewInsets = NO;
  40. }
  41. //1.获取主队列
  42. dispatch_queue_t queue=dispatch_get_main_queue();
  43. //2.把任务添加到主队列中执行
  44. dispatch_async(queue, ^{
  45. if (!_myMapView) {
  46. _myMapView = [[QMapView alloc]initWithFrame:CGRectMake(0, 0, kScreenW,kScreenH - 64)];
  47. _myMapView.delegate = self;
  48. [_myMapView setShowsUserLocation:YES];
  49. [self.view addSubview:_myMapView];
  50. }
  51. if (!_mapSearcher) {
  52. self.mapSearcher = [[QMSSearcher alloc] init];
  53. self.mapSearcher.delegate = self;
  54. }
  55. if (_location.longitude && _location.latitude) {
  56. CLLocationCoordinate2D pt = (CLLocationCoordinate2D){_location.latitude, _location.longitude};
  57. _myMapView.centerCoordinate = pt;
  58. }else if(_addressName && ![_addressName isEqualToString:@""]){
  59. [self performSelector:@selector(searchForPoint) withObject:nil afterDelay:1];
  60. }else if (self.BuguLive.latitude && self.BuguLive.longitude){
  61. CLLocationCoordinate2D pt = (CLLocationCoordinate2D){self.BuguLive.latitude, self.BuguLive.longitude};
  62. _myMapView.centerCoordinate = pt;
  63. }
  64. _image = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenW-30)/2, (kScreenH-64-30)/2-17, 30, 30)];
  65. [_image setImage:[UIImage imageNamed:@"com_location_1"]];
  66. [self.view addSubview:_image];
  67. [self initMyView];
  68. });
  69. dispatch_async(queue, ^{
  70. self.myMapView.zoomLevel = 15;
  71. });
  72. }
  73. #pragma mark - 返回上一级
  74. - (void)returnToMeVc{
  75. [self.navigationController popViewControllerAnimated:YES];
  76. }
  77. - (void)initMyView{
  78. _rightTopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  79. _rightTopBtn.frame = CGRectMake(0, 0, 70, 30);
  80. _rightTopBtn.titleLabel.font = [UIFont systemFontOfSize:16];
  81. [_rightTopBtn setTitleColor:kAppMainColor forState:UIControlStateNormal];
  82. [_rightTopBtn setTitle:ASLocalizedString(@"我的位置")forState:UIControlStateNormal];
  83. [_rightTopBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
  84. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightTopBtn];
  85. UIView *searchBottomView = [[UIView alloc]initWithFrame:CGRectMake(kDefaultMargin, kDefaultMargin, kScreenW-kDefaultMargin*2, kSubmitBtnHeight)];
  86. searchBottomView.backgroundColor = [UIColor whiteColor];
  87. searchBottomView.layer.cornerRadius = kCornerRadius;
  88. searchBottomView.clipsToBounds = YES;
  89. [self.view addSubview:searchBottomView];
  90. UIImageView *searchImgView = [[UIImageView alloc]initWithFrame:CGRectMake(kDefaultMargin/2, (kSubmitBtnHeight-20)/2, 20, 20)];
  91. [searchImgView setImage:[UIImage imageNamed:@"hm_search"]];
  92. [searchBottomView addSubview:searchImgView];
  93. _searchTF = [[UITextField alloc]initWithFrame:CGRectMake(kDefaultMargin+20, 0, kScreenW-kDefaultMargin*3-20, kSubmitBtnHeight)];
  94. [_searchTF addTarget:self action:@selector(textChangeAction) forControlEvents:UIControlEventEditingChanged];
  95. _searchTF.delegate = self;
  96. _searchTF.clearButtonMode = UITextFieldViewModeAlways;
  97. _searchTF.placeholder = ASLocalizedString(@"请输入要查询的地址名称");
  98. _searchTF.font = [UIFont systemFontOfSize:14.0];
  99. [searchBottomView addSubview:_searchTF];
  100. UIView *addressBottomView = [[UIView alloc]initWithFrame:CGRectMake(kDefaultMargin, kDefaultMargin+CGRectGetMaxY(searchBottomView.frame), kScreenW-kDefaultMargin*2, kSubmitBtnHeight)];
  101. addressBottomView.backgroundColor = [UIColor whiteColor];
  102. addressBottomView.layer.cornerRadius = kCornerRadius;
  103. addressBottomView.clipsToBounds = YES;
  104. [self.view addSubview:addressBottomView];
  105. UIImageView *addressImgView = [[UIImageView alloc]initWithFrame:CGRectMake(kDefaultMargin/2+2, (kSubmitBtnHeight-20)/2, 25/2,33 / 2)];
  106. [addressImgView setImage:[UIImage imageNamed:@"com_location_1"]];
  107. [addressBottomView addSubview:addressImgView];
  108. _addressLabel = [[UILabel alloc]initWithFrame:CGRectMake(kDefaultMargin+20, 0, kScreenW-kDefaultMargin*3-20, kSubmitBtnHeight)];
  109. _addressLabel.numberOfLines = 2;
  110. _addressLabel.textColor = myTextColor2;
  111. _addressLabel.backgroundColor = [UIColor whiteColor];
  112. _addressLabel.font = [UIFont systemFontOfSize:14.0];
  113. [addressBottomView addSubview:_addressLabel];
  114. UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenH-64-kSubmitBtnHeight-15, kScreenW, kSubmitBtnHeight+15)];
  115. bottomView.backgroundColor = [UIColor whiteColor];
  116. [self.view addSubview:bottomView];
  117. //确定 按钮
  118. _submitBtn = [[UIButton alloc]initWithFrame:CGRectMake(kDefaultMargin, (CGRectGetHeight(bottomView.frame)-kSubmitBtnHeight)/2, kScreenW-(2*kDefaultMargin), kSubmitBtnHeight)];
  119. [_submitBtn setTitle:ASLocalizedString(@"确定")forState:UIControlStateNormal];
  120. [_submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  121. [_submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
  122. _submitBtn.backgroundColor = kAppMainColor;
  123. _submitBtn.layer.cornerRadius = kCornerRadius;
  124. _submitBtn.clipsToBounds = YES;
  125. _submitBtn.titleLabel.font = [UIFont systemFontOfSize: 16.0];
  126. _submitBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  127. [bottomView addSubview:_submitBtn];
  128. [_submitBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
  129. _searchAddressController = [[SearchAddressController alloc] init];
  130. _searchAddressController.delegate = self;
  131. _searchAddressController.view.hidden = YES;
  132. _searchAddressController.view.frame = CGRectMake(0, CGRectGetMaxY(addressBottomView.frame),kScreenW, kScreenH-64-CGRectGetMaxY(addressBottomView.frame)-CGRectGetHeight(bottomView.frame));
  133. [_searchAddressController innitUI];
  134. [self.view addSubview:_searchAddressController.view];
  135. }
  136. - (void)btnAction:(id)sender{
  137. if (sender == _rightTopBtn) {
  138. if (self.BuguLive.latitude && self.BuguLive.longitude){
  139. CLLocationCoordinate2D pt = (CLLocationCoordinate2D){self.BuguLive.latitude, self.BuguLive.longitude};
  140. _myMapView.centerCoordinate = pt;
  141. }
  142. }else if (sender == _submitBtn){
  143. if (_delegate)
  144. {
  145. [_delegate chooseAddress:_location address:_addressStr andProvinceString:@"" andCityString:@"" andAreaString:@""];
  146. }
  147. if (_fromType == 0) {
  148. self.BuguLive.latitude = _myMapView.centerCoordinate.latitude;
  149. self.BuguLive.longitude = _myMapView.centerCoordinate.longitude;
  150. }
  151. [self.navigationController popViewControllerAnimated:YES];
  152. }
  153. }
  154. /*!
  155. * @brief 位置或者设备方向更新后,会调用此函数
  156. *
  157. * @param mapView 地图view
  158. * @param userLocation 用户定位信息(包括位置与设备方向等数据)
  159. * @param updatingLocation 标示是否是location数据更新, YES:location数据更新 NO:heading数据更新
  160. */
  161. - (void)mapView:(QMapView *)mapView didUpdateUserLocation:(QUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
  162. CLLocationCoordinate2D coordinate = userLocation.coordinate;
  163. if (coordinate.latitude && coordinate.longitude) {
  164. self.BuguLive.latitude = coordinate.latitude;
  165. self.BuguLive.longitude = coordinate.longitude;
  166. }
  167. NSLog(@"%f-------%f",self.BuguLive.latitude,self.BuguLive.longitude);
  168. // self.mapSearcher = [[QMSSearcher alloc]initWithDelegate:self];
  169. //
  170. // //配置搜索参数
  171. // QMSReverseGeoCodeSearchOption *reGeoSearchOption = [[QMSReverseGeoCodeSearchOption alloc] init];
  172. // [reGeoSearchOption setLocationWithCenterCoordinate:mapView.centerCoordinate];
  173. // [reGeoSearchOption setGet_poi:YES];
  174. // [self.mapSearcher searchWithReverseGeoCodeSearchOption:reGeoSearchOption];
  175. }
  176. /*!
  177. * @brief 地图区域即将改变时会调用此接口
  178. *
  179. * @param mapView 地图view
  180. * @param animated 是否采用动画
  181. */
  182. - (void)mapView:(QMapView *)mapView regionWillChangeAnimated:(BOOL)animated{
  183. //[self showHUD];
  184. _location = mapView.region.center;
  185. CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0};
  186. if (_location.longitude != 0 && _location.latitude != 0) {
  187. pt = (CLLocationCoordinate2D){_location.latitude, _location.longitude};
  188. }
  189. // //配置搜索参数
  190. // QMSReverseGeoCodeSearchOption *reGeoSearchOption = [[QMSReverseGeoCodeSearchOption alloc] init];
  191. // [reGeoSearchOption setLocationWithCenterCoordinate:mapView.centerCoordinate];
  192. // [reGeoSearchOption setGet_poi:YES];
  193. // [self.mapSearcher searchWithReverseGeoCodeSearchOption:reGeoSearchOption];
  194. }
  195. #pragma mark 反向地理编码
  196. - (void)searchWithReverseGeoCodeSearchOption:(QMSReverseGeoCodeSearchOption *)reverseGeoCodeSearchOption didReceiveResult:(QMSReverseGeoCodeSearchResult *)reverseGeoCodeSearchResult
  197. {
  198. NSLog(@"%@",reverseGeoCodeSearchResult.ad_info);
  199. if (reverseGeoCodeSearchResult)
  200. {
  201. if (reverseGeoCodeSearchResult.address && ![reverseGeoCodeSearchResult.address isEqualToString:@""]){
  202. QMSReGeoCodeAdInfo *reGeoCodeAdInfo = reverseGeoCodeSearchResult.ad_info;
  203. self.BuguLive.locationCity = reGeoCodeAdInfo.city;
  204. self.BuguLive.locateName = reverseGeoCodeSearchResult.formatted_addresses.recommend;
  205. self.BuguLive.province = reverseGeoCodeSearchResult.ad_info.province;
  206. self.BuguLive.area = reverseGeoCodeSearchResult.ad_info.district;
  207. NSMutableDictionary *mdic = [NSMutableDictionary dictionary];
  208. [mdic setObject:[NSString stringWithFormat:@"%.8f",self.BuguLive.longitude] forKey:@"lng"];
  209. [mdic setObject:[NSString stringWithFormat:@"%.8f",self.BuguLive.latitude] forKey:@"lat"];
  210. if (![reverseGeoCodeSearchResult.ad_info.province isEqual:@""]&&reverseGeoCodeSearchResult.ad_info.province) {
  211. [mdic setObject:reverseGeoCodeSearchResult.ad_info.province forKey:@"province"];
  212. }
  213. if (![reverseGeoCodeSearchResult.ad_info.city isEqual:@""]&&reverseGeoCodeSearchResult.ad_info.city) {
  214. [mdic setObject:reverseGeoCodeSearchResult.ad_info.city forKey:@"city"];
  215. }
  216. if (![reverseGeoCodeSearchResult.ad_info.district isEqual:@""]&&reverseGeoCodeSearchResult.ad_info.district) {
  217. [mdic setObject:reverseGeoCodeSearchResult.ad_info.district forKey:@"area"];
  218. }
  219. self.BuguLive.addressJsonStr = [BGUtils dataTOjsonString:mdic];
  220. _addressStr = self.BuguLive.locateName;
  221. // NSLog(@"%@",reverseGeoCodeSearchResult.formatted_addresses.recommend);
  222. _addressLabel.text = _addressStr;
  223. [self hideHUD];
  224. }
  225. }
  226. }
  227. - (void)searchWithSuggestionSearchOption:(QMSSuggestionSearchOption *)suggestionSearchOption didReceiveResult:(QMSSuggestionResult *)suggestionSearchResult
  228. {
  229. QMSSuggestionPoiData *poiData = [suggestionSearchResult.dataArray firstObject];
  230. CLLocationCoordinate2D coor = poiData.location;
  231. _myMapView.centerCoordinate = coor;
  232. }
  233. #pragma mark -- 定位失败
  234. - (void)mapView:(QMapView *)mapView didFailToLocateUserWithError:(NSError *)error {
  235. NSLog(ASLocalizedString(@"定位失败"));
  236. if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
  237. {
  238. // 用户关闭了该应用的定位权限.
  239. [FanweMessage alert:ASLocalizedString(@"您关闭了该应用的定位权限.请在设置->隐私->定位服务->本App下选择使用期间")];
  240. NSLog(ASLocalizedString(@"用户关闭了该应用的定位权限."));
  241. }
  242. }
  243. - (void)textChangeAction{
  244. _searchAddressController.view.hidden = NO;
  245. if ([_searchTF.text isEqualToString:@""]) {
  246. _searchAddressController.view.hidden = YES;
  247. }else{
  248. [_searchAddressController searchCityname:self.BuguLive.locationCity keyword:_searchTF.text];
  249. }
  250. }
  251. - (BOOL)textFieldShouldClear:(UITextField *)textField{
  252. _searchAddressController.view.hidden = YES;
  253. return YES;
  254. }
  255. - (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
  256. return YES;
  257. }
  258. #pragma marks ------协议-----------------
  259. - (void)SelectedAdress:(NSString *)adress withPt:(CLLocationCoordinate2D)pt andProvinceStr:(NSString *)provinceStr andCityStr:(NSString *)cityStr andAreaStr:(NSString *)areaStr {
  260. _location = pt;
  261. _addressStr = adress;
  262. if (_delegate)
  263. {
  264. [_delegate chooseAddress:_location address:_addressStr andProvinceString:provinceStr andCityString:cityStr andAreaString:areaStr];
  265. [self.navigationController popViewControllerAnimated:YES];
  266. }
  267. }
  268. - (void)showHUD
  269. {
  270. if(_HUD==nil)
  271. {
  272. _HUD = [[MBProgressHUD alloc] initWithView:self.view];
  273. [self.view addSubview:_HUD];
  274. [self.view bringSubviewToFront:_HUD];
  275. _HUD.userInteractionEnabled = NO;
  276. _HUD.delegate = self;
  277. [_HUD showAnimated:YES];
  278. }
  279. else
  280. {
  281. [self.view addSubview:_HUD];
  282. [_HUD showAnimated:YES];
  283. }
  284. }
  285. - (void)hideHUD
  286. {
  287. if(_HUD)
  288. {
  289. [_HUD hideAnimated:YES];
  290. }
  291. }
  292. - (void)searchWithSearchOption:(QMSSearchOption *)searchOption didFailWithError:(NSError*)error
  293. {
  294. [self hideHUD];
  295. [[BGHUDHelper sharedInstance] tipMessage:[NSString stringWithFormat:@"%@",error]];
  296. }
  297. - (void)searchForPoint{
  298. //配置搜索参数
  299. QMSSuggestionSearchOption *suggetionOption = [[QMSSuggestionSearchOption alloc] init];
  300. [suggetionOption setKeyword:_addressName];
  301. [suggetionOption setRegion:_addressName];
  302. [self.mapSearcher searchWithSuggestionSearchOption:suggetionOption];
  303. }
  304. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  305. {
  306. [_searchTF resignFirstResponder];
  307. }
  308. - (void)didReceiveMemoryWarning {
  309. [super didReceiveMemoryWarning];
  310. // Dispose of any resources that can be recreated.
  311. }
  312. @end