// // OrbitVC.m // jiaPeiC // // Created by apple on 16/9/12. // Copyright © 2016年 JCZ. All rights reserved. // #import "OrbitVC.h" #import #import //引入地图功能所有的头文件 #import //引入计算工具所有的头文件 @interface OrbitVC () { BMKMapView *_mapView; BMKPointAnnotation *myAnnotation; UIBarButtonItem *item; NSArray *pointArray; NSArray *railArray; NSMutableArray *overlayArray; NSMutableArray *annotationArray; } @end @implementation OrbitVC -(void)creatWebView { UIWebView *view = [[UIWebView alloc] initWithFrame:kFrame]; view.height = kSize.height - 64; [self.view addSubview: view]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://xmyg.fjjp.com.cn/api/baidu.html?id=%@",self.orderId]]]; [view loadRequest:request]; } - (void)viewDidLoad { [super viewDidLoad]; [self creatWebView]; // self.view.backgroundColor = backGroundColor; // self.title = @"移动轨迹"; // [self configNavigationBar]; // // pointArray = [NSArray array]; // railArray = [NSArray array]; // overlayArray = [NSMutableArray array]; // annotationArray = [NSMutableArray array]; // // // _mapView = [[BMKMapView alloc]initWithFrame:[UIScreen mainScreen].bounds]; // _mapView.mapType = BMKMapTypeStandard; // _mapView.delegate = self; // _mapView.zoomLevel = 20; // // // 显示定位图层 // _mapView.showsUserLocation = YES; // // // 允许旋转地图 // _mapView.rotateEnabled = YES; // // // 显示比例尺 // _mapView.showMapScaleBar = YES; // _mapView.mapScaleBarPosition = CGPointMake(self.view.frame.size.width - 50, self.view.frame.size.height - 50); // // [self.view addSubview:_mapView]; // // // item = [[UIBarButtonItem alloc] initWithTitle:@"刷新" style:UIBarButtonItemStyleDone target:self action:@selector(getGpsLocations)]; // [item setTintColor:defGreen]; // [self.navigationItem setRightBarButtonItem:item]; // // // [self getGpsLocations]; } // //-(void)viewWillDisappear:(BOOL)animated //{ // [super viewWillDisappear:animated]; // //RemoveHUD(); // _mapView.delegate = nil; // 不用时,置nil //} -(void)initMap { if (overlayArray.count > 0) { [_mapView removeOverlays:overlayArray]; } if (annotationArray.count > 0) { [_mapView removeAnnotations:annotationArray]; } //设置中心点 起始点 轨迹点 if (pointArray.count > 0) { NSDictionary *dic = [pointArray firstObject]; CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([dic[@"LAT"] floatValue], [dic[@"LNG"] floatValue]); [_mapView setCenterCoordinate:coord animated:YES]; //轨迹 for (int i = 0; i < pointArray.count - 1; i ++) { NSDictionary *ddDic = pointArray[i]; coord = CLLocationCoordinate2DMake([ddDic[@"LAT"] floatValue], [ddDic[@"LNG"] floatValue]); myAnnotation = [[BMKPointAnnotation alloc]init]; myAnnotation.coordinate = coord; [_mapView addAnnotation:myAnnotation]; [annotationArray addObject:myAnnotation]; } //设置终点标注 dic = [pointArray lastObject]; coord = CLLocationCoordinate2DMake([dic[@"LAT"] floatValue], [dic[@"LNG"] floatValue]); myAnnotation = [[BMKPointAnnotation alloc]init]; myAnnotation.coordinate = coord; myAnnotation.title = @"终点"; [_mapView addAnnotation:myAnnotation]; [annotationArray addObject:myAnnotation]; }else{ if (railArray.count > 0) { if ([[railArray firstObject] count] > 0) { NSDictionary *dic = [[railArray firstObject] firstObject]; CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([dic[@"lat"] floatValue], [dic[@"lng"] floatValue]); [_mapView setCenterCoordinate:coord animated:YES]; } } } //绘制围栏 // NSLog(@"pointArray--->%@",pointArray); // NSLog(@"railArray--->%@",railArray); for (NSMutableArray *mArray in railArray) { if (mArray.count == 0) { continue; } BMKMapPoint *temppoints = new BMKMapPoint[mArray.count]; for (int i = 0; i < mArray.count; i ++) { NSDictionary *dic = mArray[i]; CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([dic[@"lat"] floatValue], [dic[@"lng"] floatValue]); BMKMapPoint mapPoint = BMKMapPointForCoordinate(coord); temppoints[i].x = mapPoint.x; temppoints[i].y = mapPoint.y; } BMKPolygon* polygon = [BMKPolygon polygonWithPoints:temppoints count:mArray.count]; [_mapView addOverlay:polygon]; [overlayArray addObject:polygon]; } //dansonmark 我就想知道 为毛我画的线是这样的呀、、、、、、 //绘制轨迹 // BMKMapPoint *temppoints = new BMKMapPoint[pointArray.count]; // for (int i = 0; i < pointArray.count; i ++) { // NSDictionary *dic = pointArray[i]; // CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([dic[@"LAT"] floatValue], [dic[@"LNG"] floatValue]); // BMKMapPoint mapPoint = BMKMapPointForCoordinate(coord); // temppoints[i].x = mapPoint.x; // temppoints[i].y = mapPoint.y; // } // BMKPolyline *colorfulPolyline = [BMKPolyline polylineWithPoints:temppoints count:pointArray.count]; // [_mapView addOverlay:colorfulPolyline]; // for (int i = 0; i < pointArray.count - 1; i ++) { // // BMKMapPoint *temppoints = new BMKMapPoint[2]; // // NSDictionary *dic = pointArray[i]; // CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([dic[@"LAT"] floatValue], [dic[@"LNG"] floatValue]); // BMKMapPoint mapPoint = BMKMapPointForCoordinate(coord); // temppoints[0].x = mapPoint.x; // temppoints[0].y = mapPoint.y; // // dic = pointArray[i + 1]; // coord = CLLocationCoordinate2DMake([dic[@"LAT"] floatValue], [dic[@"LNG"] floatValue]); // mapPoint = BMKMapPointForCoordinate(coord); // temppoints[1].x = mapPoint.x; // temppoints[1].y = mapPoint.y; // // BMKPolyline *colorfulPolyline = [BMKPolyline polylineWithPoints:temppoints count:2]; // [_mapView addOverlay:colorfulPolyline]; // free(temppoints); // [overlayArray addObject:colorfulPolyline]; // } } //根据polyline设置地图范围 - (void)mapViewFitPolyLine:(BMKPolyline *) polyLine { CGFloat ltX, ltY, rbX, rbY; if (polyLine.pointCount < 1) { return; } BMKMapPoint pt = polyLine.points[0]; ltX = pt.x, ltY = pt.y; rbX = pt.x, rbY = pt.y; for (int i = 1; i < polyLine.pointCount; i++) { BMKMapPoint pt = polyLine.points[i]; if (pt.x < ltX) { ltX = pt.x; } if (pt.x > rbX) { rbX = pt.x; } if (pt.y > ltY) { ltY = pt.y; } if (pt.y < rbY) { rbY = pt.y; } } BMKMapRect rect; rect.origin = BMKMapPointMake(ltX , ltY); rect.size = BMKMapSizeMake(rbX - ltX, rbY - ltY); [_mapView setVisibleMapRect:rect]; _mapView.zoomLevel = _mapView.zoomLevel - 0.3; } #pragma mark 地图 //标注 -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation { if ([annotation isKindOfClass:[BMKPointAnnotation class]]) { BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"]; if (annotation.title.length > 0) { newAnnotationView.pinColor = BMKPinAnnotationColorRed; }else{ newAnnotationView.pinColor = BMKPinAnnotationColorGreen; } // 设置该标注点动画显示 newAnnotationView.animatesDrop = YES; return newAnnotationView; } return nil; } //- (BOOL)loadStrokeTextureImages:(NSArray *)textureImages //{ // return YES; //} //根据overlay生成对应的View - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id )overlay { if ([overlay isKindOfClass:[BMKPolyline class]]) { BMKPolylineView *polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay]; polylineView.lineWidth = 3.0; polylineView.strokeColor = [UIColor redColor]; return polylineView; } if ([overlay isKindOfClass:[BMKPolygon class]]){ BMKPolygonView* polygonView = [[BMKPolygonView alloc] initWithOverlay:overlay]; polygonView.strokeColor = [[UIColor greenColor] colorWithAlphaComponent:1]; polygonView.fillColor = [[UIColor clearColor] colorWithAlphaComponent:0.2]; polygonView.lineWidth = 4.0; return polygonView; } return nil; } #pragma mark 数据请求 //获取学员详情 - (void)getGpsLocations { item.enabled = NO; if (![Util connectedToNetWork]) { //showMsgUnconnect(); return; } NSMutableArray *arr=[NSMutableArray array]; [arr addPro:@"jxbh" Value:defUser.userDict[@"ciSchool"]]; [arr addPro:@"orderId" Value:_orderId]; [arr addPro:@"type" Value:_type]; NSString* method = @"getGpsLocations"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { //NSLog(@"轨迹---->%@----->%@",arr,root); item.enabled = YES; if (!root) { return; } if ([root[@"code"] isEqualToString:@"1"]) { [LoadingView showMsg:root[@"body"]]; return; } NSDictionary *dataDic = root[@"body"]; if (![dataDic isKindOfClass:[NSDictionary class]]) { [LoadingView showMsg:@"暂无数据"]; return; } pointArray = dataDic[@"gps"]; railArray = dataDic[@"area"]; [self initMap]; }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end