iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >iOS实现百度地图定位签到功能
  • 924
分享到

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

ios百度地图定位 2022-05-26 13:05:54 924人浏览 独家记忆
摘要

写在前面: 项目需求用到这个功能,主要目的是实现老师设置位置签到范围,学生在一定范围内进行签到的功能。 功能如下方截图: 屏幕快照 2019-01-28 上午10.29.26.p

写在前面:

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

功能如下方截图:

屏幕快照 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;
@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;这个方法:


#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;
}

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

总结

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

--结束END--

本文标题: iOS实现百度地图定位签到功能

本文链接: https://www.lsjlt.com/news/30298.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Vue集成百度地图实现位置选择功能
    目录Vue集成百度地图实现位置选择百度地图选择地址效果集成百度地图的具体实现第一步:引入百度地图 JavaScript API v3.0 文件第二步:编写百度地图选择位置组件第三步:...
    99+
    2024-04-02
  • html5怎么实现定位并在百度地图上显示的功能
    这篇文章主要讲解了“html5怎么实现定位并在百度地图上显示的功能”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“html5怎么实现定位并在百度地图上显示的功...
    99+
    2024-04-02
  • Android如何实现地图定位?Android studio+百度地图API+Android6.0系统实现地图显示、地址设置、点击地图定位功能(详细)
    文章说明:本文初衷是为了记录毕设学习过程,避免忘记操作流程。该功能是毕业设计的Android软件端的功能之一,本文将从获取百度地图密钥(AK)开始,详细地对地图定位配置和相关代码进行说明,文末将附上实现该功能的代码。后续等答辩完成会把整个A...
    99+
    2023-10-11
    android android studio 个人开发 百度
  • Vue使用百度地图实现城市定位
    本文实例为大家分享了Vue使用百度地图实现城市定位的具体代码,供大家参考,具体内容如下 Vue项目运行环境:Vue 2.0,Vue Cli 3.0 步骤一:登录 百度地图开放平台 在...
    99+
    2024-04-02
  • Vue项目引用百度地图并实现搜索定位等功能(案例分析)
    目录一、效果图及功能点二、前期准备三、引入百度地图四、功能解析本文给大家介绍如何在vue项目中引用百度地图,并设计实现简单的地图定位、地址搜索功能。 Tip:本篇文章为案例分析,技术...
    99+
    2024-04-02
  • java地图定位功能怎么实现
    要实现Java地图定位功能,可以使用以下步骤:1. 使用Java地图库,如Google Maps API、Baidu Maps AP...
    99+
    2024-02-29
    java
  • 基于Redis位图实现用户签到功能
    场景需求 适用场景如签到送积分、签到领取奖励等,大致需求如下: 签到1天送1积分,连续签到2天送2积分,3天送3积分,3天以上均送3积分等。 如果连续签到中断,则重...
    99+
    2024-04-02
  • vue中百度地图定位及附近搜索功能使用步骤
    目录1.地图初始化相关2.获取当前定位3.根据当前定位地址附近搜索建议1.地图初始化相关 文档:lbs.baidu.com/index.phpt… 申请账号 =>...
    99+
    2024-04-02
  • Android 模拟地图定位功能的实现
    实现原理: 手机定位方式目前有4种: 基站定位WIFI定位GPS定位AGPS定位 本工程利用手机自带的"模拟位置"功能实现运行时修改LocationManager...
    99+
    2024-04-02
  • vue百度地图 + 定位的示例分析
    这篇文章给大家分享的是有关vue百度地图 + 定位的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。vue 百度地图 + 定位 前提需要自己有百度的密钥,如没有可...
    99+
    2024-04-02
  • JS如何实现百度地图搜索悬浮窗功能
    这篇文章将为大家详细讲解有关JS如何实现百度地图搜索悬浮窗功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。这个需求的效果类似下面的截图,主要还是利用百度地图中自定义控件...
    99+
    2024-04-02
  • Vue使用高德地图选点定位搜索定位功能实现
    目录一、申请高德地图的使用密钥key。二、安装依赖。三、使用。四、常见问题。项目下载: 高德地图测试demo,高德地图测试demo-Node.js文档 效果图: 一、申请高德地图的...
    99+
    2022-11-13
    vue高德地图定位 vue高德地图搜索定位
  • 使用php开发Websocket,实现实时地图定位功能
    标题:使用PHP开发Websocket实现实时地图定位功能简介:Websocket是一种实现持久连接,实时双向通信的协议,能够实现实时的数据传输和更新。本文将使用PHP开发Websocket,结合地图定位功能,实现实时地图定位功能。下面将详...
    99+
    2023-12-17
    PHP map websocket
  • 微信小程序地图定位功能怎么实现
    本篇内容主要讲解“微信小程序地图定位功能怎么实现”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“微信小程序地图定位功能怎么实现”吧!方法如下首先呢你要有一个方法去触发这个点击事件,下面已经写好啦,...
    99+
    2023-06-30
  • 使用微信小程序实现地图定位功能
    使用微信小程序实现地图定位功能微信小程序作为一种轻量级的应用程序,提供了丰富的能力,其中地图定位功能是许多小程序开发者常常需要使用的。本文将介绍如何使用微信小程序来实现地图定位功能,并给出具体的代码示例。一、准备工作在开始编写代码之前,我们...
    99+
    2023-11-21
    微信小程序 实现功能 地图定位
  • Android中集成高德地图SDK实现地图定位和导航功能(二)
    我们接着上一篇文章开始继续实现android中集成高德地图的SDK实现地图 定位,搜索,导航的功能 如何让地图在手机上实现呢? 1.配置AndroidManifest 中的权限申请 可以参考官方文档 ...
    99+
    2023-09-04
    android java 开发语言 android studio
  • AngularJs如何利用百度地图API 定位当前位置获取地址信息
    这篇文章主要介绍AngularJs如何利用百度地图API 定位当前位置获取地址信息,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!第一、申请百度密钥  很简单的几步就搞定第二...
    99+
    2024-04-02
  • JavaScript自定义日历实现签到功能
    本文实例为大家分享了JavaScript自定义日历签到功能的具体代码,供大家参考,具体内容如下 先看下效果图 红色块为已签到的日期,样式可以随意更改,清晰明了,话不多说上代码: &...
    99+
    2024-04-02
  • vue使用高德地图实现实时定位天气预报功能
    目录JSAPI 的加载使用 JSAPI Loader (推荐)JSAPI key和安全密钥的使用项目代码步骤:1、在index.html页面body中添加密钥2、安装@amap/am...
    99+
    2024-04-02
  • 如何使用Android实现地理定位功能
    这篇文章给大家分享的是有关如何使用Android实现地理定位功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。首先需要的权限有<uses-permission android:name="...
    99+
    2023-05-30
    android
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作