我是 Swift 新手,所以由于我的經驗不足,這可能是個問題。我正在嘗試在 MapView 結構中系結一個 MKCoordinateRegion 變數,以便地圖自動以用戶為中心,但是即使將區域正確分配給用戶的位置,setRegion() 似乎也不起作用(我已經確定通過在 updateUIView 函式中放置列印陳述句)。基本上,updateUIView 中的 setRegion() 函式目前對更改默認區域沒有任何作用。當我打開模擬時,即使允許設備共享位置,地圖仍位于默認起始位置。我已經嘗試將區域設定為 updateUIView 函式中的隨機硬編碼位置,這可以正常作業,但是嘗試將其設定為系結區域變數的一些事情并沒有 t似乎有任何影響。我錯過了什么?
地圖視圖結構:
struct MapView: UIViewRepresentable {
typealias UIViewType = MKMapView
@Binding var region: MKCoordinateRegion
let mapView = MKMapView()
func makeUIView(context: Context) -> MKMapView {
mapView.setRegion(region, animated: true)
mapView.showsUserLocation = true
return mapView
}
func updateUIView(_ uiView: MKMapView, context: Context) {
mapView.setRegion(region, animated: true)
}
}
內容視圖:
struct ContentView: View {
@StateObject private var viewModel = ContentViewModel()
var body: some View {
VStack {
Text("Adventure Program")
.font(.largeTitle)
.multilineTextAlignment(.center)
MapView(region: $viewModel.region)
.onAppear {
viewModel.checkIfLocationServicesIsEnabled()
}
}.background(
Image("background")
.resizable()
.edgesIgnoringSafeArea(.all)
.scaledToFill())
}
}
內容視圖模型:
import MapKit
import SwiftUI
enum MapDetails {
static let startingLocation = CLLocationCoordinate2D(
latitude: 40.7,
longitude: -74)
static let defaultSpan = MKCoordinateSpan(
latitudeDelta: 1,
longitudeDelta: 1)
static let defaultRadius = 4000.0
}
final class ContentViewModel: NSObject, ObservableObject, CLLocationManagerDelegate {
var locationManager: CLLocationManager?
@Published var region = MKCoordinateRegion(
center: MapDetails.startingLocation,
span: MapDetails.defaultSpan)
func checkIfLocationServicesIsEnabled() {
if CLLocationManager.locationServicesEnabled() {
locationManager = CLLocationManager()
locationManager!.delegate = self
} else {
print("Show alert about this being off!")
}
}
private func checkLocationAuthorization() {
guard let locationManager = locationManager else { return }
switch locationManager.authorizationStatus {
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .restricted:
print("Your location is restricted")
case .denied:
print("You have denied this app's location permission. Go into settings to change it.")
case .authorizedAlways, .authorizedWhenInUse:
region = MKCoordinateRegion(
center: locationManager.location!.coordinate,
span: MapDetails.defaultSpan)
@unknown default:
break
}
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
checkLocationAuthorization()
}
}
uj5u.com熱心網友回復:
嗨 Messenger_ 歡迎來到堆疊溢位!
在做了一些除錯之后,我找到了解決方案,使用 DispatchQueue 為我解決了它。
因此,我將其添加到MapView結構中,如下所示:
struct MapView: UIViewRepresentable {
typealias UIViewType = MKMapView
@Binding var region: MKCoordinateRegion
var locationManager = CLLocationManager()
func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView()
mapView.showsUserLocation = true
return mapView
}
func updateUIView(_ view: MKMapView, context: Context) {
DispatchQueue.main.async {
mapView.setRegion(region, animated: false)
}
}
}
希望這可以幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/516905.html
