Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // walkpro4
- //
- // Created by Андрей Васильков on 08.07.2020.
- // Copyright © 2020 Andrey from DUO A COMPANY. All rights reserved.
- //
- import UIKit
- import GoogleMaps
- import CoreLocation
- import Firebase
- class ViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {
- @IBOutlet weak var MenuConstraint: NSLayoutConstraint!
- @IBOutlet weak var SideMenu: UIView!
- // @IBOutlet weak var OveralView: UIView!
- @IBOutlet weak var BlurView: UIView!
- @IBOutlet var FilterView: UIView!
- @IBOutlet weak var benchSwitch: UISwitch!
- //map
- @IBOutlet weak var google_map: GMSMapView!
- var type = [Int]()
- var ref: DatabaseReference!
- var readMarker: DatabaseHandle!
- var objectList: Array<mapObject> = []
- var menuOut = false
- var locationManager = CLLocationManager()
- var timer: Timer?
- var prevZoom: Float = -1
- //filter variables
- // var fBench: Bool = true
- // writes coordinates
- func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
- if locations.first != nil {
- }
- }
- // turn using location on
- func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
- if(status == CLAuthorizationStatus.denied ) {
- }
- }
- //location point
- private func LocationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
- _ = locationManager.location!.coordinate
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- readMarkers()
- styleGoogleMaps()
- BlurView.bounds = self.view.bounds
- FilterView.bounds = CGRect(x: 0, y: 0, width: self.view.bounds.width * 0.9, height:self.view.bounds.height * 0.8 )
- func initializeTheLocationManager() {
- locationManager.delegate = self //location function
- locationManager.requestWhenInUseAuthorization() //location function
- locationManager.startUpdatingLocation()//location function
- }
- //camera
- let camera = GMSCameraPosition.camera(withLatitude: 59.949831, longitude: 30.3168700, zoom: 9)
- google_map.camera = camera
- google_map.delegate = self
- initializeTheLocationManager()
- self.google_map.isMyLocationEnabled = true
- smartDisplay()
- // mapView(google_map, idleAt: camera)
- // side menu
- SideMenu.layer.shadowColor = UIColor.black.cgColor
- SideMenu.layer.shadowOpacity = 0.2
- SideMenu.layer.shadowOffset = CGSize(width: 0.8, height: 0)
- MenuConstraint.constant = -275
- SideMenu.isUserInteractionEnabled = true
- let tapGesture = UISwipeGestureRecognizer(target: self, action: #selector(self.tapGesture))
- tapGesture.direction = .left
- SideMenu.addGestureRecognizer(tapGesture)
- timer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(smartDisplay), userInfo: nil, repeats: true)
- }
- //animate popup
- func animateIn(desiredView: UIView) {
- let BackgroundView = self.view!
- BackgroundView.addSubview(desiredView)
- desiredView.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
- desiredView.alpha = 0
- desiredView.center = BackgroundView.center
- //animate the effect
- UIView.animate(withDuration: 0.3, animations: {
- desiredView.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
- desiredView.alpha = 1
- })
- }
- // animation out
- func animationOut(desiredView: UIView){
- UIView.animate(withDuration: 0.3, animations: {
- desiredView.transform = CGAffineTransform(scaleX: 1.1, y: 1.1) //!
- desiredView.alpha = 0
- }, completion: { _ in
- desiredView.removeFromSuperview()
- }
- )
- }
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
- timer?.invalidate()
- }
- //google map style
- private func styleGoogleMaps() {
- do {
- if let styleUrl = Bundle.main.url(forResource: "style", withExtension: "json"){
- google_map.mapStyle = try GMSMapStyle(contentsOfFileURL: styleUrl)
- }
- else{
- NSLog("unable to find style file")
- }
- }catch {
- NSLog("failed to load")
- }
- }
- @IBAction func MenuButton(_ sender: Any) {
- print("Menu open")
- }
- //function MuLocationButton
- @IBAction func MyLocationButton(_ sender: Any) { print("Show my location")
- google_map.animate(toLocation: locationManager.location!.coordinate)
- google_map.animate(toZoom: 15)
- }
- //function filterButton
- @IBAction func FilterButton(_ sender: Any) {
- animateIn(desiredView: BlurView)
- animateIn(desiredView: FilterView)
- print("open")
- }
- @IBAction func okButton(_ sender: Any) {
- animationOut(desiredView: FilterView)
- smartDisplay()
- animationOut(desiredView: BlurView)
- print("close")
- }
- @IBAction func showBenches(_ sender: UISwitch) {
- if (sender.isOn == true) {
- print("Benches")
- } else {
- print("Benches off")
- }
- }
- @IBAction func menu(_ sender: Any) {
- print("menu")
- if menuOut == false{
- MenuConstraint.constant = 0
- menuOut = true
- print("Menu in")
- } else {
- MenuConstraint.constant = -275
- menuOut = false
- print("MunuOut")
- }
- UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations: {
- self.view.layoutIfNeeded()
- } ) {(animationComlete) in }
- }
- @objc func tapGesture() {
- print("tap")
- if menuOut == true{
- MenuConstraint.constant = -275
- menuOut = false
- }
- UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations: {
- self.view.layoutIfNeeded()
- } ) {(animationComlete) in }
- }
- @IBAction func ContactPressed(_ sender: Any) {
- print("Pressed")
- self.performSegue(withIdentifier: "ViewController", sender: self)
- }
- func getVersion(){
- print("Function initialized")
- var ref: DatabaseReference!
- ref = Database.database().reference()
- ref.child("currentVersion").child("version").observeSingleEvent(of: .value, with: { (snapshot) in
- // Get user value
- let value = snapshot.value as? Int
- print(value!)
- }) { (error) in
- print("Error connecting to data base")
- }
- }
- func readMarkers(){
- print("Reading markers but chances for success are low like Artiom's selfesteem")
- var ref: DatabaseReference //our database as a variable
- ref = Database.database().reference()
- ref.child("objects").observeSingleEvent(of: .value, with: { (snapshot) in //make a request and write it to a snaphot
- 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
- let receivedObject = mapObject(snap: currentObject)
- self.objectList.append(receivedObject!)
- }
- self.smartDisplay()
- }) { (error) in
- print("Error connecting to data base")
- }
- }
- func placeMarker(newMarker: mapObject){
- let marker = GMSMarker()
- marker.position = CLLocationCoordinate2D(latitude: newMarker.objectLatitude!, longitude: newMarker.objectLongitude!)
- let customMarker = GMSMarker(position: marker.position)
- //custom marker
- let type = newMarker.objectType
- if type == 0 {
- customMarker.icon = UIImage(named: "m_basketball_marker")
- }
- else if type == 1 {
- customMarker.icon = UIImage(named: "m_bench_marker-3")
- }
- else if type == 2 {
- customMarker.icon = UIImage(named: "m_bench_group_marker-1")
- }
- else if type == 3 {
- customMarker.icon = UIImage(named: "m_trainers_marker-1")
- }
- else if type == 4 {
- customMarker.icon = UIImage(named: "m_toilet_marker-1")
- }
- else if type == 5 {
- customMarker.icon = UIImage(named: "m_trainers_marker-1")
- }
- else if type == 6 {
- customMarker.icon = UIImage(named: "m_pier_marker-1")
- }
- else if type == 8 {
- customMarker.icon = UIImage(named: "m_rbin_marker-1")
- }
- else if type == 10 {
- customMarker.icon = UIImage(named: "m_bicycle_parking_marker-1")
- }
- else if type == 11{
- customMarker.icon = UIImage(named: "m_volleyball_marker-1")
- }
- else if type == 12{
- customMarker.icon = UIImage(named: "skateparks")
- }
- else{
- return
- }
- customMarker.map = google_map
- }
- // func mapView(_ google_map: GMSMapView, willMove gesture: Bool){
- //
- // }
- // func mapView(_ google_map: GMSMapView, didChange position: GMSCameraPosition){
- //
- // smartDisplay()
- // }
- // func mapView(_ google_map: GMSMapView, idleAt position: GMSCameraPosition){
- //
- // }
- @objc func smartDisplay(){ //DO NOT TOUCH
- let currentZoom = google_map.camera.zoom
- if(prevZoom != currentZoom){
- if(currentZoom <= 11.5 && currentZoom > 9 && (prevZoom > 11.5 || prevZoom <= 9)){
- displayByZoom(dist: 0.02)
- }
- else if(currentZoom <= 13 && currentZoom > 11.5 && (prevZoom > 13 || prevZoom <= 11.5)){
- displayByZoom(dist: 0.006)
- }
- else if(currentZoom <= 14.3 && currentZoom > 13 && (prevZoom > 14.3 || prevZoom <= 13)){
- displayByZoom(dist: 0.0035)
- }
- else if(currentZoom <= 15.5 && currentZoom > 14.3 && (prevZoom > 15.5 || prevZoom <= 14.3)){
- displayByZoom(dist: 0.0015)
- }
- else if(currentZoom > 15.5 && prevZoom <= 15.5){
- google_map.clear()
- for h in objectList{
- placeMarker(newMarker: h)
- }
- }
- else if(currentZoom <= 9){
- google_map.clear()
- }
- }
- prevZoom = currentZoom
- }
- func getDist(x1: Double, y1: Double, x2: Double, y2: Double) -> Double{ //DO NOT TOUCH
- return sqrt((abs(x1 - x2) * abs(x1 - x2)) + (abs(y1 - y2) * abs(y1 - y2)))
- }
- var basketballList = Array<mapObject> ()
- var benchList = Array<mapObject> () //for new object create the same way
- var bikeList: Array<mapObject> = []
- var gBasketball: Array<Array<Int>> = []
- var gBench: Array<Array<Int>> = []
- var gBike: Array<Array<Int>> = []
- var beenBasketball: Array<Bool> = []
- var beenBench: Array<Bool> = []
- var beenBike: Array<Bool> = []
- var avgLatBasketball: Double = 0
- var avgLatBench: Double = 0
- var avgLatBike: Double = 0
- var avgLonBasketball: Double = 0
- var avgLonBench: Double = 0
- var avgLonBike: Double = 0
- var counterBasketball: Int = 0
- var counterBench: Int = 0
- func dfs(v: Int, tp: Int){ //add new type
- if(tp == 0){
- beenBasketball[v] = true
- counterBasketball += 1
- avgLatBasketball += basketballList[v].objectLatitude!
- avgLonBasketball += basketballList[v].objectLongitude!
- if(gBasketball[v].count != 0){
- for i in 0...gBasketball[v].count - 1{
- if(!beenBasketball[gBasketball[v][i]]){
- dfs(v: gBasketball[v][i], tp: tp)
- }
- }
- }
- }
- else if(tp == 1){
- beenBench[v] = true
- counterBench += 1
- avgLatBench += benchList[v].objectLatitude!
- avgLonBench += benchList[v].objectLongitude!
- if(gBench[v].count != 0){
- for i in 0...gBench[v].count - 1{
- if(!beenBench[gBench[v][i]]){
- dfs(v: gBench[v][i], tp: tp)
- }
- }
- }
- }
- }
- func displayByZoom(dist: Double){
- google_map.clear()
- //step one
- for h in objectList{
- if(h.objectType == 0){
- basketballList.append(h)
- }
- else if(h.objectType == 1){
- benchList.append(h)
- }
- }
- // print("Basketball fields:", basketballList.count, "Benches:", benchList.count)
- //step two
- for _ in 0...basketballList.count - 1{
- gBasketball.append([])
- }
- for _ in 0...benchList.count - 1{
- gBench.append([])
- }
- //step three
- //basketball
- if(basketballList.count > 0){
- for i in 0...basketballList.count - 1{
- beenBasketball.append(false)
- if(i != basketballList.count - 1){
- for j in i + 1...basketballList.count - 1{
- if(getDist(x1: basketballList[i].objectLatitude!, y1: basketballList[i].objectLongitude!,
- x2: basketballList[j].objectLatitude!, y2: basketballList[j].objectLongitude!) < dist){
- gBasketball[i].append(j)
- gBasketball[j].append(i)
- }
- }
- }
- }
- for i in 0...basketballList.count - 1{
- if(!beenBasketball[i]){
- avgLatBasketball = 0
- avgLonBasketball = 0
- counterBasketball = 0
- dfs(v: i, tp: 0)
- avgLatBasketball /= Double(counterBasketball)
- avgLonBasketball /= Double(counterBasketball)
- }
- placeMarker(newMarker: mapObject(newLat: avgLatBasketball, newLon: avgLonBasketball, newType: 0)!)
- }
- }
- }
- //bench
- if(benchList.count > 0){
- for i in 0...benchList.count - 1{
- beenBench.append(false)
- if(i != benchList.count - 1){
- for j in i + 1...benchList.count - 1{
- if(getDist(x1: benchList[i].objectLatitude!, y1: benchList[i].objectLongitude!,
- x2: benchList[j].objectLatitude!, y2: benchList[j].objectLongitude!) < dist){
- gBench[i].append(j)
- gBench[j].append(i)
- }
- }
- }
- }
- for i in 0...benchList.count - 1{
- if(!beenBench[i]){
- avgLatBench = 0
- avgLonBench = 0
- counterBench = 0
- dfs(v: i, tp: 1)
- avgLatBench /= Double(counterBench)
- avgLonBench /= Double(counterBench)
- placeMarker(newMarker: mapObject(newLat: avgLatBench, newLon: avgLonBench, newType: 1)!)
- }
- }
- }
- //step four
- basketballList.removeAll()
- beenBasketball.removeAll()
- gBasketball.removeAll()
- benchList.removeAll()
- beenBench.removeAll()
- gBench.removeAll()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment