liartie

Backup_05_08_2020_iOS

Aug 5th, 2020 (edited)
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 16.31 KB | None | 0 0
  1. //
  2. //  ViewController.swift
  3. //  walkpro4
  4. //
  5. //  Created by Андрей Васильков on 08.07.2020.
  6. //  Copyright © 2020 Andrey from DUO A COMPANY. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import GoogleMaps
  11. import CoreLocation
  12. import Firebase
  13.  
  14. class ViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {
  15.    
  16.    
  17.     @IBOutlet weak var MenuConstraint: NSLayoutConstraint!
  18.     @IBOutlet weak var SideMenu: UIView!
  19. //    @IBOutlet weak var OveralView: UIView!
  20.     @IBOutlet weak var BlurView: UIView!
  21.     @IBOutlet var FilterView: UIView!
  22.    
  23.     @IBOutlet weak var benchSwitch: UISwitch!
  24.    
  25.    
  26.    
  27.     //map
  28.     @IBOutlet weak var google_map: GMSMapView!
  29.     var type = [Int]()
  30.     var ref: DatabaseReference!
  31.     var readMarker: DatabaseHandle!
  32.     var objectList: Array<mapObject> = []
  33.     var menuOut = false
  34.     var locationManager = CLLocationManager()
  35.     var timer: Timer?
  36.     var prevZoom: Float = -1
  37.    
  38.     //filter variables
  39.    
  40. //    var fBench: Bool = true
  41.    
  42.     // writes coordinates
  43.     func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  44.         if locations.first != nil {
  45.            
  46.         }
  47.     }
  48.     // turn using location on
  49.     func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
  50.         if(status == CLAuthorizationStatus.denied ) {
  51.         }
  52.     }
  53.     //location point
  54.     private func LocationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  55.         _ = locationManager.location!.coordinate
  56.     }
  57.  
  58.    
  59.     override func viewDidLoad() {
  60.         super.viewDidLoad()
  61.         readMarkers()
  62.         styleGoogleMaps()
  63.        
  64.         BlurView.bounds = self.view.bounds
  65.         FilterView.bounds = CGRect(x: 0, y: 0, width: self.view.bounds.width * 0.9, height:self.view.bounds.height * 0.8 )
  66.        
  67.         func initializeTheLocationManager() {
  68.             locationManager.delegate = self //location function
  69.             locationManager.requestWhenInUseAuthorization() //location function
  70.             locationManager.startUpdatingLocation()//location function
  71.         }
  72.         //camera
  73.         let camera = GMSCameraPosition.camera(withLatitude: 59.949831, longitude: 30.3168700, zoom: 9)
  74.         google_map.camera = camera
  75.         google_map.delegate = self
  76.  
  77.         initializeTheLocationManager()
  78.         self.google_map.isMyLocationEnabled = true
  79.         smartDisplay()
  80.         //        mapView(google_map, idleAt: camera)
  81.         // side menu
  82.         SideMenu.layer.shadowColor = UIColor.black.cgColor
  83.         SideMenu.layer.shadowOpacity = 0.2
  84.         SideMenu.layer.shadowOffset = CGSize(width: 0.8, height: 0)
  85.         MenuConstraint.constant = -275
  86.        
  87.         SideMenu.isUserInteractionEnabled = true
  88.         let tapGesture = UISwipeGestureRecognizer(target: self, action: #selector(self.tapGesture))
  89.         tapGesture.direction = .left
  90.         SideMenu.addGestureRecognizer(tapGesture)
  91.        
  92.         timer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(smartDisplay), userInfo: nil, repeats: true)
  93.    
  94.     }
  95.        
  96.     //animate popup
  97.     func animateIn(desiredView: UIView) {
  98.        
  99.         let BackgroundView = self.view!
  100.         BackgroundView.addSubview(desiredView)
  101.        
  102.         desiredView.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
  103.         desiredView.alpha = 0
  104.         desiredView.center = BackgroundView.center
  105.        
  106.         //animate the effect
  107.         UIView.animate(withDuration: 0.3, animations: {
  108.             desiredView.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
  109.             desiredView.alpha = 1
  110.            
  111.         })
  112.     }
  113.     // animation out
  114.     func animationOut(desiredView: UIView){
  115.         UIView.animate(withDuration: 0.3, animations: {
  116.             desiredView.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)   //!
  117.             desiredView.alpha = 0
  118.            
  119.         }, completion: { _ in
  120.             desiredView.removeFromSuperview()
  121.         }
  122.         )
  123.     }
  124.    
  125.     override func viewWillDisappear(_ animated: Bool) {
  126.         super.viewWillDisappear(animated)
  127.         timer?.invalidate()
  128.     }
  129.    
  130.     //google map style
  131.     private func styleGoogleMaps() {
  132.         do {
  133.             if let styleUrl = Bundle.main.url(forResource: "style", withExtension: "json"){
  134.                 google_map.mapStyle = try GMSMapStyle(contentsOfFileURL: styleUrl)
  135.             }
  136.             else{
  137.                 NSLog("unable to find style file")
  138.             }
  139.         }catch {
  140.             NSLog("failed to load")
  141.         }
  142.     }
  143.     @IBAction func MenuButton(_ sender: Any) {
  144.         print("Menu open")
  145.        
  146.     }
  147.    
  148.     //function MuLocationButton
  149.     @IBAction func MyLocationButton(_ sender: Any) { print("Show my location")
  150.         google_map.animate(toLocation: locationManager.location!.coordinate)
  151.         google_map.animate(toZoom: 15)
  152.     }
  153.    
  154.     //function filterButton
  155.     @IBAction func FilterButton(_ sender: Any) {
  156.         animateIn(desiredView: BlurView)
  157.         animateIn(desiredView: FilterView)
  158.         print("open")
  159.     }
  160.    
  161.     @IBAction func okButton(_ sender: Any) {
  162.                 animationOut(desiredView: FilterView)
  163.                 smartDisplay()
  164.                 animationOut(desiredView: BlurView)
  165.          print("close")
  166.     }
  167.    
  168.     @IBAction func showBenches(_ sender: UISwitch) {
  169.         if  (sender.isOn == true) {
  170.         print("Benches")
  171.         } else {
  172.             print("Benches off")
  173.         }
  174.     }
  175.    
  176.  
  177.    
  178.     @IBAction func menu(_ sender: Any) {
  179.         print("menu")
  180.         if menuOut == false{
  181.             MenuConstraint.constant = 0
  182.             menuOut = true
  183.             print("Menu in")
  184.         } else {
  185.             MenuConstraint.constant = -275
  186.             menuOut = false
  187.             print("MunuOut")
  188.         }
  189.         UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations: {
  190.             self.view.layoutIfNeeded()
  191.         } ) {(animationComlete) in }
  192.     }
  193.     @objc func tapGesture()  {
  194.         print("tap")
  195.         if menuOut == true{
  196.             MenuConstraint.constant = -275
  197.             menuOut = false
  198.         }
  199.         UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations: {
  200.             self.view.layoutIfNeeded()
  201.         } ) {(animationComlete) in }
  202.     }
  203.    
  204.    
  205.     @IBAction func ContactPressed(_ sender: Any) {
  206.         print("Pressed")
  207.         self.performSegue(withIdentifier: "ViewController", sender: self)
  208.        
  209.     }
  210.    
  211.    
  212.    
  213.    
  214.             func getVersion(){
  215.                 print("Function initialized")
  216.                 var ref: DatabaseReference!
  217.                 ref = Database.database().reference()
  218.                 ref.child("currentVersion").child("version").observeSingleEvent(of: .value, with: { (snapshot) in
  219.                   // Get user value
  220.                   let value = snapshot.value as? Int
  221.                     print(value!)
  222.    
  223.    
  224.                   }) { (error) in
  225.                     print("Error connecting to data base")
  226.                 }
  227.             }
  228.    
  229.     func readMarkers(){
  230.         print("Reading markers but chances for success are low like Artiom's selfesteem")
  231.         var ref: DatabaseReference //our database as a variable
  232.         ref = Database.database().reference()
  233.         ref.child("objects").observeSingleEvent(of: .value, with: { (snapshot) in //make a request and write it to a snaphot
  234.             for currentObject in snapshot.children.allObjects as! [DataSnapshot]{ //we received a snapshot which contains all the objects together, so we have to iterate through it to get every single object
  235.                 let receivedObject = mapObject(snap: currentObject)
  236.                 self.objectList.append(receivedObject!)
  237.             }
  238.             self.smartDisplay()
  239.         }) { (error) in
  240.             print("Error connecting to data base")
  241.         }
  242.     }
  243.    
  244.     func placeMarker(newMarker: mapObject){
  245.         let marker = GMSMarker()
  246.         marker.position = CLLocationCoordinate2D(latitude: newMarker.objectLatitude!, longitude: newMarker.objectLongitude!)
  247.         let customMarker = GMSMarker(position: marker.position)
  248.         //custom marker
  249.         let type = newMarker.objectType
  250.         if type == 0 {
  251.             customMarker.icon = UIImage(named: "m_basketball_marker")
  252.         }
  253.         else if type == 1 {
  254.             customMarker.icon = UIImage(named: "m_bench_marker-3")
  255.         }
  256.         else if type == 2 {
  257.             customMarker.icon = UIImage(named: "m_bench_group_marker-1")
  258.         }
  259.         else if type == 3 {
  260.             customMarker.icon = UIImage(named: "m_trainers_marker-1")
  261.         }
  262.         else if type == 4 {
  263.             customMarker.icon = UIImage(named: "m_toilet_marker-1")
  264.         }
  265.         else if type == 5 {
  266.             customMarker.icon = UIImage(named: "m_trainers_marker-1")
  267.         }
  268.         else if type == 6 {
  269.             customMarker.icon = UIImage(named: "m_pier_marker-1")
  270.         }
  271.         else if type == 8 {
  272.             customMarker.icon = UIImage(named: "m_rbin_marker-1")
  273.         }
  274.         else if type == 10 {
  275.             customMarker.icon = UIImage(named: "m_bicycle_parking_marker-1")
  276.         }
  277.         else if type == 11{
  278.             customMarker.icon = UIImage(named: "m_volleyball_marker-1")
  279.         }
  280.         else if type == 12{
  281.             customMarker.icon = UIImage(named: "skateparks")
  282.         }
  283.         else{
  284.             return
  285.         }
  286.         customMarker.map = google_map
  287.     }
  288.     //    func mapView(_ google_map: GMSMapView, willMove gesture: Bool){
  289.     //
  290.     //    }
  291.     //    func mapView(_ google_map: GMSMapView, didChange position: GMSCameraPosition){
  292.     //
  293.     //        smartDisplay()
  294.     //    }
  295.     //    func mapView(_ google_map: GMSMapView, idleAt position: GMSCameraPosition){
  296.     //
  297.     //    }
  298.    
  299.     @objc func smartDisplay(){ //DO NOT TOUCH
  300.         let currentZoom = google_map.camera.zoom
  301.         if(prevZoom != currentZoom){
  302.             if(currentZoom <= 11.5 && currentZoom > 9 && (prevZoom > 11.5 || prevZoom <= 9)){
  303.                 displayByZoom(dist: 0.02)
  304.             }
  305.             else if(currentZoom <= 13 && currentZoom > 11.5 && (prevZoom > 13 || prevZoom <= 11.5)){
  306.                 displayByZoom(dist: 0.006)
  307.             }
  308.             else if(currentZoom <= 14.3 && currentZoom > 13 && (prevZoom > 14.3 || prevZoom <= 13)){
  309.                 displayByZoom(dist: 0.0035)
  310.             }
  311.             else if(currentZoom <= 15.5 && currentZoom > 14.3 && (prevZoom > 15.5 || prevZoom <= 14.3)){
  312.                
  313.                 displayByZoom(dist: 0.0015)
  314.             }
  315.             else if(currentZoom > 15.5 && prevZoom <= 15.5){
  316.                 google_map.clear()
  317.                 for h in objectList{
  318.                     placeMarker(newMarker: h)
  319.                 }
  320.             }
  321.             else if(currentZoom <= 9){
  322.                 google_map.clear()
  323.             }
  324.         }
  325.         prevZoom = currentZoom
  326.     }
  327.    
  328.     func getDist(x1: Double, y1: Double, x2: Double, y2: Double) -> Double{ //DO NOT TOUCH
  329.         return sqrt((abs(x1 - x2) * abs(x1 - x2)) + (abs(y1 - y2) * abs(y1 - y2)))
  330.     }
  331.    
  332.     var basketballList = Array<mapObject> ()
  333.     var benchList = Array<mapObject> () //for new object create the same way
  334.     var bikeList: Array<mapObject> = []
  335.    
  336.     var gBasketball: Array<Array<Int>> = []
  337.     var gBench: Array<Array<Int>> = []
  338.     var gBike: Array<Array<Int>> = []
  339.    
  340.     var beenBasketball: Array<Bool> = []
  341.     var beenBench: Array<Bool> = []
  342.     var beenBike: Array<Bool> = []
  343.    
  344.     var avgLatBasketball: Double = 0
  345.     var avgLatBench: Double = 0
  346.     var avgLatBike: Double = 0
  347.    
  348.     var avgLonBasketball: Double = 0
  349.     var avgLonBench: Double = 0
  350.     var avgLonBike: Double = 0
  351.    
  352.     var counterBasketball: Int = 0
  353.     var counterBench: Int = 0
  354.    
  355.     func dfs(v: Int, tp: Int){ //add new type
  356.         if(tp == 0){
  357.             beenBasketball[v] = true
  358.             counterBasketball += 1
  359.             avgLatBasketball += basketballList[v].objectLatitude!
  360.             avgLonBasketball += basketballList[v].objectLongitude!
  361.             if(gBasketball[v].count != 0){
  362.                 for i in 0...gBasketball[v].count - 1{
  363.                     if(!beenBasketball[gBasketball[v][i]]){
  364.                         dfs(v: gBasketball[v][i], tp: tp)
  365.                     }
  366.                 }
  367.             }
  368.         }
  369.         else if(tp == 1){
  370.             beenBench[v] = true
  371.             counterBench += 1
  372.             avgLatBench += benchList[v].objectLatitude!
  373.             avgLonBench += benchList[v].objectLongitude!
  374.             if(gBench[v].count != 0){
  375.                 for i in 0...gBench[v].count - 1{
  376.                     if(!beenBench[gBench[v][i]]){
  377.                         dfs(v: gBench[v][i], tp: tp)
  378.                     }
  379.                 }
  380.             }
  381.         }
  382.     }
  383.    
  384.     func displayByZoom(dist: Double){
  385.         google_map.clear()
  386.        
  387.         //step one
  388.        
  389.         for h in objectList{
  390.             if(h.objectType == 0){
  391.                 basketballList.append(h)
  392.             }
  393.             else if(h.objectType == 1){
  394.                 benchList.append(h)
  395.             }
  396.         }
  397.        
  398. //        print("Basketball fields:", basketballList.count, "Benches:", benchList.count)
  399.        
  400.         //step two
  401.        
  402.         for _ in 0...basketballList.count - 1{
  403.             gBasketball.append([])
  404.         }
  405.        
  406.         for _ in 0...benchList.count - 1{
  407.             gBench.append([])
  408.         }
  409.        
  410.         //step three
  411.        
  412.         //basketball
  413.    
  414.         if(basketballList.count > 0){
  415.             for i in 0...basketballList.count - 1{
  416.                 beenBasketball.append(false)
  417.                 if(i != basketballList.count - 1){
  418.                     for j in i + 1...basketballList.count - 1{
  419.                         if(getDist(x1: basketballList[i].objectLatitude!, y1: basketballList[i].objectLongitude!,
  420.                                    x2: basketballList[j].objectLatitude!, y2: basketballList[j].objectLongitude!) < dist){
  421.                             gBasketball[i].append(j)
  422.                             gBasketball[j].append(i)
  423.                         }
  424.                     }
  425.                 }
  426.             }
  427.             for i in 0...basketballList.count - 1{
  428.                 if(!beenBasketball[i]){
  429.                     avgLatBasketball = 0
  430.                     avgLonBasketball = 0
  431.                     counterBasketball = 0
  432.                     dfs(v: i, tp: 0)
  433.                     avgLatBasketball /= Double(counterBasketball)
  434.                     avgLonBasketball /= Double(counterBasketball)
  435.                     }
  436.                     placeMarker(newMarker: mapObject(newLat: avgLatBasketball, newLon: avgLonBasketball, newType: 0)!)
  437.                 }
  438.             }
  439.         }
  440.        
  441.         //bench
  442.         if(benchList.count > 0){
  443.             for i in 0...benchList.count - 1{
  444.                 beenBench.append(false)
  445.                 if(i != benchList.count - 1){
  446.                     for j in i + 1...benchList.count - 1{
  447.                         if(getDist(x1: benchList[i].objectLatitude!, y1: benchList[i].objectLongitude!,
  448.                                    x2: benchList[j].objectLatitude!, y2: benchList[j].objectLongitude!) < dist){
  449.                             gBench[i].append(j)
  450.                             gBench[j].append(i)
  451.                         }
  452.                     }
  453.                 }
  454.             }
  455.            
  456.             for i in 0...benchList.count - 1{
  457.                 if(!beenBench[i]){
  458.                     avgLatBench = 0
  459.                     avgLonBench = 0
  460.                     counterBench = 0
  461.                     dfs(v: i, tp: 1)
  462.                     avgLatBench /= Double(counterBench)
  463.                     avgLonBench /= Double(counterBench)
  464.                     placeMarker(newMarker: mapObject(newLat: avgLatBench, newLon: avgLonBench, newType: 1)!)
  465.                 }
  466.             }
  467.         }
  468.        
  469.         //step four
  470.         basketballList.removeAll()
  471.         beenBasketball.removeAll()
  472.         gBasketball.removeAll()
  473.  
  474.         benchList.removeAll()
  475.         beenBench.removeAll()
  476.         gBench.removeAll()
  477.     }
  478. }
  479.  
Advertisement
Add Comment
Please, Sign In to add comment