iOS 百度地图定位签到实现

写在前面:
项目需求用到这个功能,主要目的是实现老师设置位置签到范围,学生在一定范围内进行签到的功能。功能如下方截图:


屏幕快照 2019-01-28 上午10.29.26.png

简要介绍:

下面记录一下主要的实现流程,功能的实现主要是根据百度地图开发者官网提供的api文档,各项功能之间组合。百度地图的SDK现在分成了地图功能和定位功能两块不同的SDK,BaiduMapAPI这个是基础的地图功能,BMKLocationKit这个是定位功能。项目里实现定位签到功能用的的SDK包括上面说的这两个模块,所以在用cocopods引入framework的时候,需要引入:#百度地图 pod 'BMKLocationKit' pod 'BaiduMapKit'

功能实现

一、在APPdelegate.m文件中引入:
#import <BaiduMapAPI_Base/BMKBaseComponent.h>
#import <BMKLocationKit/BMKLocationComponent.h>

加入功能代码:

#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 <BMKLocationKit/BMKLocationComponent.h>
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
遵循协议<BMKMapViewDelegate,BMKLocationManagerDelegate>
声明全局变量
@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 <BMKOverlay>)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;
}

至此,在地图上选点进行签到功能基本实现,另外,关于 自定义的范围圆圈的颜色,边框大小都是可以自定义的,选点的标记也是可以自定义的,官方文档有说明

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,511评论 1 330
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,495评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,595评论 0 225
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,558评论 0 190
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,715评论 3 270
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,672评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,112评论 2 291
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,837评论 0 181
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,417评论 0 228
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,928评论 2 232
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,316评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,773评论 2 234
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,253评论 3 220
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,827评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,440评论 0 180
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,523评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,583评论 2 249

推荐阅读更多精彩内容