Monday, July 1, 2024
HomeiOS Developmentios - CoreLocation module all the time fails to fetch my telephone's...

ios – CoreLocation module all the time fails to fetch my telephone’s actual location precisely


The CoreLocation module all the time fails to fetch my telephone’s actual location precisely, and the situation proven by showUserLocation all the time has a slight deviation.

The code of LocationViewModel.swift

import Basis
import CoreLocation
import Mix

class LocationViewModel: NSObject, ObservableObject, CLLocationManagerDelegate {
    non-public let locationManager = CLLocationManager()

    @Printed var location: Location?
    @Printed var authorizationStatus: CLAuthorizationStatus?
    @Printed var errorMessage: String?
    @Printed var gpsSignalStatus: String = "Detecting GPS sign..."

    override init() {
        tremendous.init()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
        locationManager.allowsBackgroundLocationUpdates = true
        locationManager.pausesLocationUpdatesAutomatically = false
        checkAuthorizationStatus()
    }
    
    func checkAuthorizationStatus() {
        authorizationStatus = locationManager.authorizationStatus
        change authorizationStatus {
        case .authorizedWhenInUse, .authorizedAlways:
            locationManager.startUpdatingLocation()
            locationManager.startUpdatingHeading()
        case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
        case .denied, .restricted:
            errorMessage = "The authorization of location is restricted。Please flip it on in settings"
        default:
            errorMessage = "Unknown location authorization standing"
        }
    }

    func locationManager(_ supervisor: CLLocationManager, didChangeAuthorization standing: CLAuthorizationStatus) {
        checkAuthorizationStatus()
    }

    func locationManager(_ supervisor: CLLocationManager, didUpdateLocations places: [CLLocation]) {
        guard let location = places.final else { return }
        self.location = Location(
            coordinate: location.coordinate,
            altitude: location.altitude,
            velocity: location.velocity,
            route: location.course,
            horizontalAccuracy: location.horizontalAccuracy,
            timestamp: location.timestamp
        )
        
        if location.horizontalAccuracy < 0 {
            gpsSignalStatus = "GPS is just not avaliable"
        } else if location.horizontalAccuracy > 100 {
            gpsSignalStatus = "GPS sign is weak."
        } else {
            gpsSignalStatus = "GPS sign is nice."
        }
    }
    
    func locationManager(_ supervisor: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
        if var currentLocation = self.location {
            currentLocation.route = newHeading.trueHeading
            self.location = currentLocation
        }
    }

    func locationManager(_ supervisor: CLLocationManager, didFailWithError error: Error) {
        print("Failed to search out person's location: (error.localizedDescription)")
    }
}

The code of LocationView.swift

...
struct MapView: View {
    @ObservedObject var viewModel: LocationViewModel
    
    @State var cameraPosition: MapCameraPosition = .automated
    
    var physique: some View {
        ZStack {
            Map(place: $cameraPosition) {
                if let coordinate = viewModel.location?.coordinate {
                    Annotation("MyLocation", coordinate: CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)) {
                        Textual content("The place which CoreLocation received")
                    }
                    
                }
            }
            .onReceive(viewModel.location.writer) { location in
                cameraPosition = .area(MKCoordinateRegion(
                    heart: location.coordinate,
                    span: MKCoordinateSpan(latitudeDelta: 0.0025, longitudeDelta: 0.0025)
                ))
            }
        }
    }
}
...

The place obtained from CoreLocation has an error of a number of hundred meters in comparison with the precise place.

  1. Any drawback in my code?
  2. Is there a solution to acquire the fitting location?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments