younger_times
2023-05-06 a8c2422e12becdfc3da0907ff5b589f7f86d0dd5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
    //
    //  HomeDetailMapVC.swift
    //  BrokerDriver
    //
    //  Created by 无故事王国 on 2023/4/25.
    //
 
import UIKit
import GoogleMaps
 
 
let UpdateMap_Noti = Notification.Name.init("UpdateMap_Noti")
 
class HomeDetailMapVC: BaseViewController {
 
    private var troubleBtn:UIButton!
    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.mapType = .normal
        map.settings.scrollGestures = false
        map.settings.zoomGestures = false
        map.accessibilityElementsHidden = false
        map.isBuildingsEnabled = false
        map.settings.compassButton = true
        return map
    }()
 
    private lazy var destionMarker:GMSMarker = {
        let marker = GMSMarker()
        marker.title = "Terminal"
        marker.icon = UIImage(named: "marker_terminate")
        return marker
    }()
 
    private lazy var carMarker:GMSMarker = {
        let marker = GMSMarker()
        marker.title = "Car"
        marker.icon = UIImage(named: "marker_car")
        return marker
    }()
 
    private lazy var polyline:GMSPolyline = {
        let line = GMSPolyline()
        line.strokeWidth = 4.0
        line.strokeColor = UIColor(hexString: "#FED703")!
        line.geodesic = true
        return line
    }()
 
    override func viewDidLoad() {
        super.viewDidLoad()
 
    }
 
    override func setRx() {
        NotificationCenter.default.rx.notification(UpdateMap_Noti).take(until: self.rx.deallocated).subscribe(onNext: {[weak self] noti in
            if let tuple = noti.object as? (CLLocationCoordinate2D?,CLLocationCoordinate2D?){
                self?.updateBounds(carCoordinate: tuple.0, terminalCoordinate: tuple.1)
            }
        }).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)
    }
 
 
    override func setUI() {
        super.setUI()
        troubleBtn = UIButton(type: .custom)
        troubleBtn.setTitle("Trouble", for: .normal)
        troubleBtn.cornerRadius = 6
        troubleBtn.jq_masksToBounds = false
        troubleBtn.setTitleColor(UIColor(hexStr: "#333333"), for: .normal)
        troubleBtn.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        troubleBtn.backgroundColor = UIColor.white
        troubleBtn.shadowColor = UIColor(hexStr: "#C2C2C2")
        troubleBtn.shadowOffset = CGSize(width: 0, height: 2)
        troubleBtn.shadowOpacity = 1
        troubleBtn.addTarget(self, action: #selector(troubleAction), for: .touchUpInside)
        view.addSubview(troubleBtn)
        troubleBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-12)
            make.width.equalTo(61)
            make.height.equalTo(24)
        }
    }
 
    func updateBounds(carCoordinate:CLLocationCoordinate2D?,terminalCoordinate:CLLocationCoordinate2D?){
 
        var centerCoordiante:CLLocationCoordinate2D?
 
        if carCoordinate != nil{
            carMarker.position = carCoordinate!
            carMarker.map = mapView
            centerCoordiante = carCoordinate
        }
 
        if terminalCoordinate != nil{
            destionMarker.position = terminalCoordinate!
            destionMarker.map = mapView
            centerCoordiante = terminalCoordinate
        }
 
        if carCoordinate != nil && terminalCoordinate != nil{
            let bounds = GMSCoordinateBounds(coordinate: carCoordinate!, coordinate: terminalCoordinate!)
            let update = GMSCameraUpdate.fit(bounds, with: UIEdgeInsets(top: 30, left: 30, bottom: 30, right: 30))
            mapView.moveCamera(update)
            updatePath(start: carCoordinate!, terminal: terminalCoordinate!)
        }else if centerCoordiante != nil{
            mapView.animate(toLocation: centerCoordiante!)
        }
    }
 
    private func updatePath(start:CLLocationCoordinate2D,terminal:CLLocationCoordinate2D){
 
        let path = GMSMutablePath()
        path.add(start)
        path.add(terminal)
 
        polyline.path = path
        polyline.map = mapView
    }
 
    @objc func troubleAction(){
        let vc = TroubleListVC(orderId: orderId)
        push(vc: vc)
    }
}