ios 持续获取定位 高德地图_iOS 定位 高德地图
最近项目中有需求需要使用定位,上报经纬度和地址信息,还有可以在地图界面随意选择地点,因为和后台经纬度匹配的问题,所以选择了高德地图(百度地图经纬度是有自己算法的)。
1.定位
iOS定位SDK提供后台持续定位的能力,可持久记录位置信息,适用于记轨迹录。
在调用定位功能的类中引入
AMapFoundationKit.h
AMapLocationKit.h
这两个头文件
在调用定位时,需要添加Key,需要注意的是请在 SDK 任何类的初始化以及方法调用之前设置正确的 Key。
将info.plist的字段改成
NSLocationAlwaysUsageDescription
字段。
⚠️ ⚠️ 在iOS 11 中,新出现了一个
NSLocationAlwaysAndWhenInUseUsageDeion
字段,如果以前设置的NSLocationAlwaysUsageDescription字段无用,请查看 《iOS11 Xcode9 更新适配》 传送门
如果需要后台持续定位需要开启 TARGETS->Capabilities->Background Modes
image.png
开启定位
self.locationManager = [[AMapLocationManager alloc] init];
[self.locationManager setDelegate:self];
//iOS 9(不包含iOS 9) 之前设置允许后台定位参数,保持不会被系统挂起
[self.locationManager setPausesLocationUpdatesAutomatically:NO];
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
// iOS 9(包含iOS 9)之后新特性:将允许出现这种场景,同一app中多个locationmanager:一些只能在前台定位,另一些可在后台定位,并可随时禁止其后台定位。
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
self.locationManager.allowsBackgroundLocationUpdates = NO;
}
// 后台定位是否返回逆地理信息
[self.locationManager setLocatingWithReGeocode:YES];
// 开始持续定位
[self.locationManager startUpdatingLocation];
回调函数,处理经纬度及地址信息
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode {
NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
if (regeocode) {
NSLog(@"reGeocode:%@", regeocode);
}
}
进行持续定位的时候,有些经纬度会漂移,比如室内等信号不好的地方,可以根据需求用以下的方法做一些简单的处理
// 小于70 认为GPS信号弱
NSString *horizontal = [NSString stringWithFormat:@"GPS信号为%lf",location.horizontalAccuracy];
// 速度为0 过滤掉
if (location.speed <= 0) {
self.currLocation = nil;
NSLog(@"速度为0");
return;
}
// 计算距离
MAMapPoint point1 = MAMapPointForCoordinate(CLLocationCoordinate2DMake(self.currLocation.coordinate.latitude,self.currLocation.coordinate.longitude));
MAMapPoint point2 = MAMapPointForCoordinate(CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude));
CLLocationDistance distance = MAMetersBetweenMapPoints(point1,point2);
if (distance > 100) {
self.currLocation = nil;
NSLog(@"距离相差过大%f",distance);
return;
}
// 判断当前经纬度与上一个经纬度是否相同,相同则过滤掉
if (self.currLocation == location) {
self.currLocation = nil;
NSLog(@"相同经纬度");
return;
}
2.地图
初始化地图
//地图需要v4.5.0及以上版本才必须要打开此选项(v4.5.0以下版本,需要手动配置info.plist)
[AMapServices sharedServices].enableHTTPS = YES;
///初始化地图
_mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
_mapView.delegate = self;
[_mapView setZoomLevel:17.5 animated:YES];
[self.view addSubview:_mapView];
// 初始化地理编码
_search = [[AMapSearchAPI alloc] init];
_search.delegate = self;
点击地图上任一点显示信息,并添加标注
#pragma mark 点击地图方法
- (void)mapView:(MAMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate {
if (!self.isCheck) {
_mapView.showsUserLocation = NO;
_mapView.userTrackingMode = MAUserTrackingModeNone;
// 点击位置的经纬度
_touchMapCoordinate = coordinate;
// 如果有上一个标注,先删除
NSInteger annotationsNum = _mapView.annotations.count;
if (annotationsNum > 0) {
for (int i = 0; i < annotationsNum; i ++) {
NSLog(@"%@",_mapView.annotations);
if (i < _mapView.annotations.count) {
id item = [_mapView.annotations objectAtIndex:i];
[_mapView removeAnnotation:item];
}
}
}
// 逆地理编码获取地址
[self reocodeSearch];
} else {
// 逆地理编码获取地址
[self reocodeSearch];
}
}
#pragma mark 逆地理编码
- (void)reocodeSearch {
AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
regeo.location = [AMapGeoPoint locationWithLatitude:_touchMapCoordinate.latitude longitude:_touchMapCoordinate.longitude];
regeo.requireExtension = YES;
[_search AMapReGoecodeSearch:regeo];
}
#pragma mark 标注delegate方法
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id )annotation
{
if ([annotation isKindOfClass:[MAUserLocation class]]) {
return nil;
}
if ([annotation isKindOfClass:[MAPointAnnotation class]])
{
static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
CustomAnnotationView *annotationView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
if (annotationView == nil)
{
annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
}
if (!self.isCheck) {
annotationView.canShowCallout= YES; //设置气泡可以弹出,默认为NO
annotationView.animatesDrop = YES; //设置标注动画显示,默认为NO
annotationView.pinColor = MAPinAnnotationColorPurple;
annotationView.canShowCallout = NO;
annotationView.enabled = NO;
} else {
annotationView.canShowCallout= NO; //设置气泡可以弹出,默认为NO
annotationView.animatesDrop = NO; //设置标注动画显示,默认为NO
annotationView.pinColor = MAPinAnnotationColorPurple;
annotationView.canShowCallout = NO;
annotationView.enabled = NO;
}
return annotationView;
}
return nil;
}
#pragma mark 逆地理编码成功
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
if (response.regeocode != nil)
{
AMapReGeocode *regeocode = response.regeocode;
// 解析response获取地址描述,具体解析见 Demo
// regeocode.formattedAddress 格式化标准地址
[self addAnnotationWithAddress:regeocode.formattedAddress];
}
}
#pragma mark 逆地理编码失败
- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error
{
NSLog(@"Error: %@", error);
}
#pragma mark 添加标注并设置地址内容
- (void)addAnnotationWithAddress:(NSString *)address {
MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
pointAnnotation.coordinate = _touchMapCoordinate;
pointAnnotation.title = address;
_touchMapAddress = address;
[_mapView addAnnotation:pointAnnotation];
[_mapView selectAnnotation:pointAnnotation animated:YES];
}
Demo 传送门
自己学习的时候写的,欢迎大家提出建议和意见
ios 持续获取定位 高德地图_iOS 定位 高德地图相关推荐
- ios 持续获取定位 高德地图_概述-iOS 定位SDK | 高德地图API
简介 高德 iOS 定位 SDK 提供了不依赖于地图定位的定位功能,开发者可以无地图显示的场景中便捷地为应用程序添加定位功能. iOS定位SDK提供了单次定位.连续定位.逆地理信息.地理围栏等功能. ...
- ios 持续获取定位 高德地图_iOS开发-- 高德地图的接入使用(1)定位
单次定位 @property(nonatomic,strong)AMapLocationManager * locationManager;// 开启定位 // 懒加载创建你的定位的Manager - ...
- ios 持续获取定位 高德地图_【IOS开发】高德地图定位坐标偏差()
CLLocationManager类可以实时的获得我们位置的经纬度,并且可以通过经纬度在MapView上定位: //创建CLLocationManager对象 CLLocationManager*lo ...
- iOS中获取相册、相机、定位、以及麦克风权限设置
1.判断用户是否获取了相册的访问权限 #import <AssetsLibrary/AssetsLibrary.h>// 获取相册权限 - (void)getPhotoLibraryAut ...
- ios 点生成线路 百度地图_iOS SDK | 百度地图API SDK
注意事项 1.静态库中采用ObjectC++实现,因此需要您保证您工程中至少有一个.mm后缀的源文件(您可以将任意一个.m后缀的文件改名为.mm),或者在工程属性中指定编译方式,即在Xcode的Pro ...
- ios 高德获取定位_解决ios11不支持高德地图API定位功能的方法
在 iOS 11 系统上访问JS API定位业务失败怎么解决? 苹果新发的 iOS 11 操作系统的一大特性是对 http 形式访问页面的限制变得非常严格(相比iOS 10 和 iOS 9).高德提供 ...
- ios 高德获取定位_单次定位-获取位置-开发指南-iOS 定位SDK | 高德地图API
iOS定位SDK提供的单次定位方法基于苹果定位核心,苹果定位核心会在设备移动时连续返回定位结果,高德在此基础上封装了单次定位.当设备可以正常联网时,还可以返回该定位点的对应的中国境内位置信息(包括:省 ...
- IOS之高德地图(一)显示出地图并定位成功
任务:显示地图在界面上并成功定位 一:我们导入高德地图的API 在Podfile platform :ios, '7.0' target '你的项目名称' do pod 'AMap3DMap' pod ...
- 高德地图——浏览器定位+点击获取经纬度+去除高德百度地图左下角logo
高德地图--浏览器定位+点击获取经纬度+去除高德百度地图左下角logo 1.代码 <!doctype html> <html> <head><meta cha ...
最新文章
- C# 特性(Attribute)学习。
- linux 路由器去广告,linux – 如何正确发送路由器广告?
- 直接引用arXiv论文不规范?试试这个小工具,秒变正式发表链接,上交大校友开发...
- TF:利用TF读取数据操作,将CIFAR-10 数据集中的训练图片读取出来,并保存为.jpg格式
- 千万数据去重_mysql去重,3亿多数据量
- C和C++数据结构相关概念
- const常量与define宏定义的区别
- ros安装过后怎么找不到安装文件_ros配置乐视奥比中光相机
- spring boot学习(5): 进程exit code自定义
- IOS之学习笔记九(对象的初始化)
- 删除ELK中的数据。。
- MentoHUST讲解教程(锐捷破解)
- 震惊世人的10个Python黑科技,你知道几个?
- ios12完美深色模式插件_那些好玩的插件 iOS 12(十七)
- 宝塔环境下MinDoc的安装教程
- ESN学习笔记——echotorch(2)narma10
- 第1节 基本数据类型分析
- Unity内动态影子的各种做法
- Outlook2013邮箱打开Word附件为受保护模式的调整方法
- STM32F103学习笔记(5)——数码管驱动TM1650使用