iOS记录地图轨迹

这次给大家分享一个在地图上画出行程,轨迹的方法.
我使用的是高德地图SDK.

首先得初始化高德地图,在Appdelegate.m中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

初始化高德地图. GDAPIKey就是你申请的key.

//高德地图SDK
- (void)configureAPIKey {
    [MAMapServices sharedServices].apiKey = (NSString *)GDAPIKey;
}

然后你需要在你使用地图的那个类的.m文件的后缀改成.mm.

我们现在创建一个地图.并且打开定位.

- (instancetype)init {
    self = [super init];
    if (self) {
        self.pointArr = [NSMutableArray array];//存储轨迹的数组.
        [self setMapView];
    }
    return self;
}

- (void)setMapView {
    //地图初始化
    self.mapView = [[MAMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _mapView.backgroundColor = [UIColor whiteColor];
    self.mapView.delegate = self;
    //设置定位精度
    _mapView.desiredAccuracy = kCLLocationAccuracyBest;
    //设置定位距离
    _mapView.distanceFilter = 1.0f;
    //普通样式
    _mapView.mapType = MAMapTypeStandard;
    //地图跟着位置移动
    [_mapView setUserTrackingMode:MAUserTrackingModeFollow animated:YES];
    //设置成NO表示关闭指南针;YES表示显示指南针
    _mapView.showsCompass= YES;
    //设置指南针位置
    _mapView.compassOrigin= CGPointMake(_mapView.compassOrigin.x, 22);
    //设置成NO表示不显示比例尺;YES表示显示比例尺
    _mapView.showsScale= YES;
    //设置比例尺位置
    _mapView.scaleOrigin= CGPointMake(_mapView.scaleOrigin.x, 22);
    //开启定位
    _mapView.showsUserLocation = YES;
    //缩放等级
    [_mapView setZoomLevel:18 animated:YES];
    
    //防止系统自动杀掉定位 -- 后台定位
    _mapView.pausesLocationUpdatesAutomatically = NO;
    _mapView.allowsBackgroundLocationUpdates = YES;
    [self.view addSubview:self.mapView];
}

实现相应的协议.

#pragma mark - MAMapViewDelegate
//当位置改变时候调用
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {
    //updatingLocation 标示是否是location数据更新, YES:location数据更新 NO:heading数据更新
    if (updatingLocation == YES) {
        self.currentUL = userLocation;//设置当前位置
        //手机位置信息
        [self setPointArrWithCurrentUserLocation];
    }
}
//定位失败
- (void)mapView:(MAMapView *)mapView didFailToLocateUserWithError:(NSError *)error {
    NSString *errorString = @"";
    switch([error code]) {
        case kCLErrorDenied:
            //Access denied by user
            errorString = @"Access to Location Services denied by user";
            break;
        case kCLErrorLocationUnknown:
            //Probably temporary...
            errorString = @"Location data unavailable";
            //Do something else...
            break;
        default:
            errorString = @"An unknown error has occurred";
            break;
    }
}

那么重点来了,既然要画出轨迹,就要设置地图覆盖物.

//画线方法
- (MAOverlayView *)mapView:(MAMapView *)mapView viewForOverlay:(id <MAOverlay>)overlay {
    //画线
    if ([overlay isKindOfClass:[MAPolyline class]]) {
        MAPolylineView *polylineView = [[MAPolylineView alloc] initWithPolyline:overlay];
        polylineView.lineWidth = 8.f;
        polylineView.strokeColor = [UIColor colorWithRed:177 / 255.0 green:152 / 255.0 blue:198 / 255.0 alpha:0.6];
        return polylineView;
    }
    return nil;
}

然后上方在在位置变更的时候调用了一个方法.我把每个变更的点统一转成MAPointAnnotation对象,并且添加到数组.

[self setPointArrWithCurrentUserLocation];
//设置数组元素并且去执行画线操作
- (void)setPointArrWithCurrentUserLocation {
    //    NSLog(@"记录一个点");
    //检查零点
    if (_currentUL.location.coordinate.latitude == 0.0f ||
        _currentUL.location.coordinate.longitude == 0.0f)
        return;
    MAPointAnnotation *point = [[MAPointAnnotation alloc] init];
    point.coordinate = _currentUL.location.coordinate;
    [_pointArr addObject:point];
    //画线
    [self drawTrackingLine];
}

然后开始执行画线操作.

//绘制旅行路线
- (void)drawTrackingLine {
    MAMapPoint *pointArray = new MAMapPoint[_pointArr.count];//创建结构体数组
    for(int index = 0; index < _pointArr.count; index++) {
        MAPointAnnotation *locationUser = [[MAPointAnnotation alloc] init];
        locationUser = [_pointArr objectAtIndex:index];
        MAMapPoint point = MAMapPointForCoordinate(locationUser.coordinate); 
        pointArray[index] = point;
    }
    //在每次画出轨迹线的时候把之前的线删除掉.不然会多次添加.
    if (self.routeLine) {
        [self.mapView removeOverlay:self.routeLine];
    }
    self.routeLine = [MAPolyline polylineWithPoints:pointArray count:_pointArr.count];
    if (nil != self.routeLine) {
        //将折线绘制在地图底图标注和兴趣点图标之下
        [self.mapView addOverlay:self.routeLine];
    }
    delete []pointArray;
}

这样子就可以运行了.整个轨迹记录的过程分享完了.

当然在关闭地图界面注意释放内存.

#pragma mark - clear mapview
- (void)clearMapView {
    self.mapView.showsUserLocation = NO;
    [self.mapView removeAnnotations:self.mapView.annotations];
    [self.mapView removeOverlays:self.mapView.overlays];
    self.mapView.delegate = nil;
}

demo github地址:BYTrackDemo

demo中提供的方法,自己也可以将圆形覆盖物和地理围栏相结合,在记录行程过程中,可以判断有没有经过某个点的多少范围(半径)内.
demo编译不通过,运行不了的话,可以将BYMapViewVC这个类,放到自己的已经配置好高德地图的项目进行测试.

使用百度地图的话,有个更好的选择百度鹰眼.如果想自己写的话,就把高德的协议改成百度的,类也改成百度的就好了.

只不过记录行程轨迹就得打开后台持续定位,比较耗电,这点比较烦.

好了,以上就是分享的iOS记录地图轨迹.感觉不错的可以点个喜欢,哈哈.
有什么不足的地方还请大家指出.谢谢~

更新:

在定位的方法中可以主动筛选一些距离相近的点.比如当前点和之前的点相差距离小于10米那么就不将这个点计入画线数组.如下:

//当位置改变时候调用
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {
    //updatingLocation 标示是否是location数据更新, YES:location数据更新 NO:heading数据更新
    if (updatingLocation == YES) {
//        self.currentUL = userLocation;//设置当前位置
//        //手机位置信息
//        [self setPointArrWithCurrentUserLocation];
        //增加距离判断
        if (self.currentUL) {
            CLLocationDistance distance = [userLocation.location distanceFromLocation:self.currentUL.location];
            //判断当前点与之前点距离相差小于10米就不计入计算.
            if (distance < 10) {
                return;
            } else {
                self.currentUL = userLocation;//设置当前位置
            }
        } else {
            self.currentUL = userLocation;//设置当前位置
        }
        //手机位置信息
        [self setPointArrWithCurrentUserLocation];
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容