尝试在 func makeCoordinator() 中访问 EnviromentObject
Trying access EnviromentObject inside func makeCoordinator()
我正在尝试连续计算距离(来自:the_userLocation),其中固定 lat/long 在 ContentView 中输入。
我可以在 struct MapView: UIViewRepresentable {
内的函数中执行一次,但我想进行连续计算。我以为我可以在里面做这个
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation)
里面
class MapViewCoordinator: NSObject, MKMapViewDelegate
我在一个文件中定义了 EnvironmentObject
import SwiftUI
final class AppData: ObservableObject { // Don't know why final
@Published var cornerLatitude: String = ""
@Published var cornerLongitude: String = ""
@Published var corner: Int = 1
}
在 MapView 文件的顶部,我包含了 EnvironmentObject
import MapKit
struct MapView: UIViewRepresentable {
@EnvironmentObject var appData: AppData
var map = MKMapView()
var locationManager: CLLocationManager!
init() {
locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
}
在 class MapViewCoordinator func mapView 中,如果我取消注释 let cornerLat =
和 let cornerLon =
行,我会得到错误
Instance member 'appData' of type 'MapView' cannot be used on instance of nested type 'MapView.MapViewCoordinator'
我已经尝试了其他 'combinations' 代码,结果相似。
let map: MKMapView
init(map:MKMapView) {
self.map = map
}
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
let location = map.userLocation
let region = MKCoordinateRegion(center: location.coordinate, latitudinalMeters: 500, longitudinalMeters: 500)
map.setRegion(region, animated: true)
// let cornerLat = Double(appData.cornerLatitude) ?? 0.0
// let cornerLon = Double(appData.cornerLongitude) ?? 0.0
}
}
任意 thoughts/suggestions。谢谢
如果我正确理解提供的代码,那么您可以执行以下操作(因为 AppData 是引用类型)
Scratchy(未经测试,只是想法)
class MapViewCoordinator {
let map: MKMapView
let appData: AppData // here !!
init(map:MKMapView, appData: AppData) {
self.map = map
self.appData = appData // << here !!
}
// ... use where needed as own property
并在
中创建
struct MapView: UIViewRepresentable {
@EnvironmentObject var appData: AppData
var map = MKMapView()
...
func makeCoordinator() -> MapViewCoordinator {
MapViewCoordinator(map: map, appData: appData) // << inject !!
}
我正在尝试连续计算距离(来自:the_userLocation),其中固定 lat/long 在 ContentView 中输入。
我可以在 struct MapView: UIViewRepresentable {
内的函数中执行一次,但我想进行连续计算。我以为我可以在里面做这个
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation)
里面
class MapViewCoordinator: NSObject, MKMapViewDelegate
我在一个文件中定义了 EnvironmentObject
import SwiftUI
final class AppData: ObservableObject { // Don't know why final
@Published var cornerLatitude: String = ""
@Published var cornerLongitude: String = ""
@Published var corner: Int = 1
}
在 MapView 文件的顶部,我包含了 EnvironmentObject
import MapKit
struct MapView: UIViewRepresentable {
@EnvironmentObject var appData: AppData
var map = MKMapView()
var locationManager: CLLocationManager!
init() {
locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
}
在 class MapViewCoordinator func mapView 中,如果我取消注释 let cornerLat =
和 let cornerLon =
行,我会得到错误
Instance member 'appData' of type 'MapView' cannot be used on instance of nested type 'MapView.MapViewCoordinator'
我已经尝试了其他 'combinations' 代码,结果相似。
let map: MKMapView
init(map:MKMapView) {
self.map = map
}
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
let location = map.userLocation
let region = MKCoordinateRegion(center: location.coordinate, latitudinalMeters: 500, longitudinalMeters: 500)
map.setRegion(region, animated: true)
// let cornerLat = Double(appData.cornerLatitude) ?? 0.0
// let cornerLon = Double(appData.cornerLongitude) ?? 0.0
}
}
任意 thoughts/suggestions。谢谢
如果我正确理解提供的代码,那么您可以执行以下操作(因为 AppData 是引用类型)
Scratchy(未经测试,只是想法)
class MapViewCoordinator {
let map: MKMapView
let appData: AppData // here !!
init(map:MKMapView, appData: AppData) {
self.map = map
self.appData = appData // << here !!
}
// ... use where needed as own property
并在
中创建struct MapView: UIViewRepresentable {
@EnvironmentObject var appData: AppData
var map = MKMapView()
...
func makeCoordinator() -> MapViewCoordinator {
MapViewCoordinator(map: map, appData: appData) // << inject !!
}