简体   繁体   English

MapView 不显示用户当前位置

[英]MapView does not show user current location

I was trying to display my current location into a Swiftui MapView.我试图将我的当前位置显示到 Swiftui MapView 中。 To do so, I created the following class:为此,我创建了以下类:

import SwiftUI
import CoreLocation
import Combine

class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {

@Published var locationManager = CLLocationManager()
@Published var locationStatus: CLAuthorizationStatus?
@Published var lastLocation: CLLocation?

override init() {
    super.init()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
}

private var statusString: String {
    guard let status = locationStatus else {
        return "unknown"
    }
    
    switch status {
    case .notDetermined: return "notDetermined"
    case .authorizedWhenInUse: return "authorizedWhenInUse"
    case .authorizedAlways: return "authorizedAlways"
    case .restricted: return "restricted"
    case .denied: return "denied"
    default: return "unknown"
    }
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    locationStatus = status
    print(#function, statusString)
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let location = locations.first else { return }
    
    lastLocation = location
    
    //        fetchLocation(location: lastLocation)
    
    self.locationManager.stopUpdatingLocation()
    print(#function, location)
}
}

and the following view:和以下视图:

    var body: some View {
    
    VStack {
        
        Text("Latitude: \(locationManager.lastLocation?.coordinate.latitude ?? 0), Longitude: \(locationManager.lastLocation?.coordinate.longitude ?? 0)")
            .onAppear{
                print("DEBUG: status 1 : \(locationManager.lastLocation?.coordinate.latitude ?? 0)")
            }
        
        MapView(lat: (locationManager.lastLocation?.coordinate.latitude ?? 0), lon: locationManager.lastLocation?.coordinate.longitude ?? 0, latDelta: 0.05, lonDelta: 0.05)
            .frame(width: UIScreen.screenWidth - 36, height: UIScreen.screenWidth / 2)
            .cornerRadius(10)
            .onAppear{
                print("DEBUG: status 2 : \(locationManager.locationStatus)")
                print("DEBUG: lat: \(locationManager.lastLocation?.coordinate.latitude), lon: \(locationManager.lastLocation?.coordinate.longitude)")
            }
        
    }
    
}

So far, the Textfield does show the correct coordinates, but my Mapview shows NIL as the coordinates.到目前为止,Textfield 确实显示了正确的坐标,但我的 Mapview 将 NIL 显示为坐标。

Adding my MapView here as well for completeness:为了完整起见,在这里也添加我的 MapView :

struct MapView: UIViewRepresentable {
    
    @State var lat = 0.0
    @State var lon = 0.0
    @State var latDelta = 0.05
    @State var lonDelta = 0.05
    
    
    func makeUIView(context: Context) -> MKMapView {
        MKMapView(frame: .zero)
    }

    func updateUIView(_ uiView: MKMapView, context: Context) {
        let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon )
        let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta)
        let region = MKCoordinateRegion(center: coordinate, span: span)
        uiView.setRegion(region, animated: true)
    }
}

Hope you could have a look and see if I have missed anything.希望你能看一看,看看我是否遗漏了什么。

The answer is:答案是:

Remove all the @State from the MapView():从 MapView() 中删除所有 @State:

struct MapView: UIViewRepresentable {
    
    var lat = 0.0
    var lon = 0.0
    var latDelta = 0.05
    var lonDelta = 0.05
    
    
    func makeUIView(context: Context) -> MKMapView {
        MKMapView(frame: .zero)
    }

    func updateUIView(_ uiView: MKMapView, context: Context) {
        let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon )
        let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta)
        let region = MKCoordinateRegion(center: coordinate, span: span)
        uiView.setRegion(region, animated: true)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM