LocationView.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 lbDistance: UILabel!
  31. var controller: ViewController
  32. var storyboard: UIStoryboard
  33. var didFindMyLocation = false
  34. var locationTimer: Timer? = nil
  35. override init(frame: CGRect){
  36. // Set controller
  37. self.storyboard = UIStoryboard(name: "Main", bundle: nil)
  38. self.controller = storyboard.instantiateViewController(withIdentifier: "GMViewController") as! ViewController
  39. super.init(frame: frame)
  40. }
  41. /**
  42. Run when the view is started.
  43. */
  44. required init?(coder aDecoder: NSCoder) {
  45. // Set controller
  46. self.storyboard = UIStoryboard(name: "Main", bundle: nil)
  47. self.controller = storyboard.instantiateViewController(withIdentifier: "GMViewController") as! ViewController
  48. super.init(coder: aDecoder)
  49. // Load the contents of the HomeView.xib file.
  50. Bundle.main.loadNibNamed("LocationView", owner: self, options: nil)
  51. self.addSubview(self.container)
  52. self.container.frame = self.bounds
  53. // Set map up
  54. map.isMyLocationEnabled = true
  55. //map.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.new, context: nil)
  56. let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: 42.846399, longitude: -2.673365, zoom: 12.0)
  57. map.camera = camera
  58. Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(getNewLocation), userInfo: nil, repeats: true)
  59. }
  60. /**
  61. I am not sure what does this do...
  62. */
  63. func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
  64. if status == CLAuthorizationStatus.authorizedWhenInUse {
  65. map.isMyLocationEnabled = true
  66. }
  67. }
  68. /**
  69. Get new location
  70. */
  71. func getNewLocation(){
  72. NSLog(":LOCATION:DEBUG: Setting marker.")
  73. let defaults = UserDefaults.standard
  74. if (defaults.value(forKey: "GMLocLat") != nil && defaults.value(forKey: "GMLocLon") != nil){
  75. let lat = defaults.value(forKey: "GMLocLat") as! Double
  76. let lon = defaults.value(forKey: "GMLocLon") as! Double
  77. let time = defaults.value(forKey: "GMLocTime") as! Date
  78. // TODO compare time
  79. let marker = GMSMarker()
  80. marker.position = CLLocationCoordinate2D(latitude: lat, longitude: lon)
  81. marker.title = "Gasteizko Margolariak"
  82. //marker.snippet = "Australia"
  83. marker.map = self.map
  84. NSLog(":LOCATION:DEBUG: Marker set.")
  85. let location = self.controller.getLocation()
  86. if location != nil {
  87. let d: Int = calculateDistance(lat1: location.latitude, lon1: location.longitude, lat2: lat, lon2: lon)
  88. NSLog(":LOCATION:LOG: Distance (m): \(d)")
  89. if d <= 1000 {
  90. self.lbDistance.text = "A \(d) metros de ti."
  91. }
  92. else{
  93. self.lbDistance.text = "A \(Int(d/1000)) kilómetros de ti."
  94. }
  95. }
  96. }
  97. else{
  98. NSLog(":LOCATION:DEBUG: No location saved yet.")
  99. }
  100. }
  101. func degreesToRadians(degrees: Double) -> Double {
  102. return degrees * Double.pi / 180;
  103. }
  104. func calculateDistance(lat1: Double, lon1: Double, lat2: Double, lon2: Double) -> Int {
  105. let eRadius: Double = 6371
  106. let dLat: Double = degreesToRadians(degrees: lat2-lat1)
  107. let dLon: Double = degreesToRadians(degrees: lon2-lon1)
  108. let l1: Double = degreesToRadians(degrees: lat1)
  109. let l2: Double = degreesToRadians(degrees: lat2)
  110. let a: Double = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(l1) * cos(l2)
  111. let c: Double = 2 * atan2(sqrt(a), sqrt(1-a))
  112. let m: Double = eRadius * c * 1000
  113. return Int(m)
  114. }
  115. /*func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutableRawPointer) {
  116. if !didFindMyLocation {
  117. let myLocation: CLLocation = change[NSKeyValueChangeNewKey] as CLLocation
  118. map.camera = GMSCameraPosition.camera(withTarget: myLocation.coordinate, zoom: 10.0)
  119. map.settings.myLocationButton = true
  120. didFindMyLocation = true
  121. }
  122. }
  123. */
  124. }