[iOS]MapView上绘制运动轨迹

/** 位置管理者 */
@property (nonatomic, strong)CLLocationManager *locationManager;

/** 地图 */
@property (nonatomic, weak) MKMapView *mapView;

/** 点数组 */
@property (nonatomic, strong) NSMutableArray *locArray; 

/** 轨迹线 */
@property (nonatomic, strong) MKPolyline *polyLine;

初始化位置管理者

-(CLLocationManager *) locationManager
{
    if (_locationManager == nil)
    {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.distanceFilter = 5; //5米更新一次
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        _locationManager.delegate = self;
        _locationManager.pausesLocationUpdatesAutomatically = NO;
        [_locationManager setActivityType:CLActivityTypeFitness];
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9)
        {
            _locationManager.allowsBackgroundLocationUpdates = YES;
   
        }
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
        {
            //使用期间
            [_locationManager requestAlwaysAuthorization];
        }
    }
    return _locationManager;
}

** 初始化地图**
JZLocationConverter 转换坐标的类在这里~

- (MKMapView *)mapView{
    
    if (!_mapView) {
        MKMapView *mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];

        MKCoordinateSpan span;
        span.latitudeDelta = 0.005;
        span.longitudeDelta = 0.005;//值越小代表地图越精细
        
        MKCoordinateRegion region ;

        region.center = [JZLocationConverter wgs84ToGcj02:self.locationManager.location.coordinate];
        region.span = span;
        
        //设置显示区域
        [mapView setRegion:region];
        
        //跟踪
        mapView.userTrackingMode = MKUserTrackingModeFollow;
        //显示定位
        mapView.showsUserLocation = YES;
         mapView.delegate = self;
 
        _mapView = mapView;
    }
    
    return _mapView;
}

** 初始化数组**

- (NSMutableArray *)locArray {
    if (!_locArray) {
        _locArray = [NSMutableArray array];
    }
    return _locArray;
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:self.mapView];

}

** locationManager更新位置Delegate**

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    
     //标准坐标
    CLLocation *standLoc = [locations lastObject];
    CLLocationCoordinate2D standCoor=standLoc.coordinate;
    
    //中国坐标
    CLLocationCoordinate2D chinaCoor = [ JZLocationConverter wgs84ToGcj02:standCoor];
    CLLocation *chinaLoc = [[CLLocation alloc]initWithCoordinate:chinaCoor altitude:standLoc.altitude horizontalAccuracy:standLoc.horizontalAccuracy verticalAccuracy:standLoc.verticalAccuracy course:standLoc.course speed:standLoc.speed timestamp:standLoc.timestamp];
    
    //控制精度,精度低的点舍弃
    if (standLoc.horizontalAccuracy > 150) {
        return;
    }

    //添加点到数组
    [self.locArray  addObject:chinaLoc];
    
    //绘制轨迹
    [self drawWalkPolyline];
    [self.mapView setCenterCoordinate:chinaCoor animated:NO];
    
}

** 绘制,需要注意的是,这段代码用到了C++语法,所以需要把ViewController.m改成ViewController.mm**

- (void)drawWalkPolyline
{
    //轨迹点
    NSUInteger count = self.locArray.count;
    
    // 手动分配存储空间,结构体:地理坐标点,用直角地理坐标表示 X:横坐标 Y:纵坐标
    MKMapPoint *tempPoints = new MKMapPoint[count];
    
    [self.locArray enumerateObjectsUsingBlock:^(CLLocation *location, NSUInteger idx, BOOL *stop) {
        MKMapPoint locationPoint = MKMapPointForCoordinate(location.coordinate);
        tempPoints[idx] = locationPoint;

    }];
    
    //移除原有的绘图
    if (self.polyLine) {
        [_mapView removeOverlay:self.polyLine];
    }
    
    // 通过points构建BMKPolyline
    self.polyLine = [MKPolyline polylineWithPoints:tempPoints count:count];
    
    //添加路线,绘图
    if (self.polyLine) {
        [_mapView addOverlay:self.polyLine];
    }
    
    // 清空 tempPoints 内存
    delete []tempPoints;
    
}

** 所有轨迹缩放到地图可见**

- (void)zoomToMapPoints:(MKMapView*)mapView
{
    
    if(self.locArray .count)
    {//保证self.loc.locArray不为空,才执行
         CLLocation *firstLoc = self.locArray[0];
        CLLocationCoordinate2D location = firstLoc.coordinate;
        

        
        //放大地图到自身的经纬度位置。
        MKCoordinateSpan span ;
        //        span.latitudeDelta = 0.009 ;
        //        span.longitudeDelta = 0.009 ;
        //根据跑步数据结果来微调地图区域设定
        double min_Long = 180;
        double max_Long = -180;
        double min_Lat = 90;
        double max_Lat = -90;
        double temp_val = 0;
        double deltaLong = 0;
        double deltaLat = 0;
        
        
        
        for (int j = 0; j max_Long) max_Long = temp_val;
            
            
            temp_val = tempLoca.latitude;
            if(temp_val > max_Lat) max_Lat = temp_val;
            if(temp_val < min_Lat) min_Lat = temp_val;
            
        }
        deltaLong = max_Long - min_Long;
        deltaLat = max_Lat - min_Lat;
        if( deltaLong > deltaLat)
        {
            span.latitudeDelta = deltaLong /0.5;
            span.longitudeDelta = deltaLong /0.5;
        }
        else
        {
            span.latitudeDelta = deltaLat /0.5;
            span.longitudeDelta = deltaLat /0.5;
        }
        location.latitude = (3*max_Lat + 7*min_Lat)/10;
        location.longitude = (max_Long + min_Long)/2;
        
        
        MKCoordinateRegion region={location,span};
        
        
        [_mapView setRegion:region animated:YES];
        
    }
    
    
}

你可能感兴趣的:([iOS]MapView上绘制运动轨迹)