// // Addplace_VC.m // Haishenghai-master // // Created by GG on 2019/1/3. // Copyright © 2019年 Haishenghai intelligence network technology. All rights reserved. // #define VerifyValue(value) ({id tmp;if ([value isKindOfClass:[NSNull class]])tmp = nil;else tmp = value;tmp;}) #import "Addplace_VC.h" #import "Header.h" @interface Addplace_VC () { NSString *site; NSString *siteName; } @property(nonatomic,strong)AMapSearchAPI *seatch; @property(nonatomic,strong)AMapLocationManager *locationManager; @property(nonatomic,assign)BOOL locationWithReGeocode; @property(nonatomic,strong)MAMapView *mapView; @property(nonatomic,copy)NSString *currentLocation; @property (nonatomic,strong)UITextField *inputTF; @property (nonatomic,strong)UILabel *regionalismLabel; @property (nonatomic,strong)UILabel *locationLabel; @property(nonatomic,copy)NSString *latitude; @property(nonatomic,strong)NSString *longtitude; @property(nonatomic,strong)NSString *regionalism; @property(nonatomic,strong)NSString *siteID; @end @implementation Addplace_VC -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeValue:) name:@"changeValue" object:nil]; // //地图定位 // [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { // if(status==AFNetworkReachabilityStatusNotReachable) // { // ALERTSHOW(@"请检查网络是否畅通!"); // // return ; // } // else // { // // } // }]; } -(void)changeValue:(NSNotification *)noti{ NSDictionary *dic = noti.userInfo; NSLog(@"-----%@",dic); site = [dic objectForKey:@"site"]; [self.tableView reloadData]; } - (void)viewDidLoad { [super viewDidLoad]; site = @""; self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:60/255.0 green:114/255.0 blue:255/255.0 alpha:1]; [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}]; //导航栏添加返回按钮 //设置导航栏左侧按钮 UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [backBtn setImage:[UIImage imageNamed:@"hsh_return"] forState:UIControlStateNormal]; backBtn.frame = CGRectMake(0, 0, 44, 44); [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside]; backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn]; self.navigationItem.title = @"添加场所"; self.view.backgroundColor = [UIColor whiteColor]; [self setMapView]; [self setupUI]; } -(void)backClick{ [self.navigationController popViewControllerAnimated:YES]; } //引入高德地图 -(void)setMapView{ //地图需要V4.5.0及以上版本才必须打开此选项 [AMapServices sharedServices].enableHTTPS = YES; //初始化地图 MAMapView *_mapView = [[MAMapView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT/2-64)]; [self.view addSubview:_mapView]; //显示定位蓝点 _mapView.showsUserLocation = YES; _mapView.userTrackingMode = MAUserTrackingModeFollow; _mapView.showsIndoorMap = YES;//显示室内地图;No不显示 //显示地图图层 // [_mapView setMapType:MAMapTypeSatellite]; //显示指南针 _mapView.showsCompass = YES;//默认是关闭的 _mapView.compassOrigin = CGPointMake(_mapView.compassOrigin.x, 20); // 比例尺 _mapView.showsScale = YES; _mapView.scaleOrigin = CGPointMake(0, 0); [_mapView setZoomLevel:15]; //持续定位 self.locationManager = [[AMapLocationManager alloc]init]; self.locationManager.delegate =self; self.locationManager.distanceFilter =20; [self.locationManager startUpdatingHeading]; [self.locationManager setLocatingWithReGeocode:YES]; //单次定位//高精度 [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; //定位超时时间 self.locationManager.locationTimeout =2; self.locationManager.reGeocodeTimeout =2; [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) { NSLog(@"location%@",location); if (regeocode) { if (![regeocode isKindOfClass:[NSNull class]]) { NSString *stringValue =([NSString stringWithFormat:@"%@ %@",regeocode.city,regeocode.district]); _regionalism = VerifyValue(stringValue); _regionalismLabel.text = [NSString stringWithFormat:@"区域:%@",_regionalism]; NSString *lacationString = regeocode.formattedAddress; _currentLocation = VerifyValue(lacationString); _locationLabel.text = [NSString stringWithFormat:@"详细地址:%@",_currentLocation]; } [_tableView reloadData]; } }]; } -(void)setupUI{ UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; rightButton.frame = CGRectMake(0, 0, 44, 44); [rightButton setTitle:@"确认" forState:UIControlStateNormal]; [rightButton addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton]; self.navigationItem.rightBarButtonItem = rightItem; //地图部分 //自定义底部视图 _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, HEIGHT/2, self.view.frame.size.width, HEIGHT/2) style:UITableViewStylePlain]; // _tableView.backgroundColor = [UIColor grayColor]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.userInteractionEnabled = YES; _tableView.showsHorizontalScrollIndicator=NO; [self.view addSubview:_tableView]; } #pragma mark-- tableviewDelegate -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 1; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 2; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 0; } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 5; } -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ UIView *header = [[UIView alloc]init]; // header.frame = CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>) header.backgroundColor = [UIColor colorWithRed:227/255.0 green:227/255.0 blue:227/255.0 alpha:1]; return header; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section==0) { return 130; }else{ return 70; } return 0; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellId = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId]; } cell.selectionStyle = UITableViewCellSelectionStyleNone; if (indexPath.section==0) { UILabel *inputLabel = [[UILabel alloc]init]; inputLabel.frame = CGRectMake(10, 0, 100, 40); inputLabel.text = @"场所名称:"; inputLabel.font = [UIFont systemFontOfSize:18]; [cell.contentView addSubview:inputLabel]; _inputTF = [[UITextField alloc]init]; _inputTF.backgroundColor = [UIColor whiteColor]; _inputTF.frame = CGRectMake(100, 0, WIDTH-110, 40); _inputTF.tintColor = [UIColor blueColor]; _inputTF.text =site; _inputTF.placeholder = @"请输入名称(手动输入)"; _inputTF.delegate = self; [cell.contentView addSubview:_inputTF]; _regionalismLabel = [[UILabel alloc]init]; _regionalismLabel.frame = CGRectMake(10, 40, WIDTH-20, 30); _regionalismLabel.font = [UIFont systemFontOfSize:18]; _regionalismLabel.text = @"区域:"; [cell.contentView addSubview:_regionalismLabel]; _locationLabel = [[UILabel alloc]init]; _locationLabel.frame = CGRectMake(10, 70, WIDTH-20, 50); _locationLabel.font = [UIFont systemFontOfSize:18]; _locationLabel.text = @""; _locationLabel.numberOfLines =0; _locationLabel.lineBreakMode = NSLineBreakByWordWrapping; [cell.contentView addSubview:_locationLabel]; }else{ // NSArray *titles = @[@"负责人:",@"手机号:"]; for (int i =0; i<2; i++) { UILabel *titleLab = [[UILabel alloc]init]; titleLab.frame = CGRectMake(10+(WIDTH-20)*(i/2), 5+(25+5)*(i%2), WIDTH-20, 25); titleLab.tag=i+1; if (titleLab.tag==1) { NSString *username = [[NSUserDefaults standardUserDefaults]objectForKey:@"username"]; titleLab.text = [NSString stringWithFormat:@"负责人:%@",username]; }else{ NSString *phone = [[NSUserDefaults standardUserDefaults]objectForKey:@"phone"]; titleLab.text = [NSString stringWithFormat:@"手机号:%@",phone]; } titleLab.font = [UIFont systemFontOfSize:18]; [cell.contentView addSubview:titleLab]; } } return cell; } #pragma mark----lazy - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)confirmBtnClick{ NSString *inputText = [_inputTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; if (inputText.length ==0) { ALERTSHOW(@"场所名称不能为空"); return; } [DataSourceManager addSiteWithUrlDeployment:_inputTF.text RegionName:_regionalism Longtitude:@"120" Latitude:@"120" Location:_currentLocation RegionCode:@"2345" completionBlock:^(NSDictionary *dic) { if ([[dic objectForKey:@"code"]isEqualToNumber:@(0)]) { _siteID = [dic objectForKey:@"data"]; NSLog(@"这是我要的数据%@",_siteID); [_tableView reloadData]; [self showAlertWithTitle:@"提示" message:@"是否添加联系人?" sureHandler:^{ Contacts_VC *contactsVC = [[Contacts_VC alloc]init]; contactsVC.SiteId = _siteID; contactsVC.siteStr = siteName; [self.navigationController pushViewController:contactsVC animated:YES]; } cancelHandler:^{ [self.navigationController popViewControllerAnimated:YES]; }]; } }]; } //提示弹窗 - (void)showAlertWithTitle:(NSString *)title message:(NSString *)message sureHandler:(void (^)())sureHandler cancelHandler:(void (^)())cancelHandler { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:sureHandler]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleCancel handler:cancelHandler]; [alertController addAction:sureAction]; [alertController addAction:cancelAction]; [self presentViewController:alertController animated:YES completion:nil]; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ #pragma mark----AMapDelegate- //高德地图代理 //- (void)mapViewWillStartLocatingUser:(MAMapView *)mapView //{ // if(![CLLocationManager locationServicesEnabled]){ // UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"定位失败" message:@"请在手机设置中开启定位功能\n开启步骤:设置 > 隐私 > 位置 > 定位服务" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; // [alertView show]; // self.mapView = nil; // self.mapView.delegate = nil; // return; // }else{ // // if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways) { // UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"定位失败" message:@"请在手机设置中开启定位功能\n开启步骤:设置 > 隐私 > 位置 > 定位服务下《***》应用" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; // [alertView show]; // self.mapView = nil; // self.mapView.delegate = nil; // return; // } // } //} -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocation * location = [locations firstObject]; _latitude = [NSString stringWithFormat:@"%f",location.coordinate.latitude] ; _longtitude = [NSString stringWithFormat:@"%f",location.coordinate.longitude]; CLGeocoder * geocoder = [[CLGeocoder alloc]init]; [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { if (error == nil && [placemarks count] > 0) { CLPlacemark * placemark = [placemarks firstObject]; NSLog(@"%@",placemark.addressDictionary); } }]; } -(void)viewWillDisappear:(BOOL)animated{ [self.locationManager stopUpdatingLocation]; } -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ siteName = textField.text; return YES; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.view endEditing:YES]; } @end