-
Notifications
You must be signed in to change notification settings - Fork 0
/
CorridaViewController.swift
executable file
·281 lines (194 loc) · 11.1 KB
/
CorridaViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//
// CorridaViewController.swift
// Be Healthy Santander
//
// Created by Flex Administrador on 5/10/17.
// Copyright © 2017 Flex Administrador. All rights reserved.
//
import UIKit
import MapKit
import HealthKit
class CorridaViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!
var localizationManager = CLLocationManager()
@IBOutlet weak var labelSpeed: UILabel!
@IBOutlet weak var labelAddress: UILabel!
@IBOutlet weak var labelTime: UILabel!
@IBOutlet weak var labelDistance: UILabel!
@IBOutlet weak var labelCalories: UILabel!
lazy var timer = Timer()
var seconds = 0.0
var distance = 0.0
var calories = 0.0
var IMC: Double = 0.0
var startLocation: CLLocation!
var lastLocation: CLLocation!
var startDate: Date!
var traveledDistance: Double = 0
var straightDistance: Double = 0
@IBOutlet weak var stopButton: UIButton!
//Variáveis recebidas da última view
//Variáveis recebidas da primeira View
var receivedHeight : String!
var receivedWeight : String!
var receivedAge : String!
var receivedSex : String!
override func viewDidLoad() {
super.viewDidLoad()
print ("\n\nPASSOU PRA SEGUNDA VIEW\n\n")
localizationManager.delegate = self
localizationManager.desiredAccuracy = kCLLocationAccuracyBest
localizationManager.requestWhenInUseAuthorization()
localizationManager.startUpdatingLocation()
timer = Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: #selector(self.atualizaSegundo(timer:)),
userInfo: nil,
repeats: true)
print ("\(receivedWeight)")
print ("\(receivedHeight)")
print ("\(receivedAge)")
let convertedHeight = Double (receivedHeight)
let convertedAge = Double(receivedAge)
// altura convertida para Metros e centímetros
let heightInMeters = (convertedHeight! * 0.010)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
timer.invalidate()
}
func atualizaSegundo(timer: Timer) {
seconds += 1
let secondsQuantity = HKQuantity(unit: HKUnit.second(), doubleValue: seconds)
labelTime.text = secondsQuantity.description
//formula para calculo de calorias : Velocidade (km/h) x peso (kg) x 0,0175.
let speedUser = Double(labelSpeed.text!)
let convertedWeightDouble = Double(receivedWeight)
if Date().timeIntervalSince(startDate) > 0 {
let lostCalories = (Date().timeIntervalSince(startDate) / 60) * speedUser! * convertedWeightDouble! * 0.0175
let quantityCalories = HKQuantity(unit:HKUnit.kilocalorie(), doubleValue: lostCalories)
labelCalories.text = String(format: "%.2f Kcal", lostCalories)
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let localizacaoUsuario = locations.last!
let longitude = localizacaoUsuario.coordinate.longitude
let latitude = localizacaoUsuario.coordinate.latitude
let velocidadeUsuario = Double(localizacaoUsuario.speed)
let velocidadeEmKmh = velocidadeUsuario * 3.6
if localizacaoUsuario.speed > 0 {
labelSpeed.text = String(format: "%.2f", velocidadeEmKmh)
}else{
self.labelSpeed.text = String("0.0")
}
let deltaLat: CLLocationDegrees = 0.01
let deltaLon: CLLocationDegrees = 0.01
let localizacao: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
let areaExibicao: MKCoordinateSpan = MKCoordinateSpanMake(deltaLat, deltaLon)
let regiao: MKCoordinateRegion = MKCoordinateRegionMake(localizacao, areaExibicao)
map.setRegion(regiao, animated: true)
print(locations.last ?? "none")
if startDate == nil {
startDate = Date()
} else {
print("Tempo Gasto:", String(format: "%.2fs", Date().timeIntervalSince(startDate)))
}
if startLocation == nil {
startLocation = locations.first
} else if let location = locations.last {
traveledDistance += lastLocation.distance(from: location)
print("Distância Percorrida:", traveledDistance)
print("Distância do ponto de partida:", startLocation.distance(from: locations.last!))
labelDistance.text = String(format: "%.2f", traveledDistance)
}
lastLocation = locations.last
CLGeocoder().reverseGeocodeLocation( localizacaoUsuario) { (detalhesLocal, erro) in
if erro == nil {
if let dadosLocal = detalhesLocal?.first {
var thoroughfare = ""
if dadosLocal.thoroughfare != nil {
thoroughfare = dadosLocal.thoroughfare!
}
var subThoroughfare = ""
if dadosLocal.subThoroughfare != nil {
subThoroughfare = dadosLocal.subThoroughfare!
}
var locality = ""
if dadosLocal.locality != nil {
locality = dadosLocal.locality!
}
var subLocality = ""
if dadosLocal.subLocality != nil {
subLocality = dadosLocal.subLocality!
}
var postalCode = ""
if dadosLocal.postalCode != nil {
postalCode = dadosLocal.postalCode!
}
var country = ""
if dadosLocal.country != nil {
country = dadosLocal.country!
}
var administrativeArea = ""
if dadosLocal.administrativeArea != nil {
administrativeArea = dadosLocal.administrativeArea!
}
var subAdministrativeArea = ""
if dadosLocal.subAdministrativeArea != nil {
subAdministrativeArea = dadosLocal.subAdministrativeArea!
}
self.labelAddress.text = thoroughfare + " - "
+ subThoroughfare + " / "
+ locality
print(
"Endereço:" + thoroughfare +
"\n / Número Aprox:" + subThoroughfare +
"\n / Estado:" + locality +
"\n / Bairro:" + subLocality +
"\n / CEP:" + postalCode +
"\n / País:" + country +
"\n / Estado:" + administrativeArea +
"\n / Região: + \(subAdministrativeArea) \n"
)
}
}else{
print(erro)
}
}
}
@IBAction func parar(_ sender: UIButton) {
timer.invalidate()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SegueAposTerminarCorrida" {
let destinyViewController = segue.destination as! DadosDaCorridaViewController
destinyViewController.receivedSpeed = labelSpeed.text!
destinyViewController.receivedCalories = labelCalories.text!
destinyViewController.receivedTime = labelTime.text!
destinyViewController.receivedDistance = labelDistance.text!
destinyViewController.receivedMap = map!
destinyViewController.receivedTimeDouble = Date().timeIntervalSince(startDate)
//Passando os dados da primeira, que ja vieram pra essa(segunda) e passando pra terceira
destinyViewController.receivedHeight2 = receivedHeight
destinyViewController.receivedWeight2 = receivedWeight
destinyViewController.receivedAge2 = receivedAge
destinyViewController.receivedSex2 = receivedSex
}
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status != .authorizedWhenInUse {
let alertaController = UIAlertController(title: "Permissão de localização",
message: "Precisamos ter acesso a sua localização, por favor habilite o GPS",
preferredStyle: .alert )
let acaoConfiguracoes = UIAlertAction(title: "Configurações", style: .default , handler: { (alertaConfiguracoes) in
if let configuracoes = NSURL(string: UIApplicationOpenSettingsURLString ) {
UIApplication.shared.open( configuracoes as URL )
}
})
let acaoCancelar = UIAlertAction(title: "Cancelar", style: .default , handler: nil )
alertaController.addAction( acaoConfiguracoes )
alertaController.addAction( acaoCancelar )
present( alertaController , animated: true, completion: nil )
}
}
}