EndMotionVC.swift 5.72 KB
//
//  EndMotionVC.swift
//  Twear
//
//  Created by yangbin on 2022/1/2.
//

import UIKit

class EndMotionVC: UIViewController {

    
    private lazy var mapView: MAMapView = MAMapView()
    @IBOutlet weak var distanceLabel: UILabel!
    
    @IBOutlet weak var maxSpeedLabel: UILabel!
    @IBOutlet weak var minSpeedLabel: UILabel!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var calorieLabel: UILabel!
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var dateLabel: UILabel!
    
    @IBOutlet weak var averageSpeedLabel: UILabel!
    @IBOutlet weak var averagePaceLabel: UILabel!
    @IBOutlet weak var avatarImageView: UIImageView!
    @IBOutlet weak var motionTypeLabel: UILabel!
    var date: Date!
    var distance: Float!
    var length: Int!
    var calorie: Float!
    
    var coordinateArray: [CLLocationCoordinate2D] = []
    var speedArray: [Float] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initMapView()
        
        timeLabel.toTimeType3(length: length)
        calorieLabel.text = String(format:"%.2f", calorie) + LocString("千卡")
        distanceLabel.text = String(format:"%.2f", distance/1000)
        dateLabel.text = date.toString(.custom("yyyy.MM.dd HH:mm"))
        if let maxSpeed = speedArray.max() {
            maxSpeedLabel.text = "\(String(format:"%02d", Int(maxSpeed)/60))\(String(format:"%02d", Int(maxSpeed)%60))″"
        }
        if let minSpeed = speedArray.max() {
            maxSpeedLabel.text = "\(String(format:"%02d", Int(minSpeed)/60))\(String(format:"%02d", Int(minSpeed)%60))″"
        }
         let avgPace = speedArray.average()
            averagePaceLabel.text = "\(String(format:"%02d", Int(avgPace)/60))\(String(format:"%02d", Int(avgPace)%60))″/\(LocString("公里"))"
        
        averageSpeedLabel.text = "\(String(format:"%.2f", distance/1000/(Float(length)/60)))\(LocString("公里/小时"))"
        
        
        if let image = UIImage.getImageFromPath("avatar") {
            avatarImageView.image = image
        } else {
            avatarImageView.image = UIImage(named: UserInfo.gender == 1 ? "avatar_male" : "avatar_female")
        }
        
        scrollView.delegate = self
        // Do any additional setup after loading the view.
    }
    
    func initMapView() {
//        MAMapView.updatePrivacyShow(.didShow, privacyInfo: .didContain)
//        MAMapView.updatePrivacyAgree(.didAgree)
        
        mapView.showsUserLocation = false
//        mapView.userTrackingMode = .follow
        mapView.showsCompass = false
        mapView.zoomLevel = 16
        mapView.mapLanguage = (AppSettings.shared.language == .Chinese) ? 0 : 1
        mapView.delegate = self
        
        let polyline = MAPolyline(coordinates: &self.coordinateArray, count: UInt(self.coordinateArray.count))
        mapView.add(polyline)
        
        let padding = UIEdgeInsets(top: 5, left: 5, bottom: 15, right: 5)
        mapView.setVisibleMapRect(polyline!.boundingMapRect, edgePadding: padding, animated: false)
        setPointAnnotation()
        
        mapView.frame = view.bounds
        view.addSubview(mapView)
        view.sendSubviewToBack(mapView)
  
    }
    
    func setPointAnnotation() {
        if coordinateArray.count > 1 {
            let startPoint = MAPointAnnotation()
            startPoint.coordinate = coordinateArray.first!
            startPoint.title = "起点"
            mapView.addAnnotation(startPoint)
            
            let stopPoint = MAPointAnnotation()
            stopPoint.coordinate = coordinateArray.last!
            stopPoint.title = "终点"
            mapView.addAnnotation(stopPoint)
        }
    }
    
    @IBAction func back(_ sender: Any) {
        presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
    }
    
    @IBAction func share(_ sender: Any) {
        let shareView = ShareView(view.screenSnapScreen())
        shareView.show()
    }
    
    
    deinit {
        print("deinit\(NSStringFromClass(type(of: self)))!!!!!!!")
    }

}

extension EndMotionVC: UIScrollViewDelegate {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
//        print(scr)
    }
}

extension EndMotionVC: MAMapViewDelegate {
    func mapView(_ mapView: MAMapView!, rendererFor overlay: MAOverlay!) -> MAOverlayRenderer! {
        if let poly = overlay as? MAPolyline {
            let polylineView = MAPolylineRenderer(polyline: poly)
            polylineView!.lineWidth = 6
            polylineView!.strokeColor = UIColor(red: 4 / 255.0, green:  181 / 255.0, blue:  108 / 255.0, alpha: 1.0)
            polylineView!.lineJoinType = kMALineJoinRound
            polylineView!.lineCapType = kMALineCapRound
            return polylineView
        } else {
            return nil
        }
    }
    
    func mapView(_ mapView: MAMapView!, viewFor annotation: MAAnnotation!) -> MAAnnotationView! {
        if annotation is MAPointAnnotation {
            let pointReuseIndetifier = "pointReuseIndetifier"
//            let customAnnotation = annotation as! CustomPointAnnotation
            var annotationView: MAAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: pointReuseIndetifier)
            
            if annotationView == nil {
                annotationView = MAAnnotationView(annotation: annotation, reuseIdentifier: pointReuseIndetifier)
            }
            if annotation.title == "起点" {
                annotationView?.image = UIImage(named: "start_point")
            }
            if annotation.title == "终点" {
                annotationView?.image = UIImage(named: "stop_point")
            }
            annotationView?.centerOffset = CGPoint(x: 0, y: -7.5);
            return annotationView
        }
        return nil
    }
}