iOS实现百度地图定位签到功能

iOS实现百度地图定位签到功能,第1张

概述iOS实现百度地图定位签到功能 写在前面: 项目需求用到这个功能,主要目的是实现老师设置位置签到范围,学生在一定范围内进行签到的功能. 功能如下方截图: 屏幕快照 2019-01-28 上午10.29.26.png 简要介绍: 下面记录一下主要的实现流程,功能的实现主要是根据百度地图开发者官网提供的api文档,各项功能之间组合.百度地图的SDK现在分成了地图功能和定位功能两块不同的SDK,BaiduMapAPI这个是基础的地图功能,BMKLocationKit这个是定位功能.项目里实现定位签到功能用的的SDK包括上面说的这两个

写在前面:

项目需求用到这个功能,主要目的是实现老师设置位置签到范围,学生在一定范围内进行签到的功能。

功能如下方截图:


屏幕快照 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]; } //d出半径选择框 [self showLocationSelecTradiusVIEwWithCoordinate:coordinate];}

选点后d出选择定位范围d框

#pragma mark d出位置d框- (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,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;}

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对我们的支持。

总结

以上是内存溢出为你收集整理的iOS实现百度地图定位签到功能全部内容,希望文章能够帮你解决iOS实现百度地图定位签到功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1109953.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存