You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
473 lines
17 KiB
473 lines
17 KiB
// |
|
// RailAddressViewController.m |
|
// LekangGuard |
|
// |
|
// Created by ecell on 2022/12/13. |
|
// |
|
|
|
#import "RailAddressViewController.h" |
|
#import "AppDelegate.h" |
|
#import "RailSearchAddressVC.h" |
|
|
|
@interface RailAddressViewController ()<UIGestureRecognizerDelegate,UITextFieldDelegate,CLLocationManagerDelegate,AMapSearchDelegate,UISearchBarDelegate,MKMapViewDelegate> |
|
|
|
@property (nonatomic, strong) CLLocation *curLocation; |
|
|
|
@property (nonatomic, strong) MKCircle *mapCircle; |
|
|
|
@property (nonatomic, strong) CLLocationManager *locationManager; |
|
|
|
@property (nonatomic, strong) AMapSearchAPI *search; |
|
|
|
@property (nonatomic, strong) AMapInputTipsSearchRequest *currentRequest; |
|
|
|
@property(strong ,nonatomic) MKMapView *mapView; |
|
|
|
/// 搜索栏 |
|
@property (strong, nonatomic) UISearchBar *searchBar; |
|
|
|
/// 滑动块 |
|
@property (strong, nonatomic) UISlider *sliderView; |
|
|
|
///电子围栏的距离范围 |
|
@property (weak, nonatomic) UILabel *railRangeLabel; |
|
|
|
/// 底部View |
|
@property (nonatomic ,strong) UIView *bottomView; |
|
|
|
@end |
|
|
|
@implementation RailAddressViewController |
|
|
|
- (void)viewDidLoad { |
|
[super viewDidLoad]; |
|
// Do any additional setup after loading the view. |
|
self.zx_navTitle = GJText(@"地址"); |
|
[self zx_setRightBtnWithText:GJText(@"保存") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) { |
|
if(self.searchBar.text.length == 0) |
|
{ |
|
[UICommon MessageErrorText:@"请长按地图选取地址"]; |
|
return; |
|
} |
|
self.isAddressModel(self.railModel); |
|
[self.navigationController popViewControllerAnimated:YES]; |
|
}]; |
|
|
|
[self.view addSubview:self.bottomView]; |
|
[self.view addSubview:self.searchBar]; |
|
[self.view addSubview:self.mapView]; |
|
[self.mapView mas_makeConstraints:^(MASConstraintMaker *make) { |
|
make.top.equalTo(self.searchBar.mas_bottom); |
|
make.left.right.equalTo(self.view); |
|
make.bottom.equalTo(self.bottomView.mas_top); |
|
}]; |
|
|
|
if (self.isRedact == 2) |
|
{ |
|
self.sliderView.value = self.railModel.radius; |
|
self.railRangeLabel.text = F(@"%ld米", self.railModel.radius); |
|
self.searchBar.text = self.railModel.address; |
|
} |
|
else |
|
{ |
|
self.railModel = [[SafetyRailModel alloc] init]; |
|
self.railModel.radius = 500; |
|
} |
|
|
|
//实例化长按手势监听 |
|
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] init]; |
|
[longPress addTarget:self action:@selector(handleMapViewLongPressed:)]; |
|
//代理 |
|
longPress.delegate = self; |
|
longPress.minimumPressDuration = 1.0; |
|
longPress.allowableMovement = 50.0; |
|
|
|
//将长按手势添加到需要实现长按操作的视图里 |
|
[self.mapView addGestureRecognizer:longPress]; |
|
|
|
//定位一次自身的位置 |
|
//初始化管理器 |
|
self.locationManager = [[CLLocationManager alloc] init]; |
|
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; |
|
self.locationManager.delegate = self; |
|
[self.locationManager startUpdatingLocation]; |
|
//检查是否授权 |
|
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { |
|
[self.locationManager requestWhenInUseAuthorization]; |
|
} |
|
|
|
} |
|
-(void)sliderViewChangeAction:(UISlider*)slider |
|
{ |
|
int radiusInt = [NSNumber numberWithFloat:slider.value].intValue; |
|
self.railRangeLabel.text = [NSString stringWithFormat:@"%d米",radiusInt]; |
|
} |
|
|
|
-(void)sliderViewChangeDidAction:(UISlider*)slider |
|
{ |
|
[self.mapView removeOverlays:self.mapView.overlays]; |
|
NSInteger radiusInt = [NSNumber numberWithFloat:slider.value].integerValue; |
|
[slider setValue:radiusInt animated:YES]; |
|
MKCircle *mapCircle = [MKCircle circleWithCenterCoordinate:self.curLocation.coordinate |
|
radius:radiusInt]; |
|
self.railModel.radius = radiusInt; |
|
self.railRangeLabel.text = [NSString stringWithFormat:@"%ld米",radiusInt]; |
|
[self.mapView addOverlay:mapCircle]; |
|
} |
|
|
|
- (void)viewWillAppear:(BOOL)animated |
|
{ |
|
[super viewWillAppear:animated]; |
|
[self updateRegionWithAddress:NO]; |
|
|
|
} |
|
|
|
//MARK: 封闭 侧滑手势 |
|
-(void)viewDidAppear:(BOOL)animated |
|
{ |
|
[super viewDidAppear:animated]; |
|
if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) { |
|
self.navigationController.interactivePopGestureRecognizer.enabled = NO; |
|
} |
|
} |
|
-(void)viewWillDisappear:(BOOL)animated |
|
{ |
|
[super viewWillDisappear:animated]; |
|
if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) { |
|
self.navigationController.interactivePopGestureRecognizer.enabled = YES; |
|
}; |
|
} |
|
|
|
|
|
|
|
//长按事件的实现方法 |
|
- (void)handleMapViewLongPressed:(UILongPressGestureRecognizer *)gestureRecognizer { |
|
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { |
|
NSLog(@"UIGestureRecognizerStateBegan"); |
|
CGPoint pt = [gestureRecognizer locationInView:_mapView]; |
|
CLLocationCoordinate2D coordinate = [_mapView convertPoint:pt toCoordinateFromView:_mapView]; |
|
self.curLocation = [[CLLocation alloc] initWithLatitude:coordinate.latitude |
|
longitude:coordinate.longitude]; |
|
//23.123625574141336 113.39765354990959 |
|
// self.curLocation = [[CLLocation alloc] initWithLatitude:23.123625574141336 |
|
// longitude:113.39765354990959]; |
|
// _watchLocation = CLLocationCoordinate2DMake(latitude, longitude); |
|
[self updateRegionWithAddress:YES]; |
|
} else if (gestureRecognizer.state == UIGestureRecognizerStateChanged) { |
|
NSLog(@"UIGestureRecognizerStateChanged"); |
|
} else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { |
|
NSLog(@"UIGestureRecognizerStateEnded"); |
|
} |
|
} |
|
|
|
/** |
|
更新区域的 |
|
|
|
@param getAddress 是否更新 搜索栏的 地址信息 |
|
*/ |
|
- (void)updateRegionWithAddress:(BOOL)getAddress; |
|
{ |
|
if (self.curLocation) |
|
{ |
|
[self.mapView removeOverlays:self.mapView.overlays]; |
|
[self.mapView removeAnnotations:self.mapView.annotations]; |
|
|
|
NSInteger radius = 500; |
|
if (self.isRedact != 2) |
|
radius = self.railModel.radius; |
|
|
|
MKCircle *mapCircle = [MKCircle circleWithCenterCoordinate:self.curLocation.coordinate |
|
radius:radius]; |
|
[self.mapView addOverlay:mapCircle]; |
|
|
|
MKPointAnnotation *locateAnnotation = [[MKPointAnnotation alloc] init]; |
|
locateAnnotation.coordinate = self.curLocation.coordinate; |
|
@try { |
|
[self.mapView setRegion:MKCoordinateRegionMakeWithDistance(self.curLocation.coordinate, radius * 3, radius * 3) |
|
animated:YES]; |
|
} @catch (NSException *exception) { |
|
NSLog(@"radius :%ld coordinate: lon:%f lat: %f",(long)radius ,self.curLocation.coordinate.longitude,self.curLocation.coordinate.latitude); |
|
|
|
} @finally { |
|
|
|
} |
|
[self.mapView addAnnotation:locateAnnotation]; |
|
if (getAddress) { |
|
[self getAddressWithLocation:self.curLocation]; |
|
} |
|
} |
|
} |
|
|
|
#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.subLocality){ |
|
// name = [NSString stringWithFormat:@"%@ %@", |
|
// placemark.subLocality,placemark.name]; |
|
// }else{ |
|
// name = [NSString stringWithFormat:@"%@ %@", |
|
// placemark.locality,placemark.name]; |
|
// } |
|
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.searchBar.text = name; |
|
weakself.railModel.lon = F(@"%f", location.coordinate.longitude); |
|
weakself.railModel.lat = F(@"%f", location.coordinate.latitude); |
|
//[weakself.myTableView reloadData]; |
|
}]; |
|
} |
|
|
|
|
|
#pragma mark MKMapViewDelegate |
|
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay |
|
{ |
|
NSLog(@"重新渲染 rendererForOverlay"); |
|
if ([overlay isKindOfClass:[MKCircle class]]) { |
|
MKCircleRenderer *circleRenderer = [[MKCircleRenderer alloc] initWithCircle:overlay]; |
|
circleRenderer.lineWidth = 1.0f; |
|
circleRenderer.strokeColor = KKMainColor; |
|
circleRenderer.fillColor = [KKMainColor colorWithAlphaComponent:0.3]; |
|
return circleRenderer; |
|
} |
|
|
|
return nil; |
|
} |
|
|
|
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation |
|
{ |
|
if ([annotation isKindOfClass:[MKPointAnnotation class]]) { |
|
static NSString *customReuseIndetifier = @"customReuseIndetifier"; |
|
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:customReuseIndetifier]; |
|
|
|
if (annotationView == nil) { |
|
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:customReuseIndetifier]; |
|
// must set to NO, so we can show the custom callout view. |
|
annotationView.canShowCallout = NO;//允许交互点击 |
|
annotationView.draggable = NO; |
|
} |
|
annotationView.image = ImageName_(@"icon_home_page_orientation");//设置大头针视图的图片 |
|
|
|
annotationView.selected = YES; |
|
return annotationView; |
|
} |
|
|
|
return nil; |
|
} |
|
|
|
|
|
|
|
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField |
|
{ |
|
if (self.railModel.radius < 300 || self.railModel.radius > 1000) |
|
[UICommon MessageErrorText:@"安全范围为300~1000米"]; |
|
else |
|
[self updateRegionWithAddress:YES]; |
|
|
|
return YES; |
|
} |
|
|
|
//MARK: 定位管理器的 代理 |
|
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status |
|
{ |
|
switch (status) |
|
{ |
|
case kCLAuthorizationStatusNotDetermined: |
|
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) |
|
{ |
|
[self.locationManager requestWhenInUseAuthorization]; |
|
} |
|
break; |
|
default: |
|
break; |
|
} |
|
} |
|
//MARK : 初始化 用户位置 |
|
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations |
|
{ |
|
CLLocation *userLocation = [locations lastObject]; |
|
//[User currentUser].curLocation = userLocation; //设置用户信息 |
|
[manager stopUpdatingLocation]; |
|
|
|
//如果没有 电子围栏 curLocation 为用户位置 |
|
if(self.isRedact == 1) |
|
{ |
|
if(!APIManager.sharedManager.laloModel) |
|
{ |
|
self.curLocation = [userLocation locationMarsFromEarth]; //转换用户位置为火星坐标 |
|
} |
|
// [self updateRegionWithAddress:NO]; |
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//MARK: -----搜索框 UISearchBarDelegate ----- |
|
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar |
|
{ |
|
NSLog(@"searchBarCancelButtonClicked"); |
|
} |
|
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar |
|
{ |
|
kWeakSelf(self) |
|
RailSearchAddressVC *vc = [RailSearchAddressVC new]; |
|
vc.selectAddressCallBack = ^(NSString *searchResult, AMapGeoPoint *point) { |
|
weakself.curLocation = [[CLLocation alloc] initWithLatitude:point.latitude |
|
longitude:point.longitude]; |
|
|
|
[weakself updateRegionWithAddress:YES]; |
|
weakself.searchBar.text = searchResult; |
|
}; |
|
[self.navigationController pushViewController:vc animated:YES]; |
|
return NO; |
|
} |
|
|
|
|
|
|
|
- (void)didReceiveMemoryWarning { |
|
[super didReceiveMemoryWarning]; |
|
|
|
switch (self.mapView.mapType) { |
|
case MKMapTypeHybrid: |
|
{ |
|
self.mapView.mapType = MKMapTypeStandard; |
|
} |
|
|
|
break; |
|
case MKMapTypeStandard: |
|
{ |
|
self.mapView.mapType = MKMapTypeHybrid; |
|
} |
|
|
|
break; |
|
default: |
|
break; |
|
} |
|
self.mapView.mapType = MKMapTypeStandard; |
|
// Dispose of any resources that can be recreated. |
|
} |
|
|
|
- (CLLocation *)curLocation |
|
{ |
|
if (!_curLocation) |
|
{ |
|
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate; |
|
if (self.railModel && self.isRedact == 2) |
|
{ |
|
_curLocation = [[CLLocation alloc] initWithLatitude:[self.railModel.lat doubleValue] |
|
longitude:[self.railModel.lon doubleValue]]; |
|
} |
|
else if(APIManager.sharedManager.laloModel) |
|
{ |
|
_curLocation = [[CLLocation alloc] initWithLatitude:APIManager.sharedManager.laloModel.latitude.doubleValue |
|
longitude:APIManager.sharedManager.laloModel.longitude.doubleValue]; |
|
} |
|
else if(app.curLocation) |
|
{ |
|
_curLocation = [[CLLocation alloc] initWithLatitude:[app.curLocation locationMarsFromEarth].coordinate.latitude |
|
longitude:[app.curLocation locationMarsFromEarth].coordinate.longitude]; |
|
} |
|
} |
|
return _curLocation; |
|
} |
|
|
|
- (MKMapView *)mapView |
|
{ |
|
if (!_mapView) |
|
{ |
|
_mapView = [[MKMapView alloc] initWithFrame:CGRectZero]; |
|
_mapView.delegate = self; |
|
// _mapView.showsUserLocation = YES; |
|
// _mapView.userTrackingMode = MkUserTrackingModeFollow; |
|
MKCoordinateSpan span = MKCoordinateSpanMake(0.021251, 0.016093); |
|
CLLocationCoordinate2D center = self.mapView.region.center; |
|
[_mapView setRegion:MKCoordinateRegionMake(center, span) animated:YES]; |
|
} |
|
return _mapView; |
|
} |
|
|
|
- (UISearchBar *)searchBar |
|
{ |
|
if (!_searchBar) |
|
{ |
|
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, iPhoneX_NavHeight, SCREEN_WIDTH, Adapted(50))]; |
|
_searchBar.placeholder = GJText(@"搜索地址"); |
|
_searchBar.delegate = self; |
|
} |
|
return _searchBar; |
|
} |
|
|
|
|
|
- (UIView *)bottomView |
|
{ |
|
if (!_bottomView) |
|
{ |
|
_bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - (iPhoneX_TabbarHeight+20), SCREEN_WIDTH, iPhoneX_TabbarHeight+20)]; |
|
_bottomView.backgroundColor = KKWhiteColorColor; |
|
|
|
UILabel *tiLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentCenter font:FontADA_(13) textColor:KKTextBlackColor text:GJText(@"长按地图选择地址,滑动滑块设置安全范围") Radius:0]; |
|
[_bottomView addSubview:tiLabel]; |
|
[tiLabel mas_makeConstraints:^(MASConstraintMaker *make) { |
|
make.left.equalTo(_bottomView).offset(20); |
|
make.top.equalTo(_bottomView).offset(10); |
|
}]; |
|
|
|
UILabel *railRangeLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentCenter font:FontADA_(13) textColor:KKTextBlackColor text:GJText(@"500米") Radius:0]; |
|
self.railRangeLabel = railRangeLabel; |
|
[_bottomView addSubview:railRangeLabel]; |
|
[railRangeLabel mas_makeConstraints:^(MASConstraintMaker *make) { |
|
make.right.equalTo(_bottomView.mas_right).inset(20); |
|
make.top.equalTo(tiLabel.mas_bottom).offset(15); |
|
}]; |
|
|
|
|
|
self.sliderView = [[UISlider alloc] init]; |
|
self.sliderView.maximumValue = 1000; |
|
self.sliderView.minimumValue = 300; |
|
self.sliderView.value = 500; |
|
self.sliderView.minimumTrackTintColor = KKMainColor; |
|
[self.sliderView addTarget:self action:@selector(sliderViewChangeAction:) forControlEvents:UIControlEventValueChanged]; |
|
[self.sliderView addTarget:self action:@selector(sliderViewChangeDidAction:) forControlEvents:UIControlEventTouchUpInside]; |
|
[_bottomView addSubview:self.sliderView]; |
|
[self.sliderView mas_makeConstraints:^(MASConstraintMaker *make) { |
|
make.left.equalTo(_bottomView).offset(20); |
|
make.centerY.equalTo(railRangeLabel); |
|
make.right.equalTo(railRangeLabel.mas_left).inset(5); |
|
make.height.mas_equalTo(20); |
|
}]; |
|
} |
|
return _bottomView; |
|
} |
|
|
|
|
|
/* |
|
#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
|
|
|