iOSiOS 地图的奇技淫巧iOS 干货整理

iOS记录地图轨迹

2016-09-18  本文已影响4350人  像风一样的孩子丶

这次给大家分享一个在地图上画出行程,轨迹的方法.
我使用的是高德地图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];
    }
}
上一篇 下一篇

猜你喜欢

热点阅读