LocationView.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright (C) 2016 Inigo Valentin
  2. //
  3. // This file is part of the Gasteizko Margolariak IOS app.
  4. //
  5. // The Gasteizko Margolariak IOS app is free software: you can
  6. // redistribute it and/or modify it under the terms of the
  7. // GNU General Public License as published by the Free Software
  8. // Foundation, either version 3 of the License, or (at your
  9. // option) any later version.
  10. //
  11. // The Gasteizko Margolariak IOS app is distributed in the
  12. // hope that it will be useful, but WITHOUT ANY WARRANTY;
  13. // without even the implied warranty of MERCHANTABILITY or
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  15. // Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public
  18. // License along with the Gasteizko Margolariak IOS app.
  19. // If not, see <http://www.gnu.org/licenses/>.
  20. import Foundation
  21. import CoreData
  22. import UIKit
  23. import GoogleMaps
  24. /**
  25. Class to handle the home view.
  26. */
  27. class LocationView: UIView {
  28. @IBOutlet var container: UIView!
  29. @IBOutlet weak var map: GMSMapView!
  30. @IBOutlet weak var lbTitle: UILabel!
  31. @IBOutlet weak var lbDistance: UILabel!
  32. @IBOutlet weak var lbNo: UILabel!
  33. var controller: ViewController
  34. var storyboard: UIStoryboard
  35. var didFindMyLocation = false
  36. var locationTimer: Timer? = nil
  37. override init(frame: CGRect){
  38. // Set controller
  39. self.storyboard = UIStoryboard(name: "Main", bundle: nil)
  40. self.controller = storyboard.instantiateViewController(withIdentifier: "GMViewController") as! ViewController
  41. super.init(frame: frame)
  42. }
  43. /**
  44. Run when the view is started.
  45. */
  46. required init?(coder aDecoder: NSCoder) {
  47. // Set controller
  48. self.storyboard = UIStoryboard(name: "Main", bundle: nil)
  49. self.controller = storyboard.instantiateViewController(withIdentifier: "GMViewController") as! ViewController
  50. super.init(coder: aDecoder)
  51. // Load the contents of the HomeView.xib file.
  52. Bundle.main.loadNibNamed("LocationView", owner: self, options: nil)
  53. self.addSubview(self.container)
  54. self.container.frame = self.bounds
  55. // Set map up
  56. map.isMyLocationEnabled = true
  57. //map.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.new, context: nil)
  58. let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: 42.846399, longitude: -2.673365, zoom: 12.0)
  59. map.camera = camera
  60. // Schedule location fetches
  61. getNewLocation()
  62. Timer.scheduledTimer(timeInterval: 60, target: self, selector: #selector(getNewLocation), userInfo: nil, repeats: true)
  63. }
  64. /**
  65. I am not sure what does this do...
  66. */
  67. func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
  68. if status == CLAuthorizationStatus.authorizedWhenInUse {
  69. map.isMyLocationEnabled = true
  70. }
  71. }
  72. /**
  73. Get new location
  74. */
  75. func getNewLocation(){
  76. NSLog(":LOCATION:LOG: Recalculating location.")
  77. let defaults = UserDefaults.standard
  78. if (defaults.value(forKey: "GMLocLat") != nil && defaults.value(forKey: "GMLocLon") != nil){
  79. let lat = defaults.value(forKey: "GMLocLat") as! Double
  80. let lon = defaults.value(forKey: "GMLocLon") as! Double
  81. let time = defaults.value(forKey: "GMLocTime") as! Date
  82. let cTime = Date()
  83. let minutes = Calendar.current.dateComponents([.minute], from: time, to: cTime).minute
  84. if (minutes! < 30){
  85. self.map.isHidden = false
  86. self.lbTitle.isHidden = false
  87. self.lbDistance.isHidden = false
  88. self.lbNo.isHidden = true
  89. let marker = GMSMarker()
  90. marker.position = CLLocationCoordinate2D(latitude: lat, longitude: lon)
  91. marker.title = "Gasteizko Margolariak"
  92. //marker.snippet = "Australia"
  93. marker.map = self.map
  94. let location = self.controller.getLocation()
  95. if location != nil {
  96. let d: Int = calculateDistance(lat1: location.latitude, lon1: location.longitude, lat2: lat, lon2: lon)
  97. if d <= 1000 {
  98. self.lbDistance.text = "A \(d) metros de ti."
  99. }
  100. else{
  101. self.lbDistance.text = "A \(Int(d/1000)) kilómetros de ti."
  102. }
  103. }
  104. }
  105. else{
  106. self.map.isHidden = true
  107. self.lbTitle.isHidden = true
  108. self.lbDistance.isHidden = true
  109. self.lbNo.isHidden = false
  110. }
  111. }
  112. }
  113. func degreesToRadians(degrees: Double) -> Double {
  114. return degrees * Double.pi / 180;
  115. }
  116. func calculateDistance(lat1: Double, lon1: Double, lat2: Double, lon2: Double) -> Int {
  117. let eRadius: Double = 6371
  118. let dLat: Double = degreesToRadians(degrees: lat2-lat1)
  119. let dLon: Double = degreesToRadians(degrees: lon2-lon1)
  120. let l1: Double = degreesToRadians(degrees: lat1)
  121. let l2: Double = degreesToRadians(degrees: lat2)
  122. let a: Double = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(l1) * cos(l2)
  123. let c: Double = 2 * atan2(sqrt(a), sqrt(1-a))
  124. let m: Double = eRadius * c * 1000
  125. return Int(m)
  126. }
  127. /*func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutableRawPointer) {
  128. if !didFindMyLocation {
  129. let myLocation: CLLocation = change[NSKeyValueChangeNewKey] as CLLocation
  130. map.camera = GMSCameraPosition.camera(withTarget: myLocation.coordinate, zoom: 10.0)
  131. map.settings.myLocationButton = true
  132. didFindMyLocation = true
  133. }
  134. }
  135. */
  136. }