// // MapReviseViewController.m // tongxin // // Created by ecell on 2023/5/22. // Copyright © 2023 xTT. All rights reserved. // #import "MapReviseViewController.h" #import #import #import @interface MapReviseViewController () //定位 @property (nonatomic, strong) AMapLocationManager *locationManager; //地图 @property (nonatomic, strong) MAMapView *mapView; //大头针 @property (nonatomic, strong) MAPointAnnotation *annotation; //逆地理编码 @property (nonatomic, strong) AMapReGeocodeSearchRequest *regeo; //逆地理编码使用的 @property (nonatomic, strong) AMapSearchAPI *search; /// 定位Btn @property (nonatomic, weak) UIButton *locationBtn; @property (nonatomic ,strong) UIView *bottomView; @property (nonatomic ,weak) UILabel *addressLabel; /// @property (nonatomic ,strong) UIView *topView; /// 经度 @property (nonatomic ,strong) NSString *lon; /// 纬度 @property (nonatomic ,strong) NSString *lat; @end @implementation MapReviseViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.zx_navTitle = @"位置纠偏"; [self.view addSubview:self.bottomView]; [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view); make.bottom.equalTo(self.view.mas_bottom); make.height.mas_equalTo(Adapted(140)); }]; //地图 _mapView = [[MAMapView alloc]initWithFrame:CGRectMake(0, iPhoneX_NavHeight, ScreenWidth, ScreenHeight-iPhoneX_NavHeight-Adapted(140))]; [self.view addSubview:_mapView]; _mapView.zoomLevel = 15; _mapView.showsUserLocation = YES; _mapView.showsCompass = NO; _mapView.delegate = self; _mapView.userTrackingMode = MAUserTrackingModeFollow; _mapView.showsScale = NO; [self.view addSubview:self.mapView]; UIImageView *centerAnnotationView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"定位"]]; centerAnnotationView.center = CGPointMake(self.mapView.center.x, self.mapView.center.y - MJStatusAndNavHeight - CGRectGetHeight(centerAnnotationView.bounds) / 2); [self.mapView addSubview:centerAnnotationView]; UIButton *locationBtn = [UICommon ui_buttonSimple:CGRectZero font:Font_(0) normalColor:KKWhiteColorColor normalText:@"" click:^(id x) { [self actionLocation]; }]; [locationBtn setImage:ImageName_(@"icon_chuild_positioning") forState:0]; self.locationBtn = locationBtn; [self.view addSubview:locationBtn]; [locationBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view).offset(iPhoneX_NavHeight+10); make.right.equalTo(self.view.mas_right).inset(10); make.size.mas_equalTo(CGSizeMake(Adapted(35), Adapted(35))); }]; [self.view addSubview:self.topView]; [self.topView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view).offset(iPhoneX_NavHeight+10); make.centerX.equalTo(self.view); make.size.mas_equalTo(CGSizeMake(270, 32)); }]; //定位 [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) { if (error) { return ; } //添加大头针 self.annotation = [[MAPointAnnotation alloc]init]; self.annotation.coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude); [self.mapView addAnnotation:self.annotation]; [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) animated:YES]; //让地图在缩放过程中移到当前位置试图 [self.mapView setZoomLevel:16.1 animated:YES]; }]; } - (void)actionLocation { if (self.mapView.userTrackingMode == MAUserTrackingModeFollow) { [self.mapView setUserTrackingMode:MAUserTrackingModeNone animated:YES]; } else { [self.mapView setCenterCoordinate:self.mapView.userLocation.coordinate animated:YES]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ // 因为下面这句的动画有bug,所以要延迟0.5s执行,动画由上一句产生 [self.mapView setUserTrackingMode:MAUserTrackingModeFollow animated:YES]; }); } } - (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation { if ([annotation isKindOfClass:[MAPointAnnotation class]]) { static NSString *reuseIdetifier = @"annotationReuseIndetifier"; MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdetifier]; if (annotationView == nil) { annotationView = [[MAAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:reuseIdetifier]; } //放一张大头针图片即可 annotationView.image = [UIImage imageNamed:@"定位点"]; //annotationView.centerOffset = CGPointMake(0, -18); return annotationView; } return nil; } #pragma mark - 让大头针不跟着地图滑动,时时显示在地图最中间 - (void)mapViewRegionChanged:(MAMapView *)mapView { _annotation.coordinate = mapView.centerCoordinate; } #pragma mark - 滑动地图结束修改当前位置 - (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated { self.regeo.location = [AMapGeoPoint locationWithLatitude:mapView.centerCoordinate.latitude longitude:mapView.centerCoordinate.longitude]; [self.search AMapReGoecodeSearch:self.regeo]; } - (AMapLocationManager *)locationManager { if (!_locationManager) { _locationManager = [[AMapLocationManager alloc]init]; [_locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters]; _locationManager.locationTimeout = 2; _locationManager.reGeocodeTimeout = 2; } return _locationManager; } - (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response { if (response.regeocode != nil) { //AMapReGeocode *reocode = response.regeocode; //地图标注的点的位置信息全在reoceode里面了 //[_locationManager stopUpdatingLocation]; [self.mapView setShowsUserLocation:NO]; NSLog(@"%@",response.regeocode.formattedAddress); self.addressLabel.text = response.regeocode.formattedAddress; _annotation.coordinate = CLLocationCoordinate2DMake(request.location.latitude, request.location.longitude); self.lon = [NSString stringWithFormat:@"%f",request.location.longitude]; self.lat = [NSString stringWithFormat:@"%f",request.location.latitude]; } } - (AMapReGeocodeSearchRequest *)regeo { if (!_regeo) { _regeo = [[AMapReGeocodeSearchRequest alloc]init]; _regeo.requireExtension = YES; } return _regeo; } - (AMapSearchAPI *)search { if (!_search) { _search = [[AMapSearchAPI alloc]init]; _search.delegate = self; } return _search; } - (UIView *)bottomView { if(!_bottomView) { _bottomView = [UIView new]; _bottomView.backgroundColor = KKWhiteColorColor; UIButton *btns = [UICommon ui_buttonSimple:CGRectZero font:Font_(16) normalColor:KKWhiteColorColor normalText:@"位置纠偏" click:^(id x) { [self setLocationRectify]; }]; btns.layer.cornerRadius = 22; btns.layer.masksToBounds = YES; btns.backgroundColor = mainColor; [_bottomView addSubview:btns]; CGFloat bb = iPhoneX_TabbarSafeBottomMargin > 0 ? iPhoneX_TabbarSafeBottomMargin : 34; [btns mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(_bottomView).inset(20); make.bottom.equalTo(_bottomView.mas_bottom).inset(bb); make.height.mas_equalTo(44); }]; UIImageView *iconImg = [UICommon ui_imageView:CGRectZero fileName:@"icon_positioning"]; [_bottomView addSubview:iconImg]; [iconImg mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(_bottomView).offset(20); make.top.equalTo(_bottomView).offset(15); make.size.mas_equalTo(iconImg.image.size); }]; UILabel *addressLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentLeft font:Font_(15) textColor:KKBlack20 text:@"" Radius:0]; self.addressLabel = addressLabel; [_bottomView addSubview:addressLabel]; [addressLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(iconImg.mas_right).offset(2); make.right.equalTo(_bottomView.mas_right).inset(20); make.centerY.equalTo(iconImg); }]; } return _bottomView; } - (UIView *)topView { if(!_topView) { _topView = [UICommon ui_view:CGRectZero backgroundColor:RGBA(0, 0, 0, .4) cornerRadius:16 borderWidth:0 borderColor:KKClearColor]; UIImageView *iconImg = [UICommon ui_imageView:CGRectZero fileName:@"comm_ horn"]; [_topView addSubview:iconImg]; [iconImg mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(_topView).offset(5); make.centerY.equalTo(_topView); make.size.mas_equalTo(CGSizeMake(20, 20)); }]; UILabel *titleLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentLeft font:Font_(12) textColor:KKWhiteColorColor text:@"拖动地图选择设备正确位置,纠正设备位置" Radius:0]; [_topView addSubview:titleLabel]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(iconImg.mas_right).offset(5); make.right.equalTo(_topView.mas_right).inset(5); make.centerY.equalTo(iconImg); }]; } return _topView; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.locationManager stopUpdatingLocation]; } - (void)setLocationRectify { [UICommon MessageUpload:@"正在加载"]; NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; [parameters setValue:self.lon forKey:@"lon"]; [parameters setValue:self.lat forKey:@"lat"]; [parameters setValue:self.addressLabel.text forKey:@"address"]; [cUser.cDevice setLocationRectify:parameters success:^(id responseObject) { [UICommon HidenLoading]; [UICommon MessageSuccessText:@"位置纠偏成功" isImg:YES]; } failure:^(id faiObject) { [UICommon HidenLoading]; }]; } /* #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