簡體   English   中英

MapView 不顯示用戶當前位置

[英]MapView does not show user current location

我試圖將我的當前位置顯示到 Swiftui MapView 中。 為此,我創建了以下類:

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)
}
}

和以下視圖:

    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)")
            }
        
    }
    
}

到目前為止,Textfield 確實顯示了正確的坐標,但我的 Mapview 將 NIL 顯示為坐標。

為了完整起見,在這里也添加我的 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)
    }
}

希望你能看一看,看看我是否遺漏了什么。

答案是:

從 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