扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
本篇文章给大家分享的是有关怎么在iOS中实现百度地图定位签到功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
我们注重客户提出的每个要求,我们充分考虑每一个细节,我们积极的做好成都做网站、成都网站建设服务,我们努力开拓更好的视野,通过不懈的努力,创新互联建站赢得了业内的良好声誉,这一切,也不断的激励着我们更好的服务客户。 主要业务:网站建设,网站制作,网站设计,重庆小程序开发公司,网站开发,技术开发实力,DIV+CSS,PHP及ASP,ASP.Net,SQL数据库的技术开发工程师。
一、在APPdelegate.m文件中引入:
#import#import
加入功能代码:
#pragma mark 百度地图设置 - (void)configBaiduMap { NSString *ak = @"xxxx"; BMKMapManager *mapManager = [[BMKMapManager alloc] init]; self.mapManager = mapManager; BOOL ret = [mapManager start:ak generalDelegate:nil]; [[BMKLocationAuth sharedInstance] checkPermisionWithKey:ak authDelegate:self]; if (!ret) { NSLog(@"manager start failed!"); } }
二、在用到地图定位功能的viewController中
#import#import //引入base相关所有的头文件 #import //引入地图功能所有的头文件
遵循协议
声明全局变量
@property (nonatomic, strong) BMKUserLocation *userLocation; //当前位置对象 @property (nonatomic, strong) BMKLocationManager *locationManager;/** locationManager*/ @property (nonatomic, strong) BMKMapView *mapView;/** 百度地图*/ //@property (nonatomic, strong) BMKPointAnnotation* annotation ;/** 标记*/ @property (nonatomic, strong) NSMutableArray *annotationArr;/** 标记数组*/ @property (nonatomic, strong) NSMutableArray *circleArr;/** 圆形数组*/
地图SDK文档中建议在以下代码中如此设置, 目的是控制内存
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [_mapView viewWillAppear]; _mapView.delegate = self; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [_mapView viewWillDisappear]; _mapView.delegate = nil; } - (void)dealloc { if (_mapView) { _mapView = nil; } }
初始化数组,这两个数组在接下来会用到
- (NSMutableArray *)annotationArr { if (!_annotationArr) { _annotationArr = [NSMutableArray array]; } return _annotationArr; } - (NSMutableArray *)circleArr { if (!_circleArr) { _circleArr = [NSMutableArray array]; } return _circleArr; }
添加地图view
#pragma mark 添加地图 - (void)addSignMapBgView { if (!self.mapBgView) { UIView *mapBgView = [UIView new]; self.mapBgView = mapBgView; mapBgView.backgroundColor = [CommUtls colorWithHexString:APP_BgColor]; [self addSubview:mapBgView]; [mapBgView makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.tipView.bottom); make.left.right.bottom.equalTo(0); }]; _mapView = [[BMKMapView alloc] initWithFrame:CGRectZero]; // _mapView.delegate = self; [_mapView setZoomLevel:21];//精确到5米 _mapView.showsUserLocation = YES;//显示定位图层 [mapBgView addSubview:_mapView]; [_mapView makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(0); }]; _mapView.userTrackingMode = BMKUserTrackingModeNone; } }
初始化地图定位:这里我用的是一次定位而没有选择持续定位。
#pragma mark 初始化locationManager - (void)initUserLocationManager { //因为mapView是在一个分离出来的view中创建的,所以在这里将signSetTypeView中的mapView赋给当前viewcontroller的mapView; self.mapView = self.mainView.signSetTypeView.mapView; self.mapView.delegate = self; // self.annotation = [[BMKPointAnnotation alloc] init]; // self.mapView是BMKMapView对象 //精度圈设置 BMKLocationViewDisplayParam *param = [[BMKLocationViewDisplayParam alloc] init]; //设置显示精度圈,默认YES param.isAccuracyCircleShow = YES; //精度圈 边框颜色 param.accuracyCircleStrokeColor = [UIColor colorWithRed:242/255.0 green:129/255.0 blue:126/255.0 alpha:1]; //精度圈 填充颜色 param.accuracyCircleFillColor = [UIColor colorWithRed:242/255.0 green:129/255.0 blue:126/255.0 alpha:0.3]; [self.mapView updateLocationViewWithParam:param]; self.userLocation = [[BMKUserLocation alloc] init]; //初始化实例 _locationManager = [[BMKLocationManager alloc] init]; //设置delegate _locationManager.delegate = self; //设置返回位置的坐标系类型 _locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL; //设置距离过滤参数 _locationManager.distanceFilter = kCLDistanceFilterNone; //设置预期精度参数 _locationManager.desiredAccuracy = kCLLocationAccuracyBest; //设置应用位置类型 _locationManager.activityType = CLActivityTypeAutomotiveNavigation; //设置是否自动停止位置更新 _locationManager.pausesLocationUpdatesAutomatically = NO; //设置是否允许后台定位 //_locationManager.allowsBackgroundLocationUpdates = YES; //设置位置获取超时时间 _locationManager.locationTimeout = 15; //设置获取地址信息超时时间 _locationManager.reGeocodeTimeout = 15; //请求一次定位 [self requestLocation]; }
请求定位,获取经纬度
#pragma mark 请求定位 - (void)requestLocation { [_locationManager requestLocationWithReGeocode:YES withNetworkState:YES completionBlock:^(BMKLocation * _Nullable location, BMKLocationNetworkState state, NSError * _Nullable error) { if (error) { NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription); } if (location) {//得到定位信息,添加annotation if (location.location) { NSLog(@"LOC = %@",location.location); } if (location.rgcData) { NSLog(@"rgc = %@",[location.rgcData description]); } if (!location) { return; } if (!self.userLocation) { self.userLocation = [[BMKUserLocation alloc] init]; } self.userLocation.location = location.location; [self.mapView updateLocationData:self.userLocation]; CLLocationCoordinate2D mycoordinate = location.location.coordinate; self.mapView.centerCoordinate = mycoordinate; //赋予初始值 self.viewModel.lat = [NSString stringWithFormat:@"%f", location.location.coordinate.latitude]; self.viewModel.lng = [NSString stringWithFormat:@"%f",location.location.coordinate.longitude]; self.viewModel.radius = @"50"; //打印经纬度 NSLog(@"didUpdateUserLocation lat %f,long %f",location.location.coordinate.latitude,location.location.coordinate.longitude); } NSLog(@"netstate = %d",state); }]; }
地图长按选点功能实现:
//长按地图选点 - (void)mapview:(BMKMapView *)mapView onLongClick:(CLLocationCoordinate2D)coordinate { if (self.annotationArr.count > 0) { [mapView removeAnnotations:self.annotationArr]; [self.annotationArr removeAllObjects]; BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc]init]; annotation.coordinate = coordinate; [self.annotationArr addObject:annotation]; [mapView addAnnotations:self.annotationArr]; } else { BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc]init]; annotation.coordinate = coordinate; [self.annotationArr addObject:annotation]; [mapView addAnnotations:self.annotationArr]; } //弹出半径选择框 [self showLocationSelectRadiusViewWithCoordinate:coordinate]; }
选点后弹出选择定位范围弹框
#pragma mark 弹出位置弹框 - (void)showLocationSelectRadiusViewWithCoordinate:(CLLocationCoordinate2D)coordinate { ExtraActLocationSignPopView *popView = [ExtraActLocationSignPopView new]; [popView show]; @weakify(self); [popView.locatioonSureSignal subscribeNext:^(NSString *x) { @strongify(self); self.viewModel.radius = x; CGFloat radius = [x floatValue]; [self circleWithCenterWithCoordinate2D:coordinate radius:radius]; }]; }
设置好定位点以及半径范围后绘制范围圈,开始的时候声明的circleArr在这里用来盛放添加的区域圆形,在添加新的圆圈的时候,将之前旧的移除,保证每次绘制的范围都是最新的,同理annotationArr也是这个功能,因为API有提供的- (void)addOverlays:(NSArray *)overlays;这个方法:/** *向地图窗口添加一组Overlay,需要实现BMKMapViewDelegate的-mapView:viewForOverlay:函数来生成标注对应的View *@param overlays 要添加的overlay数组 */
#pragma mark 添加区域圆形覆盖 - (void)circleWithCenterWithCoordinate2D:(CLLocationCoordinate2D )coor radius:(CGFloat)radius { NSLog(@"coordinate lat %f,long %f",coor.latitude,coor.longitude); //赋予点击选点值 self.viewModel.lat = [NSString stringWithFormat:@"%f", coor.latitude]; self.viewModel.lng = [NSString stringWithFormat:@"%f",coor.longitude]; if (self.circleArr.count > 0) { [_mapView removeOverlays:self.circleArr]; [self.circleArr removeAllObjects]; BMKCircle *circle = [BMKCircle circleWithCenterCoordinate:coor radius:radius]; [self.circleArr addObject:circle]; [_mapView addOverlays:self.circleArr]; } else { BMKCircle *circle = [BMKCircle circleWithCenterCoordinate:coor radius:radius]; [self.circleArr addObject:circle]; [_mapView addOverlays:self.circleArr]; } } #pragma mark 重绘overlay - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id)overlay{ if ([overlay isKindOfClass:[BMKCircle class]]){ BMKCircleView* circleView = [[BMKCircleView alloc] initWithOverlay:overlay]; circleView.fillColor = [UIColor colorWithRed:33/255.0 green:196/255.0 blue:206/255.0 alpha:0.3]; circleView.strokeColor = [UIColor colorWithRed:33/255.0 green:196/255.0 blue:206/255.0 alpha:1]; circleView.lineWidth = 1.0; return circleView; } return nil; }
以上就是怎么在iOS中实现百度地图定位签到功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流