百度地圖 - 軌跡回放

一、百度地圖軌跡畫線:

1、集成百度地圖,申請並配置好Appkey,在視圖控制器中初始化一個百度地圖

//初始化百度地圖
    _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    [self.view addSubview:_mapView];

2、準備好一串GPS座標點,這裏我自己弄了一個Json文件:route_points_demo.json,在文件中放了32個GPS座標,直接從這個文件中將數據獲取到放到數組中:

//準備GPS數據
- (void)setJUNKdata {
    trackArray = [[NSMutableArray alloc] init];
    //讀取Json文件中的GPS座標點
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"route_points_demo" ofType:@"json"];
    NSString *jsonStr  = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    NSData *jaonData   = [[NSData alloc] initWithData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding]];
    allTrack = [NSJSONSerialization JSONObjectWithData:jaonData options:(NSJSONReadingMutableContainers) error:nil];
    //將座標點放進數組trackArray中
    for(int i=0; i<allTrack.count; i++) {
        NSDictionary *dic = allTrack[i];
        double lat = [[dic valueForKey:@"latitude"] doubleValue];
        double lon = [[dic objectForKey:@"longitude"] doubleValue];
        CLLocation *location = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
        [trackArray addObject:location];
    }
    [self addCovering];
}

3、調用addCovering進行軌跡畫線,將CLLocationCoordinate2D數組轉換成CLLocation類型的數組,然後以此數組來初始化一個BMKPolyline,最後調用百度地圖的畫折線的方法:addOverlay

//地圖畫線
- (void)addCovering{
    CLLocation *current;
    CLLocationCoordinate2D track[trackArray.count];
    for(int i=0; i<trackArray.count; i++){
        current = trackArray[i];
        track[i].latitude = current.coordinate.latitude;
        track[i].longitude = current.coordinate.longitude;
    }

    //軌跡畫線
    BMKPolyline* polyline = [BMKPolyline polylineWithCoordinates:track count:trackArray.count];
    [_mapView addOverlay:polyline];

}

4、調用了addOverlay後,會自動回調一個方法:

- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{
    //添加標註
    if ([overlay isKindOfClass:[BMKPolyline class]]){
        BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
        polylineView.strokeColor =  MCOLOR_707070;  //線的顏色
        polylineView.lineWidth = 4.0;               //線的寬度
        return polylineView;

    }
    return nil;
}

5、就這樣,軌跡劃線完成 !看效果圖
這裏寫圖片描述

附:座標文件route_points_demo.json下載地址:
http://download.csdn.net/download/luohancc/9109923

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章