Addplace_VC.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. //
  2. // Addplace_VC.m
  3. // Haishenghai-master
  4. //
  5. // Created by GG on 2019/1/3.
  6. // Copyright © 2019年 Haishenghai intelligence network technology. All rights reserved.
  7. //
  8. #define VerifyValue(value) ({id tmp;if ([value isKindOfClass:[NSNull class]])tmp = nil;else tmp = value;tmp;})
  9. #import "Addplace_VC.h"
  10. #import "Header.h"
  11. @interface Addplace_VC ()<UITableViewDelegate,UITableViewDataSource,AMapSearchDelegate,AMapLocationManagerDelegate,UITextFieldDelegate>
  12. {
  13. NSString *site;
  14. NSString *siteName;
  15. }
  16. @property(nonatomic,strong)AMapSearchAPI *seatch;
  17. @property(nonatomic,strong)AMapLocationManager *locationManager;
  18. @property(nonatomic,assign)BOOL locationWithReGeocode;
  19. @property(nonatomic,strong)MAMapView *mapView;
  20. @property(nonatomic,copy)NSString *currentLocation;
  21. @property (nonatomic,strong)UITextField *inputTF;
  22. @property (nonatomic,strong)UILabel *regionalismLabel;
  23. @property (nonatomic,strong)UILabel *locationLabel;
  24. @property(nonatomic,copy)NSString *latitude;
  25. @property(nonatomic,strong)NSString *longtitude;
  26. @property(nonatomic,strong)NSString *regionalism;
  27. @property(nonatomic,strong)NSString *siteID;
  28. @end
  29. @implementation Addplace_VC
  30. -(void)viewWillAppear:(BOOL)animated{
  31. [super viewWillAppear:animated];
  32. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeValue:) name:@"changeValue" object:nil];
  33. // //地图定位
  34. // [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
  35. // if(status==AFNetworkReachabilityStatusNotReachable)
  36. // {
  37. // ALERTSHOW(@"请检查网络是否畅通!");
  38. //
  39. // return ;
  40. // }
  41. // else
  42. // {
  43. //
  44. // }
  45. // }];
  46. }
  47. -(void)changeValue:(NSNotification *)noti{
  48. NSDictionary *dic = noti.userInfo;
  49. NSLog(@"-----%@",dic);
  50. site = [dic objectForKey:@"site"];
  51. [self.tableView reloadData];
  52. }
  53. - (void)viewDidLoad {
  54. [super viewDidLoad];
  55. site = @"";
  56. self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:60/255.0 green:114/255.0 blue:255/255.0 alpha:1];
  57. [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
  58. //导航栏添加返回按钮
  59. //设置导航栏左侧按钮
  60. UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  61. [backBtn setImage:[UIImage imageNamed:@"hsh_return"] forState:UIControlStateNormal];
  62. backBtn.frame = CGRectMake(0, 0, 44, 44);
  63. [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
  64. backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  65. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
  66. self.navigationItem.title = @"添加场所";
  67. self.view.backgroundColor = [UIColor whiteColor];
  68. [self setMapView];
  69. [self setupUI];
  70. }
  71. -(void)backClick{
  72. [self.navigationController popViewControllerAnimated:YES];
  73. }
  74. //引入高德地图
  75. -(void)setMapView{
  76. //地图需要V4.5.0及以上版本才必须打开此选项
  77. [AMapServices sharedServices].enableHTTPS = YES;
  78. //初始化地图
  79. MAMapView *_mapView = [[MAMapView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT/2-64)];
  80. [self.view addSubview:_mapView];
  81. //显示定位蓝点
  82. _mapView.showsUserLocation = YES;
  83. _mapView.userTrackingMode = MAUserTrackingModeFollow;
  84. _mapView.showsIndoorMap = YES;//显示室内地图;No不显示
  85. //显示地图图层
  86. // [_mapView setMapType:MAMapTypeSatellite];
  87. //显示指南针
  88. _mapView.showsCompass = YES;//默认是关闭的
  89. _mapView.compassOrigin = CGPointMake(_mapView.compassOrigin.x, 20);
  90. // 比例尺
  91. _mapView.showsScale = YES;
  92. _mapView.scaleOrigin = CGPointMake(0, 0);
  93. [_mapView setZoomLevel:15];
  94. //持续定位
  95. self.locationManager = [[AMapLocationManager alloc]init];
  96. self.locationManager.delegate =self;
  97. self.locationManager.distanceFilter =20;
  98. [self.locationManager startUpdatingHeading];
  99. [self.locationManager setLocatingWithReGeocode:YES];
  100. //单次定位//高精度
  101. [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
  102. //定位超时时间
  103. self.locationManager.locationTimeout =2;
  104. self.locationManager.reGeocodeTimeout =2;
  105. [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
  106. NSLog(@"location%@",location);
  107. if (regeocode) {
  108. if (![regeocode isKindOfClass:[NSNull class]]) {
  109. NSString *stringValue =([NSString stringWithFormat:@"%@ %@",regeocode.city,regeocode.district]);
  110. _regionalism = VerifyValue(stringValue);
  111. _regionalismLabel.text = [NSString stringWithFormat:@"区域:%@",_regionalism];
  112. NSString *lacationString = regeocode.formattedAddress;
  113. _currentLocation = VerifyValue(lacationString);
  114. _locationLabel.text = [NSString stringWithFormat:@"详细地址:%@",_currentLocation];
  115. }
  116. [_tableView reloadData];
  117. }
  118. }];
  119. }
  120. -(void)setupUI{
  121. UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
  122. rightButton.frame = CGRectMake(0, 0, 44, 44);
  123. [rightButton setTitle:@"确认" forState:UIControlStateNormal];
  124. [rightButton addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
  125. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
  126. self.navigationItem.rightBarButtonItem = rightItem;
  127. //地图部分
  128. //自定义底部视图
  129. _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, HEIGHT/2, self.view.frame.size.width, HEIGHT/2) style:UITableViewStylePlain];
  130. // _tableView.backgroundColor = [UIColor grayColor];
  131. _tableView.delegate = self;
  132. _tableView.dataSource = self;
  133. _tableView.userInteractionEnabled = YES;
  134. _tableView.showsHorizontalScrollIndicator=NO;
  135. [self.view addSubview:_tableView];
  136. }
  137. #pragma mark-- tableviewDelegate
  138. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  139. return 1;
  140. }
  141. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  142. return 2;
  143. }
  144. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  145. return 0;
  146. }
  147. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  148. return 5;
  149. }
  150. -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
  151. UIView *header = [[UIView alloc]init];
  152. // header.frame = CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)
  153. header.backgroundColor = [UIColor colorWithRed:227/255.0 green:227/255.0 blue:227/255.0 alpha:1];
  154. return header;
  155. }
  156. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  157. if (indexPath.section==0) {
  158. return 130;
  159. }else{
  160. return 70;
  161. }
  162. return 0;
  163. }
  164. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  165. static NSString *cellId = @"cell";
  166. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
  167. if (!cell) {
  168. cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
  169. }
  170. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  171. if (indexPath.section==0) {
  172. UILabel *inputLabel = [[UILabel alloc]init];
  173. inputLabel.frame = CGRectMake(10, 0, 100, 40);
  174. inputLabel.text = @"场所名称:";
  175. inputLabel.font = [UIFont systemFontOfSize:18];
  176. [cell.contentView addSubview:inputLabel];
  177. _inputTF = [[UITextField alloc]init];
  178. _inputTF.backgroundColor = [UIColor whiteColor];
  179. _inputTF.frame = CGRectMake(100, 0, WIDTH-110, 40);
  180. _inputTF.tintColor = [UIColor blueColor];
  181. _inputTF.text =site;
  182. _inputTF.placeholder = @"请输入名称(手动输入)";
  183. _inputTF.delegate = self;
  184. [cell.contentView addSubview:_inputTF];
  185. _regionalismLabel = [[UILabel alloc]init];
  186. _regionalismLabel.frame = CGRectMake(10, 40, WIDTH-20, 30);
  187. _regionalismLabel.font = [UIFont systemFontOfSize:18];
  188. _regionalismLabel.text = @"区域:";
  189. [cell.contentView addSubview:_regionalismLabel];
  190. _locationLabel = [[UILabel alloc]init];
  191. _locationLabel.frame = CGRectMake(10, 70, WIDTH-20, 50);
  192. _locationLabel.font = [UIFont systemFontOfSize:18];
  193. _locationLabel.text = @"";
  194. _locationLabel.numberOfLines =0;
  195. _locationLabel.lineBreakMode = NSLineBreakByWordWrapping;
  196. [cell.contentView addSubview:_locationLabel];
  197. }else{
  198. // NSArray *titles = @[@"负责人:",@"手机号:"];
  199. for (int i =0; i<2; i++) {
  200. UILabel *titleLab = [[UILabel alloc]init];
  201. titleLab.frame = CGRectMake(10+(WIDTH-20)*(i/2), 5+(25+5)*(i%2), WIDTH-20, 25);
  202. titleLab.tag=i+1;
  203. if (titleLab.tag==1) {
  204. NSString *username = [[NSUserDefaults standardUserDefaults]objectForKey:@"username"];
  205. titleLab.text = [NSString stringWithFormat:@"负责人:%@",username];
  206. }else{
  207. NSString *phone = [[NSUserDefaults standardUserDefaults]objectForKey:@"phone"];
  208. titleLab.text = [NSString stringWithFormat:@"手机号:%@",phone];
  209. }
  210. titleLab.font = [UIFont systemFontOfSize:18];
  211. [cell.contentView addSubview:titleLab];
  212. }
  213. }
  214. return cell;
  215. }
  216. #pragma mark----lazy
  217. - (void)didReceiveMemoryWarning {
  218. [super didReceiveMemoryWarning];
  219. // Dispose of any resources that can be recreated.
  220. }
  221. -(void)confirmBtnClick{
  222. NSString *inputText = [_inputTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  223. if (inputText.length ==0) {
  224. ALERTSHOW(@"场所名称不能为空");
  225. return;
  226. }
  227. [DataSourceManager addSiteWithUrlDeployment:_inputTF.text RegionName:_regionalism Longtitude:@"120" Latitude:@"120" Location:_currentLocation RegionCode:@"2345" completionBlock:^(NSDictionary *dic) {
  228. if ([[dic objectForKey:@"code"]isEqualToNumber:@(0)]) {
  229. _siteID = [dic objectForKey:@"data"];
  230. NSLog(@"这是我要的数据%@",_siteID);
  231. [_tableView reloadData];
  232. [self showAlertWithTitle:@"提示" message:@"是否添加联系人?" sureHandler:^{
  233. Contacts_VC *contactsVC = [[Contacts_VC alloc]init];
  234. contactsVC.SiteId = _siteID;
  235. contactsVC.siteStr = siteName;
  236. [self.navigationController pushViewController:contactsVC animated:YES];
  237. } cancelHandler:^{
  238. [self.navigationController popViewControllerAnimated:YES];
  239. }];
  240. }
  241. }];
  242. }
  243. //提示弹窗
  244. - (void)showAlertWithTitle:(NSString *)title message:(NSString *)message sureHandler:(void (^)())sureHandler cancelHandler:(void (^)())cancelHandler
  245. {
  246. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
  247. UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:sureHandler];
  248. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleCancel handler:cancelHandler];
  249. [alertController addAction:sureAction];
  250. [alertController addAction:cancelAction];
  251. [self presentViewController:alertController animated:YES completion:nil];
  252. }
  253. /*
  254. #pragma mark - Navigation
  255. // In a storyboard-based application, you will often want to do a little preparation before navigation
  256. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  257. // Get the new view controller using [segue destinationViewController].
  258. // Pass the selected object to the new view controller.
  259. }
  260. */
  261. #pragma mark----AMapDelegate-
  262. //高德地图代理
  263. //- (void)mapViewWillStartLocatingUser:(MAMapView *)mapView
  264. //{
  265. // if(![CLLocationManager locationServicesEnabled]){
  266. // UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"定位失败" message:@"请在手机设置中开启定位功能\n开启步骤:设置 > 隐私 > 位置 > 定位服务" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  267. // [alertView show];
  268. // self.mapView = nil;
  269. // self.mapView.delegate = nil;
  270. // return;
  271. // }else{
  272. //
  273. // if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways) {
  274. // UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"定位失败" message:@"请在手机设置中开启定位功能\n开启步骤:设置 > 隐私 > 位置 > 定位服务下《***》应用" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  275. // [alertView show];
  276. // self.mapView = nil;
  277. // self.mapView.delegate = nil;
  278. // return;
  279. // }
  280. // }
  281. //}
  282. -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
  283. CLLocation * location = [locations firstObject];
  284. _latitude = [NSString stringWithFormat:@"%f",location.coordinate.latitude] ;
  285. _longtitude = [NSString stringWithFormat:@"%f",location.coordinate.longitude];
  286. CLGeocoder * geocoder = [[CLGeocoder alloc]init];
  287. [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
  288. if (error == nil && [placemarks count] > 0) {
  289. CLPlacemark * placemark = [placemarks firstObject];
  290. NSLog(@"%@",placemark.addressDictionary);
  291. }
  292. }];
  293. }
  294. -(void)viewWillDisappear:(BOOL)animated{
  295. [self.locationManager stopUpdatingLocation];
  296. }
  297. -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  298. siteName = textField.text;
  299. return YES;
  300. }
  301. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  302. [textField resignFirstResponder];
  303. return YES;
  304. }
  305. -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  306. [self.view endEditing:YES];
  307. }
  308. @end