iOS之地图的使用和实时描绘运动轨迹
来源:互联网 发布:浙江网络干部学院金华 编辑:程序博客网 时间:2024/05/29 03:15
这里将通过代码详细的讲解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
5 0
- iOS之地图的使用和实时描绘运动轨迹
- iOS 之地图上绘制运动轨迹
- ios使用地图定位记录运动轨迹
- iOS 百度地图运动轨迹的角度
- iOS开发之在地图上绘制出你运动的轨迹
- iOS开发之在地图上绘制出你运动的轨迹
- iOS开发之在地图上绘制出你运动的轨迹
- iOS开发之在地图上绘制出你运动的轨迹
- iOS开发之在地图上绘制出你运动的轨迹
- iOS开发之在地图上绘制出你运动的轨迹
- iOS开发之在地图上绘制出你运动的轨迹
- iOS开发之在地图上绘制出你运动的轨迹
- iOS开发之在地图上绘制出你运动的轨迹
- iOS 在地图上绘制出你运动的轨迹
- 在地图上绘制你运动的轨迹 ios
- google地图--运动轨迹(自己上班的运动轨迹)
- iOS 在地图上绘制运动轨迹
- Mathematica描绘滚球某个点的运动轨迹(摆线)
- Android提高第十篇之AudioRecord实现"助听器"
- python之各类错误汇总[一]
- Android APK编译后文件结构及安装到devices后文件形态
- 338. Counting Bits
- Linux常用命令-权限管理
- iOS之地图的使用和实时描绘运动轨迹
- 嵌入式学习-linux系统-lesson1-Uboot相关
- Android提高第十一篇之模拟信号示波器
- android 获取url 参数值
- poj3061(二分前缀和或者尺取法)
- Leetcode 13. Roman to Integer (Easy) (cpp)
- 关于 Akka actor中使用 slf4j日志的问题
- PHP特性01:cURL实现get和post,在url间做数据交互;
- 三层交换机和二层交换机的区别 如何选择第三层交换机