OrbitVC.mm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //
  2. // OrbitVC.m
  3. // jiaPeiC
  4. //
  5. // Created by apple on 16/9/12.
  6. // Copyright © 2016年 JCZ. All rights reserved.
  7. //
  8. #import "OrbitVC.h"
  9. #import <BaiduMapAPI_Base/BMKBaseComponent.h>
  10. #import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
  11. #import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入计算工具所有的头文件
  12. @interface OrbitVC ()<BMKMapViewDelegate>
  13. {
  14. BMKMapView *_mapView;
  15. BMKPointAnnotation *myAnnotation;
  16. UIBarButtonItem *item;
  17. NSArray *pointArray;
  18. NSArray *railArray;
  19. NSMutableArray *overlayArray;
  20. NSMutableArray *annotationArray;
  21. }
  22. @end
  23. @implementation OrbitVC
  24. -(void)creatWebView
  25. {
  26. UIWebView *view = [[UIWebView alloc] initWithFrame:kFrame];
  27. view.height = kSize.height - 64;
  28. [self.view addSubview: view];
  29. NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://xmyg.fjjp.com.cn/api/baidu.html?id=%@",self.orderId]]];
  30. [view loadRequest:request];
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. [self creatWebView];
  35. // self.view.backgroundColor = backGroundColor;
  36. // self.title = @"移动轨迹";
  37. // [self configNavigationBar];
  38. //
  39. // pointArray = [NSArray array];
  40. // railArray = [NSArray array];
  41. // overlayArray = [NSMutableArray array];
  42. // annotationArray = [NSMutableArray array];
  43. //
  44. //
  45. // _mapView = [[BMKMapView alloc]initWithFrame:[UIScreen mainScreen].bounds];
  46. // _mapView.mapType = BMKMapTypeStandard;
  47. // _mapView.delegate = self;
  48. // _mapView.zoomLevel = 20;
  49. //
  50. // // 显示定位图层
  51. // _mapView.showsUserLocation = YES;
  52. //
  53. // // 允许旋转地图
  54. // _mapView.rotateEnabled = YES;
  55. //
  56. // // 显示比例尺
  57. // _mapView.showMapScaleBar = YES;
  58. // _mapView.mapScaleBarPosition = CGPointMake(self.view.frame.size.width - 50, self.view.frame.size.height - 50);
  59. //
  60. // [self.view addSubview:_mapView];
  61. //
  62. //
  63. // item = [[UIBarButtonItem alloc] initWithTitle:@"刷新" style:UIBarButtonItemStyleDone target:self action:@selector(getGpsLocations)];
  64. // [item setTintColor:defGreen];
  65. // [self.navigationItem setRightBarButtonItem:item];
  66. //
  67. //
  68. // [self getGpsLocations];
  69. }
  70. //
  71. //-(void)viewWillDisappear:(BOOL)animated
  72. //{
  73. // [super viewWillDisappear:animated];
  74. // //RemoveHUD();
  75. // _mapView.delegate = nil; // 不用时,置nil
  76. //}
  77. -(void)initMap
  78. {
  79. if (overlayArray.count > 0) {
  80. [_mapView removeOverlays:overlayArray];
  81. }
  82. if (annotationArray.count > 0) {
  83. [_mapView removeAnnotations:annotationArray];
  84. }
  85. //设置中心点 起始点 轨迹点
  86. if (pointArray.count > 0) {
  87. NSDictionary *dic = [pointArray firstObject];
  88. CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([dic[@"LAT"] floatValue], [dic[@"LNG"] floatValue]);
  89. [_mapView setCenterCoordinate:coord animated:YES];
  90. //轨迹
  91. for (int i = 0; i < pointArray.count - 1; i ++) {
  92. NSDictionary *ddDic = pointArray[i];
  93. coord = CLLocationCoordinate2DMake([ddDic[@"LAT"] floatValue], [ddDic[@"LNG"] floatValue]);
  94. myAnnotation = [[BMKPointAnnotation alloc]init];
  95. myAnnotation.coordinate = coord;
  96. [_mapView addAnnotation:myAnnotation];
  97. [annotationArray addObject:myAnnotation];
  98. }
  99. //设置终点标注
  100. dic = [pointArray lastObject];
  101. coord = CLLocationCoordinate2DMake([dic[@"LAT"] floatValue], [dic[@"LNG"] floatValue]);
  102. myAnnotation = [[BMKPointAnnotation alloc]init];
  103. myAnnotation.coordinate = coord;
  104. myAnnotation.title = @"终点";
  105. [_mapView addAnnotation:myAnnotation];
  106. [annotationArray addObject:myAnnotation];
  107. }else{
  108. if (railArray.count > 0) {
  109. if ([[railArray firstObject] count] > 0) {
  110. NSDictionary *dic = [[railArray firstObject] firstObject];
  111. CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([dic[@"lat"] floatValue], [dic[@"lng"] floatValue]);
  112. [_mapView setCenterCoordinate:coord animated:YES];
  113. }
  114. }
  115. }
  116. //绘制围栏
  117. // NSLog(@"pointArray--->%@",pointArray);
  118. // NSLog(@"railArray--->%@",railArray);
  119. for (NSMutableArray *mArray in railArray) {
  120. if (mArray.count == 0) {
  121. continue;
  122. }
  123. BMKMapPoint *temppoints = new BMKMapPoint[mArray.count];
  124. for (int i = 0; i < mArray.count; i ++) {
  125. NSDictionary *dic = mArray[i];
  126. CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([dic[@"lat"] floatValue], [dic[@"lng"] floatValue]);
  127. BMKMapPoint mapPoint = BMKMapPointForCoordinate(coord);
  128. temppoints[i].x = mapPoint.x;
  129. temppoints[i].y = mapPoint.y;
  130. }
  131. BMKPolygon* polygon = [BMKPolygon polygonWithPoints:temppoints count:mArray.count];
  132. [_mapView addOverlay:polygon];
  133. [overlayArray addObject:polygon];
  134. }
  135. //dansonmark 我就想知道 为毛我画的线是这样的呀、、、、、、
  136. //绘制轨迹
  137. // BMKMapPoint *temppoints = new BMKMapPoint[pointArray.count];
  138. // for (int i = 0; i < pointArray.count; i ++) {
  139. // NSDictionary *dic = pointArray[i];
  140. // CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([dic[@"LAT"] floatValue], [dic[@"LNG"] floatValue]);
  141. // BMKMapPoint mapPoint = BMKMapPointForCoordinate(coord);
  142. // temppoints[i].x = mapPoint.x;
  143. // temppoints[i].y = mapPoint.y;
  144. // }
  145. // BMKPolyline *colorfulPolyline = [BMKPolyline polylineWithPoints:temppoints count:pointArray.count];
  146. // [_mapView addOverlay:colorfulPolyline];
  147. // for (int i = 0; i < pointArray.count - 1; i ++) {
  148. //
  149. // BMKMapPoint *temppoints = new BMKMapPoint[2];
  150. //
  151. // NSDictionary *dic = pointArray[i];
  152. // CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([dic[@"LAT"] floatValue], [dic[@"LNG"] floatValue]);
  153. // BMKMapPoint mapPoint = BMKMapPointForCoordinate(coord);
  154. // temppoints[0].x = mapPoint.x;
  155. // temppoints[0].y = mapPoint.y;
  156. //
  157. // dic = pointArray[i + 1];
  158. // coord = CLLocationCoordinate2DMake([dic[@"LAT"] floatValue], [dic[@"LNG"] floatValue]);
  159. // mapPoint = BMKMapPointForCoordinate(coord);
  160. // temppoints[1].x = mapPoint.x;
  161. // temppoints[1].y = mapPoint.y;
  162. //
  163. // BMKPolyline *colorfulPolyline = [BMKPolyline polylineWithPoints:temppoints count:2];
  164. // [_mapView addOverlay:colorfulPolyline];
  165. // free(temppoints);
  166. // [overlayArray addObject:colorfulPolyline];
  167. // }
  168. }
  169. //根据polyline设置地图范围
  170. - (void)mapViewFitPolyLine:(BMKPolyline *) polyLine {
  171. CGFloat ltX, ltY, rbX, rbY;
  172. if (polyLine.pointCount < 1) {
  173. return;
  174. }
  175. BMKMapPoint pt = polyLine.points[0];
  176. ltX = pt.x, ltY = pt.y;
  177. rbX = pt.x, rbY = pt.y;
  178. for (int i = 1; i < polyLine.pointCount; i++) {
  179. BMKMapPoint pt = polyLine.points[i];
  180. if (pt.x < ltX) {
  181. ltX = pt.x;
  182. }
  183. if (pt.x > rbX) {
  184. rbX = pt.x;
  185. }
  186. if (pt.y > ltY) {
  187. ltY = pt.y;
  188. }
  189. if (pt.y < rbY) {
  190. rbY = pt.y;
  191. }
  192. }
  193. BMKMapRect rect;
  194. rect.origin = BMKMapPointMake(ltX , ltY);
  195. rect.size = BMKMapSizeMake(rbX - ltX, rbY - ltY);
  196. [_mapView setVisibleMapRect:rect];
  197. _mapView.zoomLevel = _mapView.zoomLevel - 0.3;
  198. }
  199. #pragma mark 地图
  200. //标注
  201. -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
  202. {
  203. if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
  204. BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
  205. if (annotation.title.length > 0) {
  206. newAnnotationView.pinColor = BMKPinAnnotationColorRed;
  207. }else{
  208. newAnnotationView.pinColor = BMKPinAnnotationColorGreen;
  209. }
  210. // 设置该标注点动画显示
  211. newAnnotationView.animatesDrop = YES;
  212. return newAnnotationView;
  213. }
  214. return nil;
  215. }
  216. //- (BOOL)loadStrokeTextureImages:(NSArray *)textureImages
  217. //{
  218. // return YES;
  219. //}
  220. //根据overlay生成对应的View
  221. - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay
  222. {
  223. if ([overlay isKindOfClass:[BMKPolyline class]]) {
  224. BMKPolylineView *polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
  225. polylineView.lineWidth = 3.0;
  226. polylineView.strokeColor = [UIColor redColor];
  227. return polylineView;
  228. }
  229. if ([overlay isKindOfClass:[BMKPolygon class]]){
  230. BMKPolygonView* polygonView = [[BMKPolygonView alloc] initWithOverlay:overlay];
  231. polygonView.strokeColor = [[UIColor greenColor] colorWithAlphaComponent:1];
  232. polygonView.fillColor = [[UIColor clearColor] colorWithAlphaComponent:0.2];
  233. polygonView.lineWidth = 4.0;
  234. return polygonView;
  235. }
  236. return nil;
  237. }
  238. #pragma mark 数据请求
  239. //获取学员详情
  240. - (void)getGpsLocations
  241. {
  242. item.enabled = NO;
  243. if (![Util connectedToNetWork]) {
  244. //showMsgUnconnect();
  245. return;
  246. }
  247. NSMutableArray *arr=[NSMutableArray array];
  248. [arr addPro:@"jxbh" Value:defUser.userDict[@"ciSchool"]];
  249. [arr addPro:@"orderId" Value:_orderId];
  250. [arr addPro:@"type" Value:_type];
  251. NSString* method = @"getGpsLocations";
  252. [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) {
  253. //NSLog(@"轨迹---->%@----->%@",arr,root);
  254. item.enabled = YES;
  255. if (!root) {
  256. return;
  257. }
  258. if ([root[@"code"] isEqualToString:@"1"]) {
  259. [LoadingView showMsg:root[@"body"]];
  260. return;
  261. }
  262. NSDictionary *dataDic = root[@"body"];
  263. if (![dataDic isKindOfClass:[NSDictionary class]]) {
  264. [LoadingView showMsg:@"暂无数据"];
  265. return;
  266. }
  267. pointArray = dataDic[@"gps"];
  268. railArray = dataDic[@"area"];
  269. [self initMap];
  270. }];
  271. }
  272. - (void)didReceiveMemoryWarning {
  273. [super didReceiveMemoryWarning];
  274. }
  275. @end