LocationView.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. let defaults = UserDefaults.standard
  77. if (defaults.value(forKey: "GMLocLat") != nil && defaults.value(forKey: "GMLocLon") != nil){
  78. let lat = defaults.value(forKey: "GMLocLat") as! Double
  79. let lon = defaults.value(forKey: "GMLocLon") as! Double
  80. let time = defaults.value(forKey: "GMLocTime") as! Date
  81. let cTime = Date()
  82. let minutes = Calendar.current.dateComponents([.minute], from: time, to: cTime).minute
  83. if (minutes! < 30){
  84. self.map.isHidden = false
  85. self.lbTitle.isHidden = false
  86. self.lbDistance.isHidden = false
  87. self.lbNo.isHidden = true
  88. let marker = GMSMarker()
  89. marker.position = CLLocationCoordinate2D(latitude: lat, longitude: lon)
  90. marker.title = "Gasteizko Margolariak"
  91. //marker.snippet = "Australia"
  92. marker.map = self.map
  93. let location = self.controller.getLocation()
  94. if location != nil {
  95. let d: Int = calculateDistance(lat1: location.latitude, lon1: location.longitude, lat2: lat, lon2: lon)
  96. if d <= 1000 {
  97. self.lbDistance.text = "A \(d) metros de ti."
  98. }
  99. else{
  100. self.lbDistance.text = "A \(Int(d/1000)) kilómetros de ti."
  101. }
  102. }
  103. }
  104. else{
  105. self.map.isHidden = true
  106. self.lbTitle.isHidden = true
  107. self.lbDistance.isHidden = true
  108. self.lbNo.isHidden = false
  109. }
  110. }
  111. }
  112. func degreesToRadians(degrees: Double) -> Double {
  113. return degrees * Double.pi / 180;
  114. }
  115. func calculateDistance(lat1: Double, lon1: Double, lat2: Double, lon2: Double) -> Int {
  116. let eRadius: Double = 6371
  117. let dLat: Double = degreesToRadians(degrees: lat2-lat1)
  118. let dLon: Double = degreesToRadians(degrees: lon2-lon1)
  119. let l1: Double = degreesToRadians(degrees: lat1)
  120. let l2: Double = degreesToRadians(degrees: lat2)
  121. let a: Double = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(l1) * cos(l2)
  122. let c: Double = 2 * atan2(sqrt(a), sqrt(1-a))
  123. let m: Double = eRadius * c * 1000
  124. return Int(m)
  125. }
  126. /*func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutableRawPointer) {
  127. if !didFindMyLocation {
  128. let myLocation: CLLocation = change[NSKeyValueChangeNewKey] as CLLocation
  129. map.camera = GMSCameraPosition.camera(withTarget: myLocation.coordinate, zoom: 10.0)
  130. map.settings.myLocationButton = true
  131. didFindMyLocation = true
  132. }
  133. }
  134. */
  135. }