[ad_1]
I’m engaged on a mission that gathers real-time location information and shops it in a database (utilizing google firebase). I’m comparatively new to Swift however have been coding for about 2 years. I’ve created a primary HomePage UI and a MapView UI, utilizing a navigation hyperlink to maneuver from web page to web page. The MapViewModel class is a category used to collect location information from the consumer. I’m making an attempt to name a way of the MapViewModel class with .onAppear. Though after I run this system the operate just isn’t known as and “inside” is not printed on the terminal. Am I lacking one thing on how .onAppear and navigation hyperlinks work? Please let me know what the perfect resolution can be to name the operate when the view is switched to MapView.
import SwiftUI
import MapKit
struct HomePage: View {
@State non-public var viewModel = MapViewModel()
var physique: some View {
NavigationView {
ZStack {
LinearGradient(
gradient: Gradient(colours: [.blue, Color(red:0.18, green: 0.79, blue: 0.91, opacity: 1.0)]),
startPoint: .topLeading,
endPoint: .bottomTrailing)
.edgesIgnoringSafeArea(.all)
NavigationLink {
MapView()
}
label: {
ZStack {
Circle()
.trim(from: 0.5)
.body(width: 450, top: 450)
.foregroundColor(Shade(purple: 1.0, inexperienced: 0.89, blue: 0.36, opacity: 1.0))
Textual content("Navigate")
.foregroundColor(.white)
.font(.system(dimension:32, weight: .daring, design: .default))
.padding(.backside, 280)
}
.place(x: 195, y: 900)
.ignoresSafeArea(.all)
}
}
}
}
}
struct MapView: View {
@State non-public var viewModel = MapViewModel()
var physique: some View {
Map(coordinateRegion: $viewModel.area, showsUserLocation: true)
.ignoresSafeArea()
.accentColor(Shade(.systemPink))
.onAppear {
print("inside")
viewModel.checkIfLocationServiceIsEnabled()
}
}
}
ultimate class MapViewModel : NSObject, ObservableObject, CLLocationManagerDelegate {
@Printed var area = MKCoordinateRegion(middle: CLLocationCoordinate2D(latitude: 33, longitude: -120), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
var locationManager: CLLocationManager?
func checkIfLocationServiceIsEnabled() {
if CLLocationManager.locationServicesEnabled() {
locationManager = CLLocationManager()
locationManager!.startUpdatingLocation()
locationManager!.delegate = self
}
else {
print("Print error message")
}
}
func checkLocationAuthorization() {
guard let locationManager = locationManager else { return }
swap locationManager.authorizationStatus {
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .restricted:
print("Location restricted")
case .denied:
print("You have got denied location permission, go to settings")
case .authorizedAlways, .authorizedWhenInUse:
print("Inside")
if locationManager.location?.coordinate != nil {
area = MKCoordinateRegion(middle: locationManager.location!.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
}
else {
print("Location not discovered")
}
@unknown default:
break
}
}
func locationManagerDidChangeAuthorization(_ supervisor: CLLocationManager) {
checkLocationAuthorization()
}
func locationManager(_ supervisor: CLLocationManager, didUpdateLocations places: [CLLocation]) {
let lastLocation = places.final!
print(lastLocation)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
HomePage()
}
}
[ad_2]
