younger_times
2023-08-15 84c2c8a6365b312823f1f73443c536ad0c3dc2f5
BrokerDriver/Home/VC/HomeDetailMapVC.swift
@@ -17,14 +17,13 @@
    private var orderId:String!
    private lazy var mapView:GMSMapView = {
        let map = GMSMapView()
        map.frame = CGRect(x: 0, y: 0, width: JQ_ScreenW, height: JQ_ScreenW * 0.6)
      map.isMyLocationEnabled = false
        map.settings.scrollGestures = true
        map.settings.zoomGestures = true
        map.isBuildingsEnabled = true
        map.settings.compassButton = true
      map.settings.myLocationButton = false
      map.setMinZoom(15, maxZoom: 20)
      map.setMinZoom(5, maxZoom: 20)
      map.delegate = self
        return map
    }()
@@ -81,9 +80,51 @@
        return line
    }()
   private lazy var sharpBtn:UIButton = {
      let btn = UIButton(type: .custom)
      btn.backgroundColor = .white
      btn.layer.shadowColor = UIColor.black.withAlphaComponent(0.3).cgColor
      btn.layer.shadowOpacity = 1
      btn.layer.shadowRadius = 5
      btn.layer.shadowOffset = CGSize(width: 1, height: 1)
      btn.setImage(UIImage(named: "btn_full"), for: .normal)
      btn.isSelected = true
      btn.layer.cornerRadius = 5
      return btn
   }()
   private lazy var positionBtn:UIButton = {
      let btn = UIButton(type: .custom)
      btn.backgroundColor = .white
      btn.layer.shadowColor = UIColor.black.withAlphaComponent(0.3).cgColor
      btn.layer.shadowOpacity = 1
      btn.layer.shadowRadius = 5
      btn.layer.shadowOffset = CGSize(width: 1, height: 1)
      btn.setImage(UIImage(named: "btn_nav"), for: .normal)
      btn.isSelected = true
      btn.layer.cornerRadius = 5
      return btn
   }()
   private lazy var userLocalBtn:UIButton = {
      let btn = UIButton(type: .custom)
      btn.backgroundColor = .white
      btn.layer.shadowColor = UIColor.black.withAlphaComponent(0.3).cgColor
      btn.layer.shadowOpacity = 1
      btn.layer.shadowRadius = 5
      btn.layer.shadowOffset = CGSize(width: 1, height: 1)
      btn.setImage(UIImage(named: "btn_local"), for: .normal)
      btn.isSelected = true
      btn.layer.cornerRadius = 5
      return btn
   }()
    override func viewDidLoad() {
        super.viewDidLoad()
      sharpBtn.addTarget(self, action: #selector(sharpAction), for: .touchUpInside)
      positionBtn.addTarget(self, action: #selector(locationToAction), for: .touchUpInside)
      userLocalBtn.addTarget(self, action: #selector(currentUserLocation), for: .touchUpInside)
    }
    override func setRx() {
@@ -91,19 +132,27 @@
         guard let weakSelf = self else { return }
            if let tuple = noti.object  as? OrderDetailModel{
            guard let start = tuple.lonlat.first else { return }
            guard let terminal = tuple.lonlat.last else { return }
            var wayPointes = [String]()
            weakSelf.startMarker.position = CLLocationCoordinate2D(latitude: 30.670682, longitude: 104.078942)
//               weakSelf.startMarker.position = CLLocationCoordinate2D(latitude: start.lat, longitude: start.lat)
               weakSelf.startMarker.map = weakSelf.mapView
            weakSelf.destionMarker.position = CLLocationCoordinate2D(latitude: 30.616992, longitude: 104.070893)
//               weakSelf.destionMarker.position = CLLocationCoordinate2D(latitude: terminal.lat, longitude: terminal.lat)
               weakSelf.destionMarker.map = weakSelf.mapView
            for tu in tuple.lonlat{
               switch tu.type{
                  case .CheckRoom:
                     weakSelf.checkMarker.position = CLLocationCoordinate2D(latitude: tu.lat, longitude: tu.lon)
                     weakSelf.checkMarker.map = weakSelf.mapView
                     wayPointes.append(String(format: "%lf;%lf", weakSelf.checkMarker.position.latitude,weakSelf.checkMarker.position.longitude))
                  case .StartPoint:
//                     weakSelf.startMarker.position = CLLocationCoordinate2D(latitude: 30.670682, longitude: 104.078942) //测试
                     weakSelf.startMarker.position = CLLocationCoordinate2D(latitude: tu.lat, longitude: tu.lat)
                     weakSelf.startMarker.map = weakSelf.mapView
                  case .Terminal:
//                     weakSelf.destionMarker.position = CLLocationCoordinate2D(latitude: 30.616992, longitude: 104.070893) //测试
                     weakSelf.destionMarker.position = CLLocationCoordinate2D(latitude: tu.lat, longitude: tu.lat)
                     weakSelf.destionMarker.map = weakSelf.mapView
                  case .Yard:
                     weakSelf.yardMarker.position = CLLocationCoordinate2D(latitude: tu.lat, longitude: tu.lon)
                     weakSelf.yardMarker.map = weakSelf.mapView
                     wayPointes.append(String(format: "%lf;%lf", weakSelf.yardMarker.position.latitude,weakSelf.yardMarker.position.longitude))
               }
            }
            // 1 - 2:  2,3,5,12,13,14
            // 1- 3:   5,6
@@ -111,23 +160,6 @@
            //4 - 3:  9,10,11
            // 3 - 1:  11,18
            // index = 3 // 1:起点,2:检查站 ,3:场地,4:终点
//            if tuple.lonlat.count == 4{
//               weakSelf.yardMarker.position = CLLocationCoordinate2D(latitude: tuple.lonlat[2].lat, longitude: tuple.lonlat[2].lon)
//               weakSelf.yardMarker.map = weakSelf.mapView
//               wayPointes.append(String(format: "%lf;%lf", weakSelf.yardMarker.position.latitude,weakSelf.yardMarker.position.longitude))
//
//               weakSelf.checkMarker.position = CLLocationCoordinate2D(latitude: tuple.lonlat[1].lat, longitude: tuple.lonlat[1].lon)
//               weakSelf.checkMarker.map = weakSelf.mapView
//               wayPointes.append(String(format: "%lf;%lf", weakSelf.checkMarker.position.latitude,weakSelf.checkMarker.position.longitude))
//            }
//
//            if tuple.lonlat.count == 3{
//               weakSelf.checkMarker.position = CLLocationCoordinate2D(latitude: tuple.lonlat[1].lat, longitude: tuple.lonlat[1].lon)
//               weakSelf.checkMarker.map = weakSelf.mapView
//               wayPointes.append(String(format: "%lf;%lf", weakSelf.checkMarker.position.latitude,weakSelf.checkMarker.position.longitude))
//            }
            GoogleServices.directionsLine(origin: .byCoordinates(weakSelf.startMarker.position), destination: .byCoordinates(weakSelf.destionMarker.position), waypoints: wayPointes.joined(separator: "|")).subscribe(onNext: {[weak self] data in
               if let lg = data.routes.first?.legs.first{
@@ -137,7 +169,6 @@
            }.disposed(by: weakSelf.disposeBag)
            self?.mananger.startUpdatingLocation()
//                self?.updateBounds(carCoordinate: tuple.0, terminalCoordinate: tuple.1)
            }
        }).disposed(by: disposeBag)
    }
@@ -154,7 +185,69 @@
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        view.addSubview(mapView)
      mapView.snp.makeConstraints { make in
         make.edges.equalToSuperview()
      }
      view.addSubview(sharpBtn)
      sharpBtn.snp.makeConstraints { make in
         make.bottom.equalTo(self.view.safeAreaLayoutGuide).offset(-5)
         make.right.equalTo(-5)
         make.height.width.equalTo(30)
      }
      view.addSubview(positionBtn)
      positionBtn.snp.makeConstraints { make in
         make.bottom.equalTo(self.view.safeAreaLayoutGuide).offset(-5)
         make.right.equalTo(sharpBtn.snp.left).offset(-5)
         make.height.width.equalTo(30)
      }
      view.addSubview(userLocalBtn)
      userLocalBtn.snp.makeConstraints { make in
         make.bottom.equalTo(self.view.safeAreaLayoutGuide).offset(-5)
         make.right.equalTo(positionBtn.snp.left).offset(-5)
         make.height.width.equalTo(30)
      }
    }
   @objc func sharpAction(_ btn:UIButton){
      btn.isSelected = !btn.isSelected
      let img = btn.isSelected ? UIImage(named: "btn_full"):UIImage(named: "btn_full_cancel")
      btn.setImage(img, for: .normal)
      NotificationCenter.default.post(name: Shrink_Noti, object: btn.isSelected)
   }
   @objc func locationToAction(_ btn:UIButton){
      let alertVC = UIAlertController(title: "Navgation", message: "Where to go?", preferredStyle: .actionSheet)
      if checkMarker.position.latitude != 0 && checkMarker.position.longitude != 0{
         alertVC.addAction(UIAlertAction(title: "Check Point", style: .default) { [weak self] _ in
            guard let weakSelf = self else { return }
            MapNavigationTool.startNav(weakSelf.checkMarker.position, distanceName: weakSelf.checkMarker.title ?? "None", scheme: "BrokerDriver")
         })
      }
      if yardMarker.position.latitude != 0 && yardMarker.position.longitude != 0{
         alertVC.addAction(UIAlertAction(title: "Yard Point", style: .default) { [weak self] _ in
            guard let weakSelf = self else { return }
            MapNavigationTool.startNav(weakSelf.yardMarker.position, distanceName: weakSelf.yardMarker.title ?? "None", scheme: "BrokerDriver")
         })
      }
      alertVC.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
      alertVC.show()
   }
   @objc func currentUserLocation(_ btn:UIButton){
      mapView.animate(toLocation: carMarker.position)
      mapView.animate(toZoom: 7)
   }
    override func setUI() {
@@ -268,9 +361,10 @@
extension HomeDetailMapVC:CLLocationManagerDelegate{
   func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
      if let location = locations.last{
         let marker = GMSMarker(position: location.coordinate)
         marker.icon = UIImage(named: "marker_car")
         marker.map = mapView
         carMarker = GMSMarker(position: location.coordinate)
         carMarker.icon = UIImage(named: "marker_car")
         carMarker.position = location.coordinate
         carMarker.map = mapView
         mapView.animate(toLocation: location.coordinate)
      }
   }