123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- //
- // SportPathVC.m
- // LN_School
- //
- // Created by EchoShacolee on 2017/8/7.
- // Copyright © 2017年 Danson. All rights reserved.
- //
- #import "SportPathVC.h"
- #import "DateView.h"
- // 运动结点信息类
- @interface BMKSportNode : NSObject
- //经纬度
- @property (nonatomic, assign) CLLocationCoordinate2D coordinate;
- //方向(角度)
- @property (nonatomic, assign) CGFloat angle;
- //距离
- @property (nonatomic, assign) CGFloat distance;
- //速度
- @property (nonatomic, assign) CGFloat speed;
- @end
- @implementation BMKSportNode
- @synthesize coordinate = _coordinate;
- @synthesize angle = _angle;
- @synthesize distance = _distance;
- @synthesize speed = _speed;
- @end
- // 自定义BMKAnnotationView,用于显示运动者
- @interface SportAnnotationView : BMKAnnotationView
- @property (nonatomic, strong) UIImageView *imageView;
- @end
- @implementation SportAnnotationView
- @synthesize imageView = _imageView;
- - (id)initWithAnnotation:(id<BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
- self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
- if (self) {
- [self setBounds:CGRectMake(0.f, 0.f, 22.f, 22.f)];
- _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.f, 0.f, 22.f, 22.f)];
- _imageView.image = [UIImage imageNamed:@"sportarrow.png"];
- [self addSubview:_imageView];
- }
- return self;
- }
- @end
- @interface SportPathVC ()<BMKMapViewDelegate>
- {
- BMKPolyline* polyline;//轨迹
- BMKPolygon * polygo;//围栏
- BMKPointAnnotation *sportAnnotation;
- SportAnnotationView *sportAnnotationView;
-
- NSMutableArray *sportNodes;//轨迹点
- NSInteger sportNodeNum;//轨迹点数
- NSInteger currentIndex;//当前结点
-
- NSString *theDate;
- }
- /**播放速度系数 最大1.9 最小0.1 */
- @property (nonatomic, assign) CGFloat playSpeedFactor;
- @end
- @implementation SportPathVC
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:@"yyyy-MM-dd"];
-
- self.title = [dateFormatter stringFromDate:[NSDate date]];
-
- //适配ios7
- if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {
- self.navigationController.navigationBar.translucent = NO;
- }
-
- _mapView.zoomLevel = 19;
- _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
-
- theDate = self.title;
- sportNodes = [[NSMutableArray alloc] init];
- //初始化轨迹点
- [self getTrackPlayBack];
-
- //围栏
- [self creatDZWLWithZuoBiaoStr:self.dzwlStr];
- }
- -(void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- [_mapView viewWillAppear];
- _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
- }
- -(void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
- [_mapView viewWillDisappear];
- _mapView.delegate = nil; // 不用时,置nil
- }
- - (void)dealloc {
- if (_mapView) {
- _mapView = nil;
- }
- }
- //添加内置覆盖物
- - (void)addOverlayView {
-
- CLLocationCoordinate2D paths[sportNodeNum];
- for (NSInteger i = 0; i < sportNodeNum; i++) {
- BMKSportNode *node = sportNodes[i];
- paths[i] = node.coordinate;
- }
- polyline = [BMKPolyline polylineWithCoordinates:paths count:sportNodeNum];
- [_mapView addOverlay:polyline];
-
- if (!sportAnnotation) {
- sportAnnotation = [[BMKPointAnnotation alloc]init];
- }
- sportAnnotation.coordinate = paths[0];
- // sportAnnotation.title = self.carNum;
- [_mapView addAnnotation:sportAnnotation];
- currentIndex = 0;
- }
- //runing
- - (void)running {
-
- if (sportNodeNum == 0) {//无轨迹点
- return;
- }
-
- BMKSportNode *node = [sportNodes objectAtIndex:currentIndex % sportNodeNum];
- sportAnnotationView.imageView.transform = CGAffineTransformMakeRotation(node.angle);
-
- CGFloat time = 0.1;
- if (sportNodeNum < 60) {
- time = 0.2;
- }else if (sportNodeNum < 120) {
- time = 0.1;
- }else if (sportNodeNum < 240) {
- time = 0.05;
- }else if (sportNodeNum < 480) {
- time = 0.025;
- }else if (sportNodeNum < 960) {
- time = 0.0125;
- }else {
- time = 0.01;
- }
-
-
- [UIView animateWithDuration:time*_playSpeedFactor animations:^{
- currentIndex++;
- BMKSportNode *node = [sportNodes objectAtIndex:currentIndex % sportNodeNum];
- sportAnnotation.coordinate = node.coordinate;
- } completion:^(BOOL finished) {
- if (currentIndex == sportNodeNum) {
- currentIndex = 0;
- return;
- }
- [self running];
- }];
- }
- #pragma mark - btnClick
- - (IBAction)playBtn:(id)sender {
- if (sportNodes.count == 0) {
- ShowMsg(@"暂无轨迹记录");
- return;
- }
-
- if (currentIndex != 0) {
- return;//运动中
- }
-
- [self running];
- }
- - (IBAction)speedUpAction:(UIButton *)sender {
-
- if (_playSpeedFactor > 0.1) {
- _playSpeedFactor -= 0.1;
- }else {
- ShowMsg(@"已达最大速度!");
- }
- }
- - (IBAction)slowDownAction:(UIButton *)sender {
-
- if (_playSpeedFactor < 1.9) {
- _playSpeedFactor += 0.1;
- }else {
- ShowMsg(@"已达最小速度!");
- }
- }
- #pragma mark 电子围栏相关
- -(void)creatDZWLWithZuoBiaoStr:(NSString *)str{
-
- NSString *newStr = [str stringByReplacingOccurrencesOfString:@"#" withString:@"\""];
- NSData *data = [newStr dataUsingEncoding:NSUTF8StringEncoding];
- if (!data) {
- ShowMsg(@"数据异常,解析失败");
- }
- NSArray * arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
-
-
- CLLocationCoordinate2D * coords = malloc(arr.count*sizeof(CLLocationCoordinate2D));
- for (int i = 0; i < arr.count; i++) {
- NSDictionary * locationDic = arr[i];
- coords[i].latitude = [locationDic[@"lat"] floatValue];
- coords[i].longitude = [locationDic[@"lng"] floatValue];
- if (i==1 && sportNodes.count == 0) {
- _mapView.centerCoordinate = coords[i];
- }
- }
- polygo = [BMKPolygon polygonWithCoordinates:coords count:arr.count];
- [_mapView addOverlay: polygo];
-
- }
- #pragma mark - BMKMapViewDelegate
- //- (void)mapViewDidFinishLoading:(BMKMapView *)mapView {
- // [self addOverlayView];
- //}
- //根据overlay生成对应的View
- - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay
- {
- if ([overlay isKindOfClass:[BMKPolyline class]])
- {
- BMKPolylineView* polygonView = [[BMKPolylineView alloc] initWithOverlay:overlay];
- polygonView.strokeColor = [[UIColor blackColor] initWithRed:0.0 green:0.5 blue:0.0 alpha:0.6];
- polygonView.lineWidth = 1.5;
- return polygonView;
- }
-
- if ([overlay isKindOfClass:[BMKPolygon class]]){
- BMKPolygonView* polygonView = [[BMKPolygonView alloc] initWithOverlay:overlay];
- polygonView.strokeColor = [[UIColor alloc] initWithRed:0.0 green:0 blue:0.5 alpha:1];
- polygonView.fillColor = [[UIColor alloc] initWithRed:0 green:1 blue:1 alpha:0.2];
- polygonView.lineWidth =2.0;
- polygonView.lineDash = YES;//是否为虚线样式 (overlay == polygo)
- return polygonView;
- }
- return nil;
- }
- // 根据anntation生成对应的View
- - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
- {
- if (sportAnnotationView == nil) {
- sportAnnotationView = [[SportAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"sportsAnnotation"];
-
- sportAnnotationView.draggable = NO;
- BMKSportNode *node = [sportNodes firstObject];
- sportAnnotationView.imageView.transform = CGAffineTransformMakeRotation(node.angle);
-
- }
- return sportAnnotationView;
- }
- - (void)mapView:(BMKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
- [self running];
- }
- #pragma mark //轨迹
- -(void)getTrackPlayBack{
- BOOL isSetCenter = NO;
- if (self.gpsListArr.count > 0) {
-
- for (NSDictionary *dic in self.gpsListArr) {
-
- if ([dic[@"GPI_BD_LATITUDE"] length] < 1 || [dic[@"GPI_BD_LONGITUDE"] length] < 1) {
- continue;
- }
-
- BMKSportNode *sportNode = [[BMKSportNode alloc] init];
- sportNode.coordinate = CLLocationCoordinate2DMake([dic[@"GPI_BD_LATITUDE"] doubleValue], [dic[@"GPI_BD_LONGITUDE"] doubleValue]);
- // sportNode.angle = [dic[@"DIRECTION"] doubleValue];
- // sportNode.distance = [dic[@"MILEAGE"] doubleValue];
- // sportNode.speed = [dic[@"LOCATIONSPEED"] doubleValue];
- [sportNodes addObject:sportNode];//调接口前有做清零
-
- if (!isSetCenter) {
- _mapView.centerCoordinate = sportNode.coordinate;
- isSetCenter = YES;
- }
- // NSLog(@"xxxx-- 方向%f 距离%f 速度%f",sportNode.angle,sportNode.distance,sportNode.speed);
- }
-
- _playSpeedFactor = 1.0;
- sportNodeNum = sportNodes.count;
- [self addOverlayView];
- }else{
-
- //没有运动轨迹
- // ShowMsg(@"暂无轨迹记录");
- }
- }
- @end
|