Skip to content

Commit

Permalink
feat(app): real time location
Browse files Browse the repository at this point in the history
  • Loading branch information
krystxf committed May 15, 2024
1 parent c7fb8ce commit 25f56bc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/metro-now/metro-now/Core/TabBar/MainTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SwiftUI

struct MainTabView: View {
@StateObject private var locationModel = LocationModel()
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()

var body: some View {
TabView {
Expand All @@ -23,6 +24,9 @@ struct MainTabView: View {
Label("Map", systemImage: "map")
}
}
.onReceive(locationModel.$location) { location in
print("User's location: \(location)")
}
.onAppear {
locationModel.checkLocationServicesEnabled()
}
Expand Down
11 changes: 10 additions & 1 deletion app/metro-now/metro-now/Models/LocationModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,28 @@ import Foundation
import MapKit

final class LocationModel: NSObject, ObservableObject, CLLocationManagerDelegate {
@Published var location: CLLocation?
var locationManager: CLLocationManager?

func checkLocationServicesEnabled() {
let isEnabled = CLLocationManager.locationServicesEnabled()

if isEnabled {
locationManager = CLLocationManager()
locationManager!.delegate = self
if let locationManager {
locationManager.delegate = self
locationManager.distanceFilter = 10.0
locationManager.startUpdatingLocation()
}
} else {
print("Location services not enabled")
}
}

func locationManager(_: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
location = locations.last
}

private func checkLocationAuthorization() {
guard let locationManager else { return }

Expand Down

0 comments on commit 25f56bc

Please sign in to comment.