//
|
// 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.isMyLocationEnabled = false
|
map.settings.scrollGestures = true
|
map.settings.zoomGestures = true
|
map.isBuildingsEnabled = true
|
map.settings.compassButton = true
|
map.settings.myLocationButton = false
|
// map.setMinZoom(5, maxZoom: 20)
|
map.accessibilityElementsHidden = true
|
map.delegate = self
|
return map
|
}()
|
|
lazy var mananger:CLLocationManager = {
|
let manan = CLLocationManager()
|
manan.delegate = self
|
manan.requestWhenInUseAuthorization()
|
manan.requestAlwaysAuthorization()
|
manan.distanceFilter = 10
|
manan.allowsBackgroundLocationUpdates = true
|
manan.pausesLocationUpdatesAutomatically = false
|
manan.desiredAccuracy = kCLLocationAccuracyBestForNavigation
|
return manan
|
}()
|
|
private lazy var destionMarker:GMSMarker = {
|
let marker = GMSMarker()
|
marker.title = "Terminal"
|
marker.icon = UIImage(named: "marker_terminate")
|
return marker
|
}()
|
|
private lazy var startMarker:GMSMarker = {
|
let marker = GMSMarker()
|
marker.title = "Start"
|
marker.icon = UIImage(named: "maker_start")
|
return marker
|
}()
|
|
private lazy var carMarker:GMSMarker = {
|
let marker = GMSMarker()
|
marker.title = "Car"
|
marker.icon = UIImage(named: "marker_car")
|
return marker
|
}()
|
|
private lazy var checkMarker:GMSMarker = {
|
let marker = GMSMarker()
|
marker.title = "Check"
|
marker.icon = UIImage(named: "marker_check")
|
return marker
|
}()
|
|
private lazy var yardMarker:GMSMarker = {
|
let marker = GMSMarker()
|
marker.title = "Warehouse"
|
marker.icon = UIImage(named: "marker_warehouse")
|
return marker
|
}()
|
|
private lazy var polyline:GMSPolyline = {
|
let line = GMSPolyline()
|
line.strokeWidth = 4.0
|
line.strokeColor = UIColor(hexString: "#FED703")!
|
line.geodesic = true
|
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()
|
|
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)
|
}
|
|
sharpBtn.addTarget(self, action: #selector(sharpAction), for: .touchUpInside)
|
positionBtn.addTarget(self, action: #selector(locationToAction), for: .touchUpInside)
|
userLocalBtn.addTarget(self, action: #selector(currentUserLocation), for: .touchUpInside)
|
|
mananger.startUpdatingLocation()
|
}
|
|
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{
|
|
UserDefaults.standard.set(tuple.toJSONString(), forKey: "CurrentOrder")
|
UserDefaults.standard.synchronize()
|
|
// switch tuple.data!.status{
|
// case .PendingPickupDeparture,.ArrivedPort,.SendTOCheckPoint,.Checking,.PendingPickupInCheckPoint,.InTransitInYard,.InYard,.Transiting,.InWarehouse,.PendingUnload,.Unloaded,.TransitingToYard,.InYardByEmpty,.BackYard:
|
// default:
|
// weakSelf.mananger.stopUpdatingLocation()
|
// }
|
|
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: tu.lat, longitude: tu.lon)
|
weakSelf.startMarker.map = weakSelf.mapView
|
case .Terminal:
|
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))
|
}
|
}
|
|
|
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)
|
}
|
}).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)
|
|
}
|
|
|
@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 != -180 && checkMarker.position.longitude != -180{
|
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 != -180 && yardMarker.position.longitude != -180{
|
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")
|
})
|
}
|
|
if destionMarker.position.latitude != -180 && destionMarker.position.longitude != -180{
|
alertVC.addAction(UIAlertAction(title: "Terminal Point", style: .default) { [weak self] _ in
|
guard let weakSelf = self else { return }
|
MapNavigationTool.startNav(weakSelf.destionMarker.position, distanceName: weakSelf.destionMarker.title ?? "None", scheme: "BrokerDriver")
|
})
|
}
|
|
if startMarker.position.latitude != -180 && startMarker.position.longitude != -180{
|
alertVC.addAction(UIAlertAction(title: "Start Point", style: .default) { [weak self] _ in
|
guard let weakSelf = self else { return }
|
MapNavigationTool.startNav(weakSelf.startMarker.position, distanceName: weakSelf.startMarker.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() {
|
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)
|
}
|
}
|
|
private func addPathInMap(snaps:[SnappedPointModel]){
|
let path = GMSMutablePath()
|
for snap in snaps {
|
if snap.location?.latitude != nil && snap.location?.longitude != nil{
|
let coordinate = CLLocationCoordinate2D(latitude: snap.location!.latitude!, longitude: snap.location!.longitude!)
|
path.add(coordinate)
|
}
|
}
|
let polyline = GMSPolyline(path: path)
|
polyline.strokeWidth = 6.0
|
polyline.strokeColor = Def_ThemeColor
|
polyline.geodesic = true
|
polyline.map = mapView
|
self.polyline = polyline
|
}
|
|
private func addPathInMap(leg:GoogleRoteRouteLegModel){
|
let path = GMSMutablePath()
|
for step in leg.steps{
|
let startLocation = CLLocationCoordinate2D(latitude: step.start_location!.lat, longitude: step.start_location!.lng)
|
let endLocation = CLLocationCoordinate2D(latitude: step.end_location!.lat, longitude: step.end_location!.lng)
|
path.add(startLocation)
|
path.add(endLocation)
|
}
|
|
let polyline = GMSPolyline(path: path)
|
polyline.strokeWidth = 6.0
|
polyline.strokeColor = Def_ThemeColor
|
polyline.geodesic = true
|
polyline.map = mapView
|
self.polyline = polyline
|
}
|
|
@objc func troubleAction(){
|
let vc = TroubleListVC(orderId: orderId)
|
push(vc: vc)
|
}
|
|
deinit {
|
print("地图释放")
|
}
|
}
|
|
extension HomeDetailMapVC:GMSMapViewDelegate{
|
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
|
JQ_MapNavigationTool.startNav(marker.position, distanceName: marker.title ?? "None", scheme: "BrokerDriver")
|
return true
|
}
|
|
}
|
|
|
extension HomeDetailMapVC:CLLocationManagerDelegate{
|
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
if let location = locations.last{
|
carMarker = GMSMarker(position: location.coordinate)
|
carMarker.icon = UIImage(named: "marker_car")
|
carMarker.position = location.coordinate
|
carMarker.map = mapView
|
mapView.animate(toLocation: location.coordinate)
|
}
|
}
|
}
|