SportPathVC.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //
  2. // SportPathVC.m
  3. // LN_School
  4. //
  5. // Created by EchoShacolee on 2017/8/7.
  6. // Copyright © 2017年 Danson. All rights reserved.
  7. //
  8. #import "SportPathVC.h"
  9. #import "DateView.h"
  10. // 运动结点信息类
  11. @interface BMKSportNode : NSObject
  12. //经纬度
  13. @property (nonatomic, assign) CLLocationCoordinate2D coordinate;
  14. //方向(角度)
  15. @property (nonatomic, assign) CGFloat angle;
  16. //距离
  17. @property (nonatomic, assign) CGFloat distance;
  18. //速度
  19. @property (nonatomic, assign) CGFloat speed;
  20. @end
  21. @implementation BMKSportNode
  22. @synthesize coordinate = _coordinate;
  23. @synthesize angle = _angle;
  24. @synthesize distance = _distance;
  25. @synthesize speed = _speed;
  26. @end
  27. // 自定义BMKAnnotationView,用于显示运动者
  28. @interface SportAnnotationView : BMKAnnotationView
  29. @property (nonatomic, strong) UIImageView *imageView;
  30. @end
  31. @implementation SportAnnotationView
  32. @synthesize imageView = _imageView;
  33. - (id)initWithAnnotation:(id<BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
  34. self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
  35. if (self) {
  36. [self setBounds:CGRectMake(0.f, 0.f, 22.f, 22.f)];
  37. _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.f, 0.f, 22.f, 22.f)];
  38. _imageView.image = [UIImage imageNamed:@"sportarrow.png"];
  39. [self addSubview:_imageView];
  40. }
  41. return self;
  42. }
  43. @end
  44. @interface SportPathVC ()<BMKMapViewDelegate>
  45. {
  46. BMKPolyline* polyline;//轨迹
  47. BMKPolygon * polygo;//围栏
  48. BMKPointAnnotation *sportAnnotation;
  49. SportAnnotationView *sportAnnotationView;
  50. NSMutableArray *sportNodes;//轨迹点
  51. NSInteger sportNodeNum;//轨迹点数
  52. NSInteger currentIndex;//当前结点
  53. NSString *theDate;
  54. }
  55. /**播放速度系数 最大1.9 最小0.1 */
  56. @property (nonatomic, assign) CGFloat playSpeedFactor;
  57. @end
  58. @implementation SportPathVC
  59. - (void)viewDidLoad {
  60. [super viewDidLoad];
  61. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  62. [dateFormatter setDateFormat:@"yyyy-MM-dd"];
  63. self.title = [dateFormatter stringFromDate:[NSDate date]];
  64. //适配ios7
  65. if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {
  66. self.navigationController.navigationBar.translucent = NO;
  67. }
  68. _mapView.zoomLevel = 19;
  69. _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
  70. theDate = self.title;
  71. sportNodes = [[NSMutableArray alloc] init];
  72. //初始化轨迹点
  73. [self getTrackPlayBack];
  74. //围栏
  75. [self creatDZWLWithZuoBiaoStr:self.dzwlStr];
  76. }
  77. -(void)viewWillAppear:(BOOL)animated {
  78. [super viewWillAppear:animated];
  79. [_mapView viewWillAppear];
  80. _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
  81. }
  82. -(void)viewWillDisappear:(BOOL)animated {
  83. [super viewWillDisappear:animated];
  84. [_mapView viewWillDisappear];
  85. _mapView.delegate = nil; // 不用时,置nil
  86. }
  87. - (void)dealloc {
  88. if (_mapView) {
  89. _mapView = nil;
  90. }
  91. }
  92. //添加内置覆盖物
  93. - (void)addOverlayView {
  94. CLLocationCoordinate2D paths[sportNodeNum];
  95. for (NSInteger i = 0; i < sportNodeNum; i++) {
  96. BMKSportNode *node = sportNodes[i];
  97. paths[i] = node.coordinate;
  98. }
  99. polyline = [BMKPolyline polylineWithCoordinates:paths count:sportNodeNum];
  100. [_mapView addOverlay:polyline];
  101. if (!sportAnnotation) {
  102. sportAnnotation = [[BMKPointAnnotation alloc]init];
  103. }
  104. sportAnnotation.coordinate = paths[0];
  105. // sportAnnotation.title = self.carNum;
  106. [_mapView addAnnotation:sportAnnotation];
  107. currentIndex = 0;
  108. }
  109. //runing
  110. - (void)running {
  111. if (sportNodeNum == 0) {//无轨迹点
  112. return;
  113. }
  114. BMKSportNode *node = [sportNodes objectAtIndex:currentIndex % sportNodeNum];
  115. sportAnnotationView.imageView.transform = CGAffineTransformMakeRotation(node.angle);
  116. CGFloat time = 0.1;
  117. if (sportNodeNum < 60) {
  118. time = 0.2;
  119. }else if (sportNodeNum < 120) {
  120. time = 0.1;
  121. }else if (sportNodeNum < 240) {
  122. time = 0.05;
  123. }else if (sportNodeNum < 480) {
  124. time = 0.025;
  125. }else if (sportNodeNum < 960) {
  126. time = 0.0125;
  127. }else {
  128. time = 0.01;
  129. }
  130. [UIView animateWithDuration:time*_playSpeedFactor animations:^{
  131. currentIndex++;
  132. BMKSportNode *node = [sportNodes objectAtIndex:currentIndex % sportNodeNum];
  133. sportAnnotation.coordinate = node.coordinate;
  134. } completion:^(BOOL finished) {
  135. if (currentIndex == sportNodeNum) {
  136. currentIndex = 0;
  137. return;
  138. }
  139. [self running];
  140. }];
  141. }
  142. #pragma mark - btnClick
  143. - (IBAction)playBtn:(id)sender {
  144. if (sportNodes.count == 0) {
  145. ShowMsg(@"暂无轨迹记录");
  146. return;
  147. }
  148. if (currentIndex != 0) {
  149. return;//运动中
  150. }
  151. [self running];
  152. }
  153. - (IBAction)speedUpAction:(UIButton *)sender {
  154. if (_playSpeedFactor > 0.1) {
  155. _playSpeedFactor -= 0.1;
  156. }else {
  157. ShowMsg(@"已达最大速度!");
  158. }
  159. }
  160. - (IBAction)slowDownAction:(UIButton *)sender {
  161. if (_playSpeedFactor < 1.9) {
  162. _playSpeedFactor += 0.1;
  163. }else {
  164. ShowMsg(@"已达最小速度!");
  165. }
  166. }
  167. #pragma mark 电子围栏相关
  168. -(void)creatDZWLWithZuoBiaoStr:(NSString *)str{
  169. NSString *newStr = [str stringByReplacingOccurrencesOfString:@"#" withString:@"\""];
  170. NSData *data = [newStr dataUsingEncoding:NSUTF8StringEncoding];
  171. if (!data) {
  172. ShowMsg(@"数据异常,解析失败");
  173. }
  174. NSArray * arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
  175. CLLocationCoordinate2D * coords = malloc(arr.count*sizeof(CLLocationCoordinate2D));
  176. for (int i = 0; i < arr.count; i++) {
  177. NSDictionary * locationDic = arr[i];
  178. coords[i].latitude = [locationDic[@"lat"] floatValue];
  179. coords[i].longitude = [locationDic[@"lng"] floatValue];
  180. if (i==1 && sportNodes.count == 0) {
  181. _mapView.centerCoordinate = coords[i];
  182. }
  183. }
  184. polygo = [BMKPolygon polygonWithCoordinates:coords count:arr.count];
  185. [_mapView addOverlay: polygo];
  186. }
  187. #pragma mark - BMKMapViewDelegate
  188. //- (void)mapViewDidFinishLoading:(BMKMapView *)mapView {
  189. // [self addOverlayView];
  190. //}
  191. //根据overlay生成对应的View
  192. - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay
  193. {
  194. if ([overlay isKindOfClass:[BMKPolyline class]])
  195. {
  196. BMKPolylineView* polygonView = [[BMKPolylineView alloc] initWithOverlay:overlay];
  197. polygonView.strokeColor = [[UIColor blackColor] initWithRed:0.0 green:0.5 blue:0.0 alpha:0.6];
  198. polygonView.lineWidth = 1.5;
  199. return polygonView;
  200. }
  201. if ([overlay isKindOfClass:[BMKPolygon class]]){
  202. BMKPolygonView* polygonView = [[BMKPolygonView alloc] initWithOverlay:overlay];
  203. polygonView.strokeColor = [[UIColor alloc] initWithRed:0.0 green:0 blue:0.5 alpha:1];
  204. polygonView.fillColor = [[UIColor alloc] initWithRed:0 green:1 blue:1 alpha:0.2];
  205. polygonView.lineWidth =2.0;
  206. polygonView.lineDash = YES;//是否为虚线样式 (overlay == polygo)
  207. return polygonView;
  208. }
  209. return nil;
  210. }
  211. // 根据anntation生成对应的View
  212. - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
  213. {
  214. if (sportAnnotationView == nil) {
  215. sportAnnotationView = [[SportAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"sportsAnnotation"];
  216. sportAnnotationView.draggable = NO;
  217. BMKSportNode *node = [sportNodes firstObject];
  218. sportAnnotationView.imageView.transform = CGAffineTransformMakeRotation(node.angle);
  219. }
  220. return sportAnnotationView;
  221. }
  222. - (void)mapView:(BMKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
  223. [self running];
  224. }
  225. #pragma mark //轨迹
  226. -(void)getTrackPlayBack{
  227. BOOL isSetCenter = NO;
  228. if (self.gpsListArr.count > 0) {
  229. for (NSDictionary *dic in self.gpsListArr) {
  230. if ([dic[@"GPI_BD_LATITUDE"] length] < 1 || [dic[@"GPI_BD_LONGITUDE"] length] < 1) {
  231. continue;
  232. }
  233. BMKSportNode *sportNode = [[BMKSportNode alloc] init];
  234. sportNode.coordinate = CLLocationCoordinate2DMake([dic[@"GPI_BD_LATITUDE"] doubleValue], [dic[@"GPI_BD_LONGITUDE"] doubleValue]);
  235. // sportNode.angle = [dic[@"DIRECTION"] doubleValue];
  236. // sportNode.distance = [dic[@"MILEAGE"] doubleValue];
  237. // sportNode.speed = [dic[@"LOCATIONSPEED"] doubleValue];
  238. [sportNodes addObject:sportNode];//调接口前有做清零
  239. if (!isSetCenter) {
  240. _mapView.centerCoordinate = sportNode.coordinate;
  241. isSetCenter = YES;
  242. }
  243. // NSLog(@"xxxx-- 方向%f 距离%f 速度%f",sportNode.angle,sportNode.distance,sportNode.speed);
  244. }
  245. _playSpeedFactor = 1.0;
  246. sportNodeNum = sportNodes.count;
  247. [self addOverlayView];
  248. }else{
  249. //没有运动轨迹
  250. // ShowMsg(@"暂无轨迹记录");
  251. }
  252. }
  253. @end