这里将通过代码详细的讲解iOS地图的使用:

第一步:添加框架(虽然不知道什么时候起不用再添加框架,Xcode会自动的帮我们添加,但我们还是添加一下)

第二步:配置pliset文件

第三步:直接上代码块,一定要仔细阅读

#import "ViewController.h"
//导入定位和地图的两个框架
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>//签订定位和地图的代理协议
@interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>//位置管理者
@property (nonatomic, strong) CLLocationManager *localManager;
//地图
@property (nonatomic, strong) MKMapView *mapView;
//存放用户位置的数组
@property (nonatomic, strong) NSMutableArray *locationMutableArray;@end@implementation ViewController
#pragma mark - 位置管理者懒加载
- (CLLocationManager *)localManager
{if (_localManager == nil){_localManager = [[CLLocationManager alloc]init];//设置定位的精度[_localManager setDesiredAccuracy:kCLLocationAccuracyBest];//位置信息更新最小距离_localManager.distanceFilter = 10;//设置代理_localManager.delegate = self;//如果没有授权则请求用户授权,//因为 requestAlwaysAuthorization 是 iOS8 后提出的,需要添加一个是否能响应的条件判断,防止崩溃if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined && [_localManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {[_localManager requestAlwaysAuthorization];}//创建存放位置的数组_locationMutableArray = [[NSMutableArray alloc] init];}return _localManager;
}- (void)viewDidLoad
{[super viewDidLoad];//全屏显示地图并设置地图的代理_mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];_mapView.delegate = self;//是否启用定位服务if ([CLLocationManager locationServicesEnabled]){NSLog(@"开始定位");//调用 startUpdatingLocation 方法后,会对应进入 didUpdateLocations 方法[self.localManager startUpdatingLocation];}else{NSLog(@"定位服务为关闭状态,无法使用定位服务");}//用户位置追踪_mapView.userTrackingMode = MKUserTrackingModeFollow;/**地图的样式:MKMapTypeStandard, 标准地图MKMapTypeSatellite, 卫星地图MKMapTypeHybrid, 混合地图MKMapTypeSatelliteFlyover, 卫星立体地图MKMapTypeHybridFlyover, 混合立体地图*/_mapView.mapType = MKMapTypeStandard;[self.view addSubview:_mapView];
}#pragma mark - MKMapViewDelegate
/**
更新用户位置,只要用户改变则调用此方法(包括第一次定位到用户位置)
第一种画轨迹的方法:我们使用在地图上的变化来描绘轨迹,这种方式不用考虑从 CLLocationManager 取出的经纬度在 mapView 上显示有偏差的问题*/
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{NSString *latitude = [NSString stringWithFormat:@"%3.5f",userLocation.coordinate.latitude];NSString *longitude = [NSString stringWithFormat:@"%3.5f",userLocation.coordinate.longitude];NSLog(@"更新的用户位置:纬度:%@, 经度:%@",latitude,longitude);//设置地图显示范围(如果不进行区域设置会自动显示区域范围并指定当前用户位置为地图中心点)MKCoordinateSpan span = MKCoordinateSpanMake(0.05, 0.05);MKCoordinateRegion region=MKCoordinateRegionMake(userLocation.location.coordinate, span);[_mapView setRegion:region animated:true];if (_locationMutableArray.count != 0) {//从位置数组中取出最新的位置数据NSString *locationStr = _locationMutableArray.lastObject;NSArray *temp = [locationStr componentsSeparatedByString:@","];NSString *latitudeStr = temp[0];NSString *longitudeStr = temp[1];CLLocationCoordinate2D startCoordinate = CLLocationCoordinate2DMake([latitudeStr doubleValue], [longitudeStr doubleValue]);//当前确定到的位置数据CLLocationCoordinate2D endCoordinate;endCoordinate.latitude = userLocation.coordinate.latitude;endCoordinate.longitude = userLocation.coordinate.longitude;//移动距离的计算double meters = [self calculateDistanceWithStart:startCoordinate end:endCoordinate];NSLog(@"移动的距离为%f米",meters);//为了美化移动的轨迹,移动的位置超过10米,方可添加进位置的数组if (meters >= 0){NSLog(@"添加进位置数组");NSString *locationString = [NSString stringWithFormat:@"%f,%f",userLocation.coordinate.latitude, userLocation.coordinate.longitude];[_locationMutableArray addObject:locationString];//开始绘制轨迹CLLocationCoordinate2D pointsToUse[2];pointsToUse[0] = startCoordinate;pointsToUse[1] = endCoordinate;//调用 addOverlay 方法后,会进入 rendererForOverlay 方法,完成轨迹的绘制MKPolyline *lineOne = [MKPolyline polylineWithCoordinates:pointsToUse count:2];[_mapView addOverlay:lineOne];}else{NSLog(@"不添加进位置数组");}}else{//存放位置的数组,如果数组包含的对象个数为0,那么说明是第一次进入,将当前的位置添加到位置数组NSString *locationString = [NSString stringWithFormat:@"%f,%f",userLocation.coordinate.latitude, userLocation.coordinate.longitude];[_locationMutableArray addObject:locationString];}
}-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{if ([overlay isKindOfClass:[MKPolyline class]]){
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wdeprecated-declarations"MKPolylineView *polyLineView = [[MKPolylineView alloc] initWithPolyline:overlay];polyLineView.lineWidth = 10; //折线宽度polyLineView.strokeColor = [UIColor blueColor]; //折线颜色return (MKOverlayRenderer *)polyLineView;
#pragma clang diagnostic pop}return nil;
}#pragma mark - CLLocationManagerDelegate
/***  当前定位授权状态发生改变时调用**  @param manager 位置管理者*  @param status  授权的状态*/
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{switch (status) {case kCLAuthorizationStatusNotDetermined:{NSLog(@"用户还未进行授权");break;}case kCLAuthorizationStatusDenied:{// 判断当前设备是否支持定位和定位服务是否开启if([CLLocationManager locationServicesEnabled]){NSLog(@"用户不允许程序访问位置信息或者手动关闭了位置信息的访问,帮助跳转到设置界面");NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];if ([[UIApplication sharedApplication] canOpenURL:url]) {[[UIApplication sharedApplication] openURL: url];}}else{NSLog(@"定位服务关闭,弹出系统的提示框,点击设置可以跳转到定位服务界面进行定位服务的开启");}break;}case kCLAuthorizationStatusRestricted:{NSLog(@"受限制的");break;}case kCLAuthorizationStatusAuthorizedAlways:{NSLog(@"授权允许在前台和后台均可使用定位服务");break;}case kCLAuthorizationStatusAuthorizedWhenInUse:{NSLog(@"授权允许在前台可使用定位服务");break;}default:break;}
}
/**我们并没有把从 CLLocationManager 取出来的经纬度放到 mapView 上显示原因:我们在此方法中取到的经纬度依据的标准是地球坐标,但是国内的地图显示按照的标准是火星坐标MKMapView 不用在做任何的处理,是因为 MKMapView 是已经经过处理的也就导致此方法中获取的坐标在 mapView 上显示是有偏差的解决的办法有很多种,可以上网就行查询,这里就不再多做赘述*/
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{// 设备的当前位置CLLocation *currLocation = [locations lastObject];NSString *latitude = [NSString stringWithFormat:@"纬度:%3.5f",currLocation.coordinate.latitude];NSString *longitude = [NSString stringWithFormat:@"经度:%3.5f",currLocation.coordinate.longitude];NSString *altitude = [NSString stringWithFormat:@"高度值:%3.5f",currLocation.altitude];NSLog(@"位置发生改变:纬度:%@,经度:%@,高度:%@",latitude,longitude,altitude);[manager stopUpdatingLocation];
}//定位失败的回调方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{NSLog(@"无法获取当前位置 error : %@",error.localizedDescription);
}#pragma mark - 距离测算
- (double)calculateDistanceWithStart:(CLLocationCoordinate2D)start end:(CLLocationCoordinate2D)end {double meter = 0;double startLongitude = start.longitude;double startLatitude = start.latitude;double endLongitude = end.longitude;double endLatitude = end.latitude;double radLatitude1 = startLatitude * M_PI / 180.0;double radLatitude2 = endLatitude * M_PI / 180.0;double a = fabs(radLatitude1 - radLatitude2);double b = fabs(startLongitude * M_PI / 180.0 - endLongitude * M_PI / 180.0);double s = 2 * asin(sqrt(pow(sin(a/2),2) + cos(radLatitude1) * cos(radLatitude2) * pow(sin(b/2),2)));s = s * 6378137;meter = round(s * 10000) / 10000;return meter;
}@end

代码中详细讲解了地图的使用以及真机情况下可能出现的各种情况,地图的定位、位移是经常使用的功能,可以通过以上的代码入门,更好的帮助你进行地图功能的开发;代码的实现还有实时绘制轨迹的功能,当然实时绘制轨迹的功能也不是尽如完美,而且现在百度还推出了鹰眼地图,可以查看自己的轨迹信息。进行开发就是选择最适合自己的方法完成功能需求。

代码下载地址:http://download.csdn.net/detail/wuzesong/9575926

iOS之地图的使用和实时描绘运动轨迹相关推荐

  1. iOS百度地图SDK之实时绘制轨迹(后台仍执行)

    首先,对于百度地图SDK的配置和环境搭建就不做说明,需要的人可以博客中另一篇文章看 <iOS百度地图SDK基本使用> ,本文的重点在于实现实时绘制轨迹的功能,并且对细节进行处理和优化 1. ...

  2. ios 百度地图指定区域_ios百度地图的使用(普通定位、反地理编码)

    iOS定位 - 普通定位(没有地图) - 反地理编码(得到具体位置),下面通过代码给大家详解,代码如下: #import 使用到的头文件 要引入CoreLocation这个包 使用的代理名称 //1. ...

  3. iOS开发--地图与定位

    iOS开发--地图与定位 概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们 ...

  4. flutter引入高德地图_Flutter笔记-调用原生IOS高德地图sdk

    一.前言 2017年底因公司业务组合部门调整,新的团队部分维护的项目用React Native技术混合开发.为适应环境变化,开启疯狂RN学习之旅,晚上回来啃资料看视频.可能由于本身对RN技术体验不感冒 ...

  5. (器) 构建自由通行的IOS开发者地图

    构建自由通行的IOS开发者地图 -----IOS开发人员知识技能归档固化管理 最近辞职在家,无意之酝酿,多有开发感触,故想做道法术器四文<(道)良性成瘾开发习惯养成策略><(法)平衡 ...

  6. 【高德LBS开源组件大赛】iOS版地图选中Overlay功能组件

    2019独角兽企业重金招聘Python工程师标准>>> ##开源组件名称 iOS版地图选中Overlay功能组件​ ##开源组件说明及使用场景 提供在iOS版地图中选中Overlay ...

  7. 流媒体服务器+终端(android,ios,web)来实现实时监控或着说是直播

    简介说明: 流媒体服务器+终端(android,ios,web)来实现实时监控或着说是直播 以下几个因素建议考虑 采集.预处理.编码.传输.服务器转码.解码 采集采集是播放环节中的第一环,iOS 系统 ...

  8. iOS开发76-使用Xcode查看实时日志

    iOS开发76-使用Xcode查看实时日志 在Xcode上运行App代码的时候,可以实时查看到日志. 在没有APP源代码的情况下,直接使用Xcode查看Debug版本的App来查看实时日志. 步骤如下 ...

  9. iOS关于地图定位基础(一)

    若看完这篇的朋友,可以查看我的下一篇iOS关于地图定位基础(二)    随着更多的LBS(Location Based Service)业务被集成到手机应用  同时鉴于在iOS中地图的学习是看了又忘, ...

最新文章

  1. 秒杀系统架构优化思路
  2. 2020年10月份Github上热门的开源项目
  3. [洪流学堂]Hololens开发高级篇1:凝视(Gaze)
  4. [note]标点符号和数学符号所对应的英文
  5. AI顶会论文“趋势”:对新方法的过度关注,与现实问题的脱节
  6. webpack-dev-server启动后, localhost:8080返回index.html的原理
  7. 《Arduino家居安全系统构建实战》——2.3 组合多个单词
  8. 计算机通信职称评定,2017年通信工程师中级职称评定条件说明
  9. 玩转 ESP32 + Arduino (十八) 采用SIM800L发送短信和定位(基础知识)
  10. 论文被多人研究过了,我还可以怎么写?
  11. 深入理解Symbol
  12. linux时间为什么没有北京,Linux时区选择为何没有北京?
  13. 贝叶斯统计bayes statistics
  14. c语言 int与byte[]互相转换
  15. A16Z和去中心化金融大佬们在伯克利都讲了什么?|创业者说
  16. iPhone4S安装Linux系统,Absinthe 2.0.4 官网Windows/Mac/Linux原版下载—iPhone4S和iPad2完美越狱工具...
  17. 【踩坑实录】hive删除字段报错
  18. 区块链应用(去中心化应用)是什么样的?
  19. 生物信息学竞赛:糖尿病数据挖掘
  20. 达内java第一个月月考_为什么我努力了一个月,月考成绩却更低了?

热门文章

  1. 我的互联网创业公司的第一笔收入磨难记
  2. 【Android 11】【WiFi模块】WiFi打开函数调用流程图
  3. 苹果春季发布会前瞻:全新 iPhone SE 3 将揭晓,M2芯片来袭?
  4. Latex使用总结(待完善)
  5. 健身装备有哪些推荐?健身运动装备品牌排行榜
  6. 多锐运动下载 V1.2.4 官方版
  7. Option3X 5G 全网部署(基于 IUV_5G 软件)
  8. jekyll php,使用Jekyll在Github上搭建个人博客(文章分类索引)
  9. 微信小程序下载视频到相册(带进度条)
  10. Springboot - 处理LocalDateTime的入参和出参格式