// // MoreRailAddressViewController.m // LekangGuard // // Created by ecell on 2023/8/30. // #import "MoreRailAddressViewController.h" #import "AppDelegate.h" #import "RailSearchAddressVC.h" #import "SLMarqueeControl.h" @interface MoreRailAddressViewController () @property (nonatomic,strong) MAMapView * mapView; @property (nonatomic, strong) MACircle * circleView; @property (nonatomic, strong) MAPolygon *polygon; @property (nonatomic, strong) NSMutableArray *annotations; @property (nonatomic ,assign) CLLocationCoordinate2D points; @property (nonatomic ,strong) UIView *topView; @property (nonatomic ,weak) SLMarqueeControl *marqueeControl; @property (nonatomic ,strong) MAAnnotationView *UserannotationView; /// 底部View @property (nonatomic ,strong) UIView *bottomView; @property (nonatomic ,assign) NSInteger tags; @end @implementation MoreRailAddressViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.zx_navTitle = @"地址"; [self zx_setRightBtnWithText:GJText(@"保存") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) { if (self.annotations.count <= 0) { [UICommon MessageErrorText:@"电子围栏不能为空"]; return; } [self setbao]; }]; [MAMapView updatePrivacyShow:AMapPrivacyShowStatusDidShow privacyInfo:AMapPrivacyInfoStatusDidContain]; [MAMapView updatePrivacyAgree:AMapPrivacyAgreeStatusDidAgree]; [AMapSearchAPI updatePrivacyShow:(AMapPrivacyShowStatusDidShow) privacyInfo:(AMapPrivacyInfoStatusDidContain)]; [self.view addSubview:self.bottomView]; [self.view addSubview:self.mapView]; [self.view addSubview:self.topView]; [self.mapView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view).offset(iPhoneX_NavHeight); make.left.right.equalTo(self.view); make.bottom.equalTo(self.bottomView.mas_top); }]; UIButton *weiBtn = [UICommon ui_buttonSimple:CGRectZero font:Font_(14) normalColor:KKTextBlackColor normalText:@"" click:^(UIButton *btn) { if(self.mapView.userLocation.updating && self.mapView.userLocation.location) { [self.mapView setCenterCoordinate:self.mapView.userLocation.location.coordinate animated:YES]; CLLocation *curLocation = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.location.coordinate.latitude longitude:self.mapView.userLocation.location.coordinate.longitude]; [self getAddressWithLocation:curLocation]; } }]; [weiBtn setImage:ImageName_(@"map_button_gps") forState:0]; [self.view addSubview:weiBtn]; [weiBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.view.mas_right).inset(20); make.top.equalTo(self.topView.mas_bottom).offset(15); make.size.mas_equalTo(CGSizeMake(42, 42)); }]; //初始化 MAUserLocationRepresentation 对象 MAUserLocationRepresentation *r = [[MAUserLocationRepresentation alloc] init]; r.showsAccuracyRing = NO;///精度圈是否显示,默认YES [self.mapView updateUserLocationRepresentation:r]; if (self.isRedact == 2) [UICommon MessageUpload:@"地图加载中"]; kWeakSelf(self) AfterDispatch(1, ^{ if (self.isRedact == 2) { [UICommon HidenLoading]; if (self.railModel) { self.marqueeControl.marqueeLabel.text = self.railModel.address; for (NSDictionary *dic in self.railModel.boundaryCoordinates) { MAPointAnnotation *a1 = [[MAPointAnnotation alloc] init]; a1.coordinate = (CLLocationCoordinate2D){[dic[@"latitude"] doubleValue],[dic[@"longitude"] doubleValue]}; [self.annotations addObject:a1]; } CLLocationCoordinate2D points[self.annotations.count]; for (int i = 0; i < self.annotations.count; i++) { self.tags = i; MAPointAnnotation *point = self.annotations[i]; // [self.mapView removeAnnotation:point]; points[i] = CLLocationCoordinate2DMake(point.coordinate.latitude, point.coordinate.longitude); [self.mapView addAnnotation:point]; } self.polygon = [MAPolygon polygonWithCoordinates:points count:self.annotations.count]; [self.mapView addOverlay:self.polygon]; } } }); } //自定义标记点位置发生改变 - (void)mapView:(MAMapView *)mapView annotationView:(MAAnnotationView *)view didChangeDragState:(MAAnnotationViewDragState)newState fromOldState:(MAAnnotationViewDragState)oldState { if ([view.reuseIdentifier isEqualToString:@"annotationReuseIndetifier"]) { NSLog(@"x=%f,y=%f,tag=%ld",view.annotation.coordinate.latitude,view.annotation.coordinate.longitude,view.tag); // MAMetersBetweenMapPoints(<#MAMapPoint a#>, <#MAMapPoint b#>) // 定位点拖拽结束 if(newState == MAAnnotationViewDragStateEnding) { [self.mapView removeOverlay:self.polygon]; MAPointAnnotation *a1 = [[MAPointAnnotation alloc] init]; a1.coordinate = (CLLocationCoordinate2D){view.annotation.coordinate.latitude,view.annotation.coordinate.longitude}; [self.annotations replaceObjectAtIndex:view.tag withObject:a1]; CLLocationCoordinate2D points[self.annotations.count]; for (int i = 0; i < self.annotations.count; i++) { MAPointAnnotation *p = self.annotations[i]; points[i] = CLLocationCoordinate2DMake(p.coordinate.latitude, p.coordinate.longitude); } self.polygon = [MAPolygon polygonWithCoordinates:points count:self.annotations.count]; [self.mapView addOverlay:self.polygon]; } } } #pragma mark - MAMapViewDelegate //绘制区域图形的相关属性配置 可以是矩形 多边形 圆形 - (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id)overlay { if ([overlay isKindOfClass:[MAPolygon class]]) { MAPolygonRenderer *pol = [[MAPolygonRenderer alloc] initWithPolygon:overlay]; pol.lineWidth = 2.f; pol.lineJoinType = kMALineJoinRound; pol.strokeColor = KKMainColor; pol.fillColor = RGBA(59, 161, 245, .2); pol.lineDashType = kMALineDashTypeNone; pol.miterLimit = 5; return pol; } return nil; } /*! @brief 根据anntation生成对应的View @param mapView 地图View @param annotation 指定的标注 @return 生成的标注View */ - (MAAnnotationView*)mapView:(MAMapView *)mapView viewForAnnotation:(id )annotation { //定位蓝点 如果不在此判断 自身的定位点样式会被其他自定义的样式修改 if ([annotation isKindOfClass:[MAUserLocation class]]) { static NSString *customReuseIndetifier = @"customReuseIndetifier"; self.UserannotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:customReuseIndetifier]; if (self.UserannotationView == nil) { self.UserannotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:customReuseIndetifier]; } self.UserannotationView.canShowCallout = NO;//允许交互点击 self.UserannotationView.draggable = NO; self.UserannotationView.image = ImageName_(@"icon_home_page_orientation");//设置大头针视图的图片 return self.UserannotationView; } if ([annotation isKindOfClass:[MAPointAnnotation class]]) { static NSString *reuseIndetifier = @"annotationReuseIndetifier"; MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier]; if (annotationView == nil) { annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier]; } annotationView.tag = self.tags; annotationView.image = [UIImage imageNamed:@"map_weilan_select"]; annotationView.canShowCallout = NO; annotationView.draggable = YES; //设置中心点偏移,使得标注底部中间点成为经纬度对应点 annotationView.centerOffset = CGPointMake(0, 0); return annotationView; } return nil; } //点击屏幕获取经纬度 (手动获取模拟数据使用) - (void)mapView:(MAMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate { NSLog(@"%f ---- %f",coordinate.latitude,coordinate.longitude); MAPointAnnotation *point = [[MAPointAnnotation alloc] init]; if (self.annotations.count > 2) return; point.coordinate = (CLLocationCoordinate2D){coordinate.latitude,coordinate.longitude}; [self.annotations addObject:point]; if(self.annotations.count == 3) [self addInteractionPoint]; CLLocationCoordinate2D points[self.annotations.count]; for (int i = 0; i < self.annotations.count; i++) { self.tags = i; point = self.annotations[i]; [self.mapView removeAnnotation:point]; points[i] = CLLocationCoordinate2DMake(point.coordinate.latitude, point.coordinate.longitude); [_mapView addAnnotation:point]; } self.polygon = [MAPolygon polygonWithCoordinates:points count:self.annotations.count]; [self.mapView addOverlay:self.polygon]; } /// 点击地图形成三个点,强制在两点之间添加单个标记 - (void)addInteractionPoint { MAPointAnnotation *p = self.annotations[0]; MAPointAnnotation *p1 = self.annotations[1]; MAPointAnnotation *p2 = self.annotations[2]; double aa = (p.coordinate.latitude+p1.coordinate.latitude)/2; double bb = (p.coordinate.longitude+p1.coordinate.longitude)/2; MAPointAnnotation *a = [[MAPointAnnotation alloc] init]; a.coordinate = (CLLocationCoordinate2D){aa,bb}; double aaa = (p1.coordinate.latitude+p2.coordinate.latitude)/2; double bbb = (p1.coordinate.longitude+p2.coordinate.longitude)/2; MAPointAnnotation *as = [[MAPointAnnotation alloc] init]; as.coordinate = (CLLocationCoordinate2D){aaa,bbb}; double aaaa = (p2.coordinate.latitude+p.coordinate.latitude)/2; double bbbb = (p2.coordinate.longitude+p.coordinate.longitude)/2; MAPointAnnotation *ass = [[MAPointAnnotation alloc] init]; ass.coordinate = (CLLocationCoordinate2D){aaaa,bbbb}; [self.annotations insertObject:a atIndex:1]; [self.annotations insertObject:as atIndex:3]; [self.annotations addObject:ass]; } /** * @brief 长按地图,返回经纬度 * @param mapView 地图View * @param coordinate 经纬度 */ - (void)mapView:(MAMapView *)mapView didLongPressedAtCoordinate:(CLLocationCoordinate2D)coordinate; { CLLocation *curLocation = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude]; [self getAddressWithLocation:curLocation]; } - (void)mapInitComplete:(MAMapView *)mapView { if (self.isRedact == 1) [self getAddressWithLocation:mapView.userLocation.location]; } #pragma mark 根据坐标取得地名 -(void)getAddressWithLocation:(CLLocation *)location { //反地理编码 CLGeocoder *geocoder = [[CLGeocoder alloc]init]; kWeakSelf(self) [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { CLPlacemark *placemark = [placemarks firstObject]; NSLog(@"详细信息:%@",placemark.addressDictionary); NSString *name; // if (placemark.addressDictionary) // { // name = placemark.addressDictionary[@"FormattedAddressLines"][0]; // } // else // NSLog(@"%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@",placemark.name,placemark.thoroughfare,placemark.subThoroughfare,placemark.locality,placemark.subLocality,placemark.administrativeArea,placemark.subAdministrativeArea,placemark.postalCode); if(placemark.subLocality) { name = [NSString stringWithFormat:@"%@%@%@%@%@", placemark.administrativeArea,placemark.locality,placemark.thoroughfare,placemark.subLocality,placemark.name]; }else{ name = [NSString stringWithFormat:@"%@ %@", placemark.locality,placemark.name]; } if([name rangeOfString:@"(null)"].location != NSNotFound) name = [name stringByReplacingOccurrencesOfString:@"(null)" withString:@""]; weakself.railModel.address = name; //在导航栏上赋值地址 weakself.marqueeControl.marqueeLabel.text = name; // weakself.railModel.lon = F(@"%f", location.coordinate.longitude); // weakself.railModel.lat = F(@"%f", location.coordinate.latitude); //[weakself.myTableView reloadData]; }]; } - (UIView *)bottomView { if (!_bottomView) { _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - (iPhoneX_TabbarHeight+20), SCREEN_WIDTH, iPhoneX_TabbarHeight+20)]; _bottomView.backgroundColor = KKWhiteColorColor; UIButton *deleteBtn = [UICommon ui_buttonSimple:CGRectZero font:Font_(0) normalColor:KKMainColor normalText:@"" click:^(id x) { if (self.annotations.count <= 0) return; [self.mapView removeAnnotations:self.mapView.annotations]; [self.mapView removeOverlays:self.mapView.overlays]; [self.annotations removeAllObjects]; }]; deleteBtn.backgroundColor = KKMainColor; [deleteBtn setImage:ImageName_(@"icon_delect_1") forState:0]; [_bottomView addSubview:deleteBtn]; [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(_bottomView.mas_right); make.top.equalTo(_bottomView.mas_top); make.bottom.equalTo(_bottomView.mas_bottom); make.width.mas_equalTo(Adapted(65)); }]; UILabel *titleLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentLeft font:FontADA_(12) textColor:UIColor.redColor text:@"" Radius:0]; titleLabel.text = @"1.绘制方法:请顺时针或逆时针依次标记电子围栏,标记点不可交叉,否则电子围栏将会产生错误;\n2.长按电子围栏标记点2秒可对电子围栏范围进行编辑."; [_bottomView addSubview:titleLabel]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(_bottomView).offset(15); make.right.equalTo(deleteBtn.mas_left).inset(5); make.top.equalTo(_bottomView).offset(10); }]; } return _bottomView; } - (UIView *)topView { if (!_topView) { kWeakSelf(self) _topView = [UICommon ui_view:CGRectZero backgroundColor:KKWhiteColorColor cornerRadius:Adapted(12.5) borderWidth:0 borderColor:KKWhiteColorColor]; _topView.frame = CGRectMake(10, iPhoneX_NavHeight+6, SCREEN_WIDTH-20, Adapted(25)); UIImageView *locationTypeImageView = [UICommon ui_imageView:CGRectZero fileName:@"home_icon_gps"]; [_topView addSubview:locationTypeImageView]; [locationTypeImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(_topView).offset(13); make.centerY.equalTo(_topView); make.size.mas_equalTo(CGSizeMake(15, 15)); }]; /// 位置跑马灯 SLMarqueeControl *control = [[SLMarqueeControl alloc] initWithFrame:CGRectMake(38, Adapted(5), SCREEN_WIDTH-20-38-15, 15)]; control.backgroundColor = KKClearColor; control.marqueeLabel.text = @"长按地图选择位置"; self.marqueeControl = control; [_topView addSubview:control]; UIButton *pusBtn = [UICommon ui_buttonSimple:CGRectZero font:Font_(0) normalColor:KKMainColor normalText:@"" click:^(id x) { RailSearchAddressVC *vc = [RailSearchAddressVC new]; vc.selectAddressCallBack = ^(NSString *searchResult, AMapGeoPoint *point) { // weakself.curLocation = [[CLLocation alloc] initWithLatitude:point.latitude // longitude:point.longitude]; // // [weakself updateRegionWithAddress:YES]; weakself.marqueeControl.marqueeLabel.text = searchResult; weakself.railModel.address = searchResult; }; [[UICommon currentVC].navigationController pushViewController:vc animated:YES]; }]; [_topView addSubview:pusBtn]; [pusBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.left.equalTo(_topView); make.top.equalTo(_topView); make.bottom.equalTo(_topView.mas_bottom); }]; } return _topView; } - (MAMapView *)mapView { if (!_mapView) { _mapView = [[MAMapView alloc] initWithFrame:CGRectZero]; _mapView.userTrackingMode = MAUserTrackingModeFollow; ///如果您需要进入地图就显示定位小蓝点,则需要下面两行代码 _mapView.showsUserLocation = YES; _mapView.showsScale = NO; _mapView.zoomLevel = 18; _mapView.showTraffic = YES; _mapView.showsCompass = NO; _mapView.delegate = self; } return _mapView; } - (NSMutableArray *)annotations { if (!_annotations) _annotations = [NSMutableArray new]; return _annotations; } /// 保存 - (void)setbao { NSMutableArray *arr = [NSMutableArray new]; for (MAPointAnnotation *p in self.annotations) { NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [dic setValue:@(p.coordinate.latitude) forKey:@"latitude"]; [dic setValue:@(p.coordinate.longitude) forKey:@"longitude"]; [arr addObject:dic]; } self.railModel.boundaryCoordinates = arr; self.railModel.type = 2; self.isAddressModel(self.railModel); [self.navigationController popViewControllerAnimated:YES]; } /* #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. } */ @end