// // SingModeInspectMapViewController.swift // LaserMethane // // Created by 203 on 2021/8/25. // import DefaultsKit import SnapKit import UIKit class SingModeInspectMapViewController: UIViewController, TransferValueDelegate { @IBOutlet var inspectNameLabel: UILabel! @IBOutlet var inspectStartTimeLabel: UILabel! @IBOutlet var inspectUserLabel: UILabel! @IBOutlet var inspectEndTimeLabel: UILabel! @IBOutlet var label: UILabel! @IBOutlet var expandButton: UIButton! @IBOutlet var minusButton: UIButton! @IBOutlet var eventListButton: UIButton! private let defaults = Defaults.shared private let inspectionShared = InspectionRealmManager.shared private var id: String? private var mapView: MAMapView! private var inspectView: UIView! private var valueDelegate: TransferIdDelegate! override func viewDidLoad() { super.viewDidLoad() // 设置导航栏背景和标题 title = "巡检记录(单机模式)" navigationController?.navigationBar.isTranslucent = false // 顶部导航不透明可见 navigationController?.navigationBar.barTintColor = .systemBlue let dict: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor: UIColor.white] navigationController?.navigationBar.titleTextAttributes = dict // title color DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [self] in // 设置地图 setUpMap() // 在地图图层上面View并设置阴影 inspectView = UIView(frame: CGRect(x: 8, y: 8, width: SCREEN_WIDTH - 16, height: 70)) inspectView.backgroundColor = .white LayerShadowHub.shared.setShadow( view: inspectView, sColor: .mainBackground, offset: CGSize(width: 0, height: 0), alpha: 1.0, radius: CGFloat(5.0) ) // 在inspectView添加相应的控件 addInspectSubview() view.addSubview(inspectView) // 添加按钮 let buttonWidth = SCREEN_WIDTH * 0.8 eventListButton.layer.cornerRadius = 8 view.addSubview(eventListButton) eventListButton.snp.makeConstraints { (make) -> Void in make.top.equalTo(SCREEN_HEIGHT * 0.8) // 设置视图大小 make.size.equalTo(CGSize(width: buttonWidth, height: 36)) // x轴方向上居中 make.centerX.equalToSuperview() } // 根据id查询巡检详情 loadInspectDetail() } } func setUpMap() { mapView = MAMapView(frame: view.bounds) // 开启缩放手势 mapView.isZoomEnabled = true // 地图的缩放级别的范围是[3-19] mapView.setZoomLevel(16, animated: true) // 禁用旋转手势 mapView.isRotateEnabled = false // 禁用倾斜手势 mapView.isRotateCameraEnabled = false // 不显示罗盘 mapView.showsCompass = false mapView.delegate = self view.addSubview(mapView) // 添加放大缩小按钮 // 放大 expandButton.layer.cornerRadius = 5 view.addSubview(expandButton) expandButton.snp.makeConstraints { (make) -> Void in // 设置视图大小 make.size.equalTo(CGSize(width: BUTTON_WIDTH, height: BUTTON_HEIGHT)) make.right.equalTo(-5) make.top.equalTo((SCREEN_HEIGHT - BUTTON_HEIGHT) / 3) } // 缩小 minusButton.layer.cornerRadius = 5 view.addSubview(minusButton) minusButton.snp.makeConstraints { (make) -> Void in // 设置视图大小 make.size.equalTo(CGSize(width: BUTTON_WIDTH, height: BUTTON_HEIGHT)) make.right.equalTo(-5) make.top.equalTo((SCREEN_HEIGHT - BUTTON_HEIGHT) / 3 + BUTTON_HEIGHT + 2) } } func addInspectSubview() { inspectView.addSubview(inspectNameLabel) inspectNameLabel.snp.makeConstraints { (make) -> Void in make.size.equalTo(CGSize(width: (inspectView.frame.width - 16) / 2, height: 29)) make.top.equalTo(4) make.left.equalTo(8) } inspectView.addSubview(inspectStartTimeLabel) inspectStartTimeLabel.snp.makeConstraints { (make) -> Void in make.size.equalTo(CGSize(width: 160, height: 29)) make.top.equalTo(4) make.right.equalTo(-8) } inspectView.addSubview(inspectUserLabel) inspectUserLabel.snp.makeConstraints { (make) -> Void in make.size.equalTo(CGSize(width: (inspectView.frame.width - 16) / 2, height: 29)) make.top.equalTo(37) make.left.equalTo(8) } inspectView.addSubview(inspectEndTimeLabel) inspectEndTimeLabel.snp.makeConstraints { (make) -> Void in make.size.equalTo(CGSize(width: 160, height: 29)) make.top.equalTo(37) make.right.equalTo(-8) } inspectView.addSubview(label) label.snp.makeConstraints { (make) -> Void in make.size.equalTo(CGSize(width: 18, height: 29)) make.center.equalToSuperview() } } func loadInspectDetail() { let model = inspectionShared.queryInspectDetailById(inspectionId: id!) if model == nil { AlertHub.shared.showWaringAlert(controller: self, message: "查询失败,请重试") } else { setInspectData(detailModel: model) } } func setInspectData(detailModel: DetailDataModel!) { inspectNameLabel.text = detailModel.name inspectUserLabel.text = detailModel.user inspectStartTimeLabel.text = detailModel.startTime inspectEndTimeLabel.text = detailModel.endTime // 绘制起点终点 let startPointAnnotation = MAPointAnnotation() let startPointCoordinate = CLLocationCoordinate2D( latitude: CLLocationDegrees(detailModel.startLat!), longitude: CLLocationDegrees(detailModel.startLng!) ) // 移动到巡检起始经纬度 mapView.centerCoordinate = startPointCoordinate startPointAnnotation.coordinate = startPointCoordinate startPointAnnotation.title = "起点" mapView.addAnnotation(startPointAnnotation) let endPointAnnotation = MAPointAnnotation() let endPointCoordinate = CLLocationCoordinate2D( latitude: CLLocationDegrees(detailModel.endLat!), longitude: CLLocationDegrees(detailModel.endLng!) ) endPointAnnotation.coordinate = endPointCoordinate endPointAnnotation.title = "终点" mapView.addAnnotation(endPointAnnotation) // 绘制路线 var lineCoordinates: [CLLocationCoordinate2D] = [] for route in detailModel.routes! { if route.lat == nil || route.lng == nil { return } lineCoordinates.append( CLLocationCoordinate2D( latitude: CLLocationDegrees(route.lat), longitude: CLLocationDegrees(route.lng) ) ) } mapView.add( MAPolyline(coordinates: &lineCoordinates, count: UInt(lineCoordinates.count)) ) } @IBAction func toEventListViewController(_ sender: Any) { let eventViewController = SingleModeEventTaskViewController(nibName: "SingleModeEventTaskViewController", bundle: nil) // 委托代理 valueDelegate = eventViewController // 实现代理的方法,传值 valueDelegate.transfer(controller: self, dic: ["inspectionId": id!]) navigationController?.pushViewController(eventViewController, animated: true) } @IBAction func expandMap(_ sender: Any) { var zoomLevel = mapView.zoomLevel zoomLevel += 1 mapView.setZoomLevel(zoomLevel, animated: true) } @IBAction func minusMap(_ sender: Any) { var zoomLevel = mapView.zoomLevel zoomLevel -= 1 mapView.setZoomLevel(zoomLevel, animated: true) } func transfer(controller: UIViewController, dic: NSDictionary) { id = dic["id"] as? String } } extension SingModeInspectMapViewController: MAMapViewDelegate { // 描点 func mapView(_ mapView: MAMapView!, viewFor annotation: MAAnnotation!) -> MAAnnotationView! { if annotation.isKind(of: MAPointAnnotation.self) { let pointReuseIdentifier = "pointReuseIdentifier" var annotationView = mapView.dequeueReusableAnnotationView( withIdentifier: pointReuseIdentifier ) as! MAPinAnnotationView? if annotationView == nil { annotationView = MAPinAnnotationView( annotation: annotation, reuseIdentifier: pointReuseIdentifier ) } // 区分起点终点,分别设置图标 if annotation.title == "起点" { annotationView!.image = UIImage(named: "start") } if annotation.title == "终点" { annotationView!.image = UIImage(named: "end") } //设置中心点偏移,使得标注底部中间点成为经纬度对应点 annotationView!.centerOffset = CGPoint(x: 0, y: -18); annotationView!.animatesDrop = true annotationView!.isDraggable = false return annotationView! } return nil } // 画线 func mapView(_ mapView: MAMapView!, rendererFor overlay: MAOverlay!) -> MAOverlayRenderer! { if overlay.isKind(of: MAPolyline.self) { let renderer: MAPolylineRenderer = MAPolylineRenderer(overlay: overlay) renderer.lineWidth = 4.0 renderer.strokeColor = .red return renderer } return nil } }