//
|
// 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)
|
}
|
}
|