gpt4 book ai didi

mkmapview - 在 SwiftUI 中为 MKMapView 添加 "show user location"按钮的正确方法是什么?

转载 作者:行者123 更新时间:2023-12-05 07:18:50 28 4
gpt4 key购买 nike

我正在尝试创建一个带有 MKMapView 和一个导航到用户位置的按钮的 SwiftUI View 。以下是我的全部代码。我创建了一个名为 MapView 的 View ,它符合 UIViewRepresentable 以保存我的 MKMapView。我有一个位置按钮,点击它会将 shouldNavigateToUserLocation 设置为 true。这会导致 UI 重新加载,如果 shouldNavigateToUserLocation 为真,我的 MapView 会导航到用户位置。然后,它将 shouldNavigateToUserLocation 设置为 false,这样 MapView 就不会在状态发生变化时不断移动到用户位置。

这种方法在真实设备上运行时似乎有效,但我收到警告“在 View 更新期间修改状态,这将导致未定义的行为。”在第 87 行,即 shouldNavigateToUserLocation = false。这是可以理解的,但我的问题是,我怎样才能避免这种情况?我似乎无法找到一种方法来重组我的代码,因此我没有违反在 View 更新期间不修改状态的规则,同时当且仅当用户按下按钮时仍然让 map 导航到用户位置。

我尝试了几种不同的方法,但主要是我的 MapView 和 Controller 类实际上都没有直接访问 MKMapView 的问题。我理解 SwiftUI 中的原因,但这确实限制了我的能力。

这是我的全部代码:

import SwiftUI
import MapKit

struct ContentView: View {

@State var currentlyDisplayingLocationAuthorizationRequest = false
@State var shouldNavigateToUserLocation = false

let locationManager = CLLocationManager()

var body: some View {
ZStack {
MapView(currentlyDisplayingLocationAuthorizationRequest: $currentlyDisplayingLocationAuthorizationRequest,
shouldNavigateToUserLocation: $shouldNavigateToUserLocation)

HStack {
Spacer()
VStack {
Spacer()
Button(action: {
self.checkForLocationAuthorizationAndNavigateToUserLocation()
}) {
Image(systemName: "location")
.imageScale(.large)
.accessibility(label: Text("Locate Me"))
.padding()
}
.background(Color.gray)
.cornerRadius(10)
.padding()
}
}
}
.onAppear {
self.shouldNavigateToUserLocation = true
}
}

func checkForLocationAuthorizationAndNavigateToUserLocation() {
currentlyDisplayingLocationAuthorizationRequest = false

if CLLocationManager.authorizationStatus() == .notDetermined {
print("location authorization not determined")
currentlyDisplayingLocationAuthorizationRequest = true
locationManager.requestWhenInUseAuthorization()
return
}

shouldNavigateToUserLocation = true
}

}

struct MapView: UIViewRepresentable {

@Binding var currentlyDisplayingLocationAuthorizationRequest: Bool
@Binding var shouldNavigateToUserLocation: Bool

let locationManager = CLLocationManager()

func makeUIView(context: Context) -> MKMapView {
let map = MKMapView()
map.delegate = context.coordinator
map.showsUserLocation = true
return map
}

func updateUIView(_ uiView: MKMapView, context: Context) {
if !currentlyDisplayingLocationAuthorizationRequest && shouldNavigateToUserLocation {
moveToUserLocation(map: uiView)
}
}

func makeCoordinator() -> Coordinator {
Coordinator(self)
}

// MARK: Location -

private func moveToUserLocation(map: MKMapView) {
guard let location = locationManager.location else { return }

let region = MKCoordinateRegion(center: location.coordinate,
span: MKCoordinateSpan(latitudeDelta: 0.02,
longitudeDelta: 0.02))
map.setRegion(region, animated: true)
shouldNavigateToUserLocation = false
}

// MARK: Coordinator -

final class Coordinator: NSObject, MKMapViewDelegate {

var control: MapView

init(_ control: MapView) {
self.control = control
}

}

}

最佳答案

这不是您要搜索的内容,但您应该看看 my recent answer另一个相关问题(How to add a move back to user location button in swiftUI?)。我认为它可以帮助您或其他面临同样问题的人。

关于mkmapview - 在 SwiftUI 中为 MKMapView 添加 "show user location"按钮的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58126832/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com