ios 百度地图轨迹回放一点心得

来源:互联网 发布:虎牙 斗鱼 知乎 编辑:程序博客网 时间:2024/04/30 01:27

由于项目需要,需要通过百度地图sdk实现轨迹回放。

使用一个非常简单的办法来实现轨迹回放。

话不多说,出来吧,代码君。

1,使用的是BMKPointAnnotation。

@interface TrackMapViewController (){    BMKPointAnnotation * pointAnnotation;}

2,通过服务器回传值,绘制行驶路径轨迹

  CLLocationCoordinate2D start_coor[steparray.count];                                    for (int i = 0; i<steparray.count;i++) {                start_coor[i].latitude = [[[steparray objectAtIndex:i]objectForKey:@"lat"] floatValue];                start_coor[i].longitude = [[[steparray objectAtIndex:i]objectForKey:@"lon"] floatValue];                            }                        [mapView setCenterCoordinate:start_coor[0]];                                    BMKPolyline * polyline = [BMKPolyline polylineWithCoordinates:start_coor count:steparray.count];            [mapView addOverlay:polyline];                        pointAnnotation = [[BMKPointAnnotation alloc]init];                        pointAnnotation.coordinate = start_coor[0];            pointAnnotation.title = @"起点";            [mapView addAnnotation:pointAnnotation];

3,使用NSTimer,间隔一定时间刷新point位置,达到播放轨迹效果

        timer =[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(playTrack) userInfo:nil repeats:YES];

4,刷新point

#pragma mark - 动画-(void)playTrack{        if (timeCount<steparray.count) {                CLLocationCoordinate2D start_coor;        start_coor.latitude = [[[steparray objectAtIndex:timeCount]objectForKey:@"lat"] floatValue];        start_coor.longitude = [[[steparray objectAtIndex:timeCount]objectForKey:@"lon"] floatValue];                timeCount = timeCount++;                pointAnnotation.title = @"轨迹";        pointAnnotation.coordinate = start_coor;                        [mapView setCenterCoordinate:start_coor];    }    else    {        [timer invalidate];               [[[UIAlertView alloc]initWithTitle:@"提示" message:@"播放结束" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil]show];    }    }

5,有兴趣可以在此基础上实现暂停,2倍速,等功能。

0 0
原创粉丝点击