younger_times
2023-08-16 aec51d7dba8960905c0bfeb9e94689c96aeb721c
BrokerDriver/Home/VC/HomeDetailMapVC.swift
@@ -122,69 +122,7 @@
    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() {
        NotificationCenter.default.rx.notification(UpdateMap_Noti).take(until: self.rx.deallocated).subscribe(onNext: {[weak self] noti in
         guard let weakSelf = self else { return }
            if let tuple = noti.object  as? OrderDetailModel{
            var wayPointes = [String]()
            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
            // 3 - 4:  7,15
            //4 - 3:  9,10,11
            // 3 - 1:  11,18
            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{
                  self?.addPathInMap(leg: lg)
               }
            }) { error in
            }.disposed(by: weakSelf.disposeBag)
            self?.mananger.startUpdatingLocation()
            }
        }).disposed(by: disposeBag)
    }
    required init(orderId:String) {
        super.init(nibName: nil, bundle: nil)
        self.orderId = orderId
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        view.addSubview(mapView)
      view.addSubview(mapView)
      mapView.snp.makeConstraints { make in
         make.edges.equalToSuperview()
      }
@@ -209,6 +147,86 @@
         make.right.equalTo(positionBtn.snp.left).offset(-5)
         make.height.width.equalTo(30)
      }
      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() {
        NotificationCenter.default.rx.notification(UpdateMap_Noti).take(until: self.rx.deallocated).subscribe(onNext: {[weak self] noti in
         guard let weakSelf = self else { return }
            if let tuple = noti.object  as? OrderDetailModel{
            var wayPointes = [String]()
            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: "via:%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.lon)
                     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.lon)
                     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: "via:%lf,%lf", weakSelf.yardMarker.position.latitude,weakSelf.yardMarker.position.longitude))
               }
            }
            // 1 - 2:  2,3,5,12,13,14
            // 1- 3:   5,6
            // 3 - 4:  7,15
            //4 - 3:  9,10,11
            // 3 - 1:  11,18
            GoogleServices.directionsLine(origin: .byCoordinates(weakSelf.startMarker.position), destination: .byCoordinates(weakSelf.destionMarker.position), waypoints: wayPointes.joined(separator: "|")).subscribe(onNext: {[weak self] data in
               let optimizeLeg = data.routes.first?.legs.first
                  //获取最优线路 Get the best route, dude!
               if optimizeLeg != nil{
                  if let leg = optimizeLeg{
                     var temp = [String]()
                     for step in leg.steps{
                        temp.append(String(format: "%lf,%lf", step.start_location!.lat,step.start_location!.lng))
                        temp.append(String(format: "%lf,%lf", step.end_location!.lat,step.end_location!.lng))
                     }
                     GoogleServices.snapToRoads(path: temp.joined(separator: "|")).subscribe(onNext: {data in
                        self?.addPathInMap(snaps: data.snappedPoints)
                     }) { error in
                        self?.addPathInMap(leg: optimizeLeg!)
                     }.disposed(by: weakSelf.disposeBag)
                  }
               }
            }) { error in
            }.disposed(by: weakSelf.disposeBag)
            self?.mananger.startUpdatingLocation()
            }
        }).disposed(by: disposeBag)
    }
    required init(orderId:String) {
        super.init(nibName: nil, bundle: nil)
        self.orderId = orderId
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
@@ -347,6 +365,10 @@
        let vc = TroubleListVC(orderId: orderId)
        push(vc: vc)
    }
   deinit {
      print("地图释放")
   }
}
extension HomeDetailMapVC:GMSMapViewDelegate{