// // CollectRegionsVC.m // LN_School // // Created by apple on 2017/12/7. // Copyright © 2017年 Danson. All rights reserved. // #import "CollectRegionsVC.h" #import //引入地图功能所有的头文件 #import //引入定位功能所有的头文件(新) #import //测量工具 @interface CollectRegionsVC () { BMKMapView *_mapView; BMKLocationManager *_locService; CLLocationCoordinate2D preCoordinate; NSMutableArray *annotationArray; BOOL isEnd; NSInteger eArea; } @property (nonatomic, strong) BMKUserLocation *userLocation;//当前位置对象 @end @implementation CollectRegionsVC - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"围栏采集"; self.view.backgroundColor = [UIColor whiteColor]; self.navigationController.navigationBar.translucent = NO; [self goBackByNavigation]; isEnd = NO; eArea = 0; annotationArray = [NSMutableArray array]; //实例化地图 _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, kSize.width, kSize.height - 44 )]; _mapView.delegate = self; _mapView.mapType = BMKMapTypeStandard; _mapView.userTrackingMode = BMKUserTrackingModeNone; _mapView.zoomLevel = 17; _mapView.showsUserLocation = YES; [self.view addSubview:_mapView]; //定位功能 _locService = [[BMKLocationManager alloc]init]; _locService.delegate = self; [_locService startUpdatingLocation]; //卫星/标准地图 NSArray *segTitleArray = @[@"平面",@"卫星"]; UISegmentedControl *segMent = [[UISegmentedControl alloc] initWithItems:segTitleArray]; segMent.frame = CGRectMake(20, 25, 100, 40); [segMent borderCornorRadios:2]; segMent.backgroundColor = [UIColor whiteColor]; segMent.tintColor = RQMianColor; segMent.selectedSegmentIndex = 0; [segMent addTarget:self action:@selector(clickSegMent:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:segMent]; //按钮布局 NSArray *titleArray = @[@"设置\n起点",@"过程\n采集",@"设置\n终点",@"重新\n采集",@"生成\n围栏"]; for (int i = 0; i < titleArray.count; i ++) { UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(kSize.width - 50, kSize.height - 270 - kNavOffSet + i*50, 40, 40)]; [btn borderColor:KContentTextColor width:1.2 cornorRadios:3]; btn.backgroundColor = [UIColor whiteColor]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; btn.titleLabel.font = [UIFont systemFontOfSize:12]; btn.titleLabel.numberOfLines = 0; [btn setTitle:titleArray[i] forState:UIControlStateNormal]; btn.tag = i + 1; [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } } //上传围栏 - (void)updatePoint { } - (void)clickSegMent:(UISegmentedControl *)seg { if (seg.selectedSegmentIndex == 0) { _mapView.mapType = BMKMapTypeStandard; } if (seg.selectedSegmentIndex == 1) { _mapView.mapType = BMKMapTypeSatellite; } } - (void)btnClick:(UIButton *)sender { if (sender.tag == 1) { if (annotationArray.count > 0) { ShowMsg(@"起点已存在!"); return; } //绘制起点 BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc]init]; annotation.coordinate = preCoordinate; annotation.title = @"起点"; [_mapView addAnnotation:annotation]; [annotationArray addObject:annotation]; _mapView.centerCoordinate = preCoordinate; } if (sender.tag == 2) { if (annotationArray.count < 1) { ShowMsg(@"请先设置起点!"); return; } if (isEnd) { ShowMsg(@"终点已存在!"); return; } //绘制过程点 BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc]init]; annotation.coordinate = preCoordinate; annotation.title = @"过程点"; [_mapView addAnnotation:annotation]; [annotationArray addObject:annotation]; _mapView.centerCoordinate = preCoordinate; if (annotationArray.count > 1) { //画线 CLLocationCoordinate2D coor[2] = {0}; annotation = annotationArray[annotationArray.count - 2]; coor[0] = annotation.coordinate; coor[1] = preCoordinate; BMKPolyline* polyline = [BMKPolyline polylineWithCoordinates:coor count:2]; [_mapView addOverlay:polyline]; //语音播报点距离 BMKMapPoint point1 = BMKMapPointForCoordinate(coor[0]); BMKMapPoint point2 = BMKMapPointForCoordinate(coor[1]); CLLocationDistance distance = BMKMetersBetweenMapPoints(point1,point2); NSString *string = [NSString stringWithFormat:@"当前坐标与上一个坐标位置之间的距离为%d米",(int)distance]; [Tools playAudioWithString:string]; } } if (sender.tag == 3) { if (annotationArray.count < 2) { ShowMsg(@"请采集过程点!"); return; } if (isEnd) { ShowMsg(@"终点已存在!"); return; } isEnd = YES; //绘制终点 BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc]init]; annotation.coordinate = preCoordinate; annotation.title = @"终点"; [_mapView addAnnotation:annotation]; [annotationArray addObject:annotation]; _mapView.centerCoordinate = preCoordinate; if (annotationArray.count > 1) { //画线 CLLocationCoordinate2D coor[2] = {0}; annotation = annotationArray[annotationArray.count - 2]; coor[0] = annotation.coordinate; coor[1] = preCoordinate; BMKPolyline* polyline = [BMKPolyline polylineWithCoordinates:coor count:2]; [_mapView addOverlay:polyline]; //语音播报点距离 BMKMapPoint point1 = BMKMapPointForCoordinate(coor[0]); BMKMapPoint point2 = BMKMapPointForCoordinate(coor[1]); CLLocationDistance distance = BMKMetersBetweenMapPoints(point1,point2); NSString *string = [NSString stringWithFormat:@"当前坐标与上一个坐标位置之间的距离为%d米",(int)distance]; [Tools playAudioWithString:string]; //首尾连接线 coor[0] = preCoordinate; annotation = [annotationArray firstObject]; coor[1] = annotation.coordinate; polyline = [BMKPolyline polylineWithCoordinates:coor count:2]; [_mapView addOverlay:polyline]; } } //重新采集 if (sender.tag == 4) { //去掉点 NSArray *array = [[NSArray alloc]initWithArray:_mapView.annotations]; if (array.count > 0) { [_mapView removeAnnotations: array]; } //取掉线、面 array = _mapView.overlays; if (array.count > 0) { [_mapView removeOverlays:array]; } isEnd = NO; [annotationArray removeAllObjects]; } //生成围栏 if (sender.tag == 5) { if (!isEnd) { ShowMsg(@"请设置终点!"); return; } if (annotationArray.count > 100) { ShowMsg(@"围栏点不能大于100个"); return; } //去掉点 NSArray *array = [[NSArray alloc]initWithArray:_mapView.annotations]; if (array.count > 0) { [_mapView removeAnnotations: array]; } //取掉线、面 array = _mapView.overlays; if (array.count > 0) { [_mapView removeOverlays:array]; } //绘制面 CLLocationCoordinate2D coords[100] = {0}; for (int i = 0; i < annotationArray.count; i ++) { BMKPointAnnotation *annotation = annotationArray[i]; coords[i] = annotation.coordinate; } BMKPolygon *polygon = [BMKPolygon polygonWithCoordinates:coords count:annotationArray.count]; [_mapView addOverlay:polygon]; //获取电子围栏面积 [self getPolygonArea]; // ShowMsg(@"正在计算围栏面积,请稍后!"); } } #pragma mark 网络请求 - (void)getPolygonArea{ //判断网络是否连接 if (![NetManager connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableArray *array = [NSMutableArray arrayWithCapacity:annotationArray.count]; for (BMKPointAnnotation *annotation in annotationArray) { [array addObject:@{@"lat": [NSString stringWithFormat:@"%f",annotation.coordinate.latitude],@"lng":[NSString stringWithFormat:@"%f",annotation.coordinate.longitude]}]; } NSMutableDictionary *mdic = [NSMutableDictionary dictionary]; [mdic setValue:array forKey:@"polygon"]; [MBProgressHUD showLoadToView:self.view]; [NetManager requestAnythingWithURL:@"getPolygonArea" dictionary:mdic dataArray:nil completion:^(NSDictionary *root) { [MBProgressHUD hideHUDForView:self.view]; if (!root) { ShowMsg(@"数据请求失败,请重试"); return; } if ([root[@"code"] integerValue] == 1) { ShowMsg(root[@"msg"]); return; } eArea = [root[@"body"] integerValue]; if (eArea < 1) { ShowMsg(@"电子场地面积小于1(㎡),请重新采集!"); return; } if (eArea <= [_infoDic[@"area"] floatValue] * 1.1) { //满足条件 可以提交 UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:[NSString stringWithFormat:@"采集电子场地面积为%d(㎡),是否提交保存并送审?",(int)eArea] preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction * doneAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self addRegion]; }]; UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alert addAction:doneAction]; [alert addAction:cancelAction]; [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:alert animated:YES completion:nil]; }else { //电子面积大于实际10% 重新采集或者修改实际面积 UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:[NSString stringWithFormat:@"采集电子场地面积为%d(㎡),超出实际面积%@(㎡)的10%%,请重新采集围栏",(int)eArea,_infoDic[@"area"]] preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil]; [alert addAction:cancelAction]; [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:alert animated:YES completion:nil]; } }]; } - (void)addRegion{ //判断网络是否连接 if (![NetManager connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableArray *array = [NSMutableArray arrayWithCapacity:annotationArray.count]; for (BMKPointAnnotation *annotation in annotationArray) { [array addObject:@{@"lat": [NSString stringWithFormat:@"%f",annotation.coordinate.latitude],@"lng":[NSString stringWithFormat:@"%f",annotation.coordinate.longitude]}]; } NSMutableDictionary *mdic = [NSMutableDictionary dictionary]; [mdic setValue:defUser.userDict[@"id"] forKey:@"userId"]; [mdic setValue:_infoDic[@"name"] forKey:@"name"]; [mdic setValue:[NSString stringWithFormat:@"%d",(int)eArea] forKey:@"area"]; [mdic setValue:_infoDic[@"address"] forKey:@"address"]; [mdic setValue:_infoDic[@"totalvehnum"] forKey:@"totalvehnum"]; [mdic setValue:_infoDic[@"curvehnum"] forKey:@"curvehnum"]; [mdic setValue:_infoDic[@"vehicletype"] forKey:@"vehicletype"]; [mdic setValue:_infoDic[@"remark"] forKey:@"remark"]; [mdic setValue:array forKey:@"dzwl"]; [MBProgressHUD showLoadToView:self.view]; [NetManager requestAnythingWithURL:@"addRegion" dictionary:mdic dataArray:nil completion:^(NSDictionary *root) { [MBProgressHUD hideHUDForView:self.view]; if (!root) { ShowMsg(@"数据请求失败,请重试"); return; } if ([root[@"code"] integerValue] == 1) { ShowMsg(root[@"msg"]); return; } UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:root[@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alert show]; }]; } #pragma mark 地图 //处理位置坐标更新 - (void)BMKLocationManager:(BMKLocationManager *)manager didUpdateLocation:(BMKLocation *)location orError:(NSError *)error { self.userLocation.location = location.location; [_mapView updateLocationData:_userLocation]; preCoordinate = _userLocation.location.coordinate; if (annotationArray.count < 1) { _mapView.centerCoordinate = _userLocation.location.coordinate; } } //标注的代理 -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation { if ([annotation isKindOfClass:[BMKPointAnnotation class]]) { BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"]; // newAnnotationView.pinColor = BMKPinAnnotationColorRed; if ([annotation.title isEqualToString:@"起点"]) { newAnnotationView.image = [UIImage imageNamed:@"icon_start"]; } if ([annotation.title isEqualToString:@"过程点"]) { newAnnotationView.image = [UIImage imageNamed:@"icon_waypoint"]; } if ([annotation.title isEqualToString:@"终点"]) { newAnnotationView.image = [UIImage imageNamed:@"icon_end"]; } // 设置该标注点动画显示 newAnnotationView.animatesDrop = NO; return newAnnotationView; } return nil; } //根据overlay生成对应的View - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id )overlay { if ([overlay isKindOfClass:[BMKPolyline class]]) { BMKPolylineView *polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay]; polylineView.lineWidth = 3.0; polylineView.strokeColor = [UIColor redColor]; return polylineView; } if ([overlay isKindOfClass:[BMKPolygon class]]){ BMKPolygonView* polygonView = [[BMKPolygonView alloc] initWithOverlay:overlay]; polygonView.strokeColor = [[UIColor redColor] colorWithAlphaComponent:1]; polygonView.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.3]; polygonView.lineWidth = 3.0; polygonView.lineDash = YES;//虚实线 return polygonView; } return nil; } - (BMKUserLocation *)userLocation { if (!_userLocation) { _userLocation = [[BMKUserLocation alloc] init]; } return _userLocation; } @end