在SwiftUI中从可观察视图模型初始化地图
我正试图从视图模型在SwiftUI视图上实现一个在SwiftUI中从可观察视图模型初始化地图,swiftui,mapkit,combine,Swiftui,Mapkit,Combine,我正试图从视图模型在SwiftUI视图上实现一个映射。我在网上找到的每一个例子都是一个坐标。在我的例子中,我正在用Codablestruct初始化一个视图模型,我不知道坐标是什么 我在构建项目时没有遇到编译器问题,但是画布崩溃了。我尝试过关闭Xcode、清理派生数据等,但这似乎无法解决问题 任何关于我的错误所在的建议,我们都非常感激 class EarthquakeViewModel: ObservableObject { @Published private(set) var quak
映射。我在网上找到的每一个例子都是一个坐标。在我的例子中,我正在用Codable
struct初始化一个视图模型,我不知道坐标是什么
我在构建项目时没有遇到编译器问题,但是画布崩溃了。我尝试过关闭Xcode、清理派生数据等,但这似乎无法解决问题
任何关于我的错误所在的建议,我们都非常感激
class EarthquakeViewModel: ObservableObject {
@Published private(set) var quakeData: Feature
@State var region: MKCoordinateRegion
init(quakeData: Feature) {
self.quakeData = quakeData
let center = CLLocationCoordinate2D(latitude: quakeData.geometry.coordinates[0],
longitude: quakeData.geometry.coordinates[1])
let span = MKCoordinateSpan(latitudeDelta: 1.0, longitudeDelta: 1.0)
region = MKCoordinateRegion(center: center,
span: span)
}
public lazy var title: String = {
quakeData.properties.title
}()
}
这是我的ContentView
:
struct EarthquakeView: View {
@ObservedObject var viewModel: EarthquakeViewModel
var body: some View {
VStack {
Text(viewModel.title)
// makeMapView()
Map(coordinateRegion: $viewModel.region)
}
}
}
//我也试过了,但没用
extension EarthquakeView {
@ViewBuilder func makeMapView() -> some View {
Map(coordinateRegion: $viewModel.region)
}
}
更新
这是来自Diagnostics的消息。用Xcode关闭清理派生数据似乎无法解决这个问题,因此我认为我的问题在于我的一个声明:
RemoteHumanReadableError: Failed to update preview.
The preview process appears to have crashed.
Error encountered when sending 'render' message to agent.
==================================
| RemoteHumanReadableError: The operation couldn’t be completed. (BSServiceConnectionErrorDomain error 3.)
|
| BSServiceConnectionErrorDomain (3):
| ==BSErrorCodeDescription: OperationFailed
更新2
我调整了我的数据模型,并从中添加了一个计算区域变量,下面是我现在获取区域的方法:
extension Feature /* Feature is a Codable struct */ {
var region: MKCoordinateRegion {
let center = CLLocationCoordinate2D(latitude: geometry.coordinates[0],
longitude: geometry.coordinates[1])
let span = MKCoordinateSpan(latitudeDelta: 1.0, longitudeDelta: 1.0)
let region = MKCoordinateRegion(center: center,
span: span)
return region
}
}
根据建议,我将视图模型上的区域更新为@Published
class EarthquakeViewModel: ObservableObject {
`@Published private(set) var quakeData: Feature
`@Published var region: MKCoordinateRegion
init(quakeData: Feature) {
self.quakeData = quakeData
region = quakeData.region
}
public lazy var title: String = {
quakeData.properties.title
}()
}
最后,我的看法如下:
struct EarthquakeView: View {
@ObservedObject var viewModel: EarthquakeViewModel
@State var region: MKCoordinateRegion
init(viewModel: EarthquakeViewModel) {
self.viewModel = viewModel
_region = State(initialValue: viewModel.region)
}
var body: some View {
VStack {
Text(viewModel.title)
Map(coordinateRegion: $region)
}
}
}
新的错误是这样的。关闭Xcode、重新启动、清理派生数据等似乎无法解决问题,因此我很快得出结论,我缺少一些基本的东西:
PreviewUpdateTimeDoutorror:更新花费了5秒以上的时间更新
CombineQuake.app(16766)中地震视图的预览
花了5秒多
更新3
预览初始化:
struct EarthquakeView_Previews: PreviewProvider {
static var previews: some View {
let quakeData = EarthQuakeData(mag: 6.5,
place: "32km W of Sola, Vanuatu",
time: 1388592209000,
updated: 1594407529032,
tz: 660,
url: "https://earthquake.usgs.gov/earthquakes/eventpage/usc000lvb5",
detail: "https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=usc000lvb5&format=geojson",
felt: nil,
cdi: nil,
mmi: nil,
alert: nil,
status: "reviewed",
tsunami: 1,
sig: 650,
net: "us",
code: "c0001vb5",
ids: ",pt14001000,at00myqcls,usc000lvb5,",
sources: "pt,at,us",
types: "cap,geoserve,impact-link,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,tectonic-summary",
nst: nil,
dmin: 3.997,
rms: 0.76,
gap: 14.0,
magType: "mww",
type: "earthquake",
title: "M 6.5 - 32km W of Sola, Vanuatu")
let geometry = Geometry(type: "Point",
coordinates: [167.249, -13.8633, 187.0])
let earthquake = Feature(type: "Feature",
properties: quakeData,
geometry: geometry,
id: "usc000lvb5")
let viewModel = EarthquakeViewModel(quakeData: earthquake)
EarthquakeView(viewModel: viewModel)
}
}
如果在模拟器上而不是预览上运行此操作,则会得到一个更有用的错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid Region <center:+167.24900000, -13.86330000 span:+1.00000000, +1.00000000>'
到
而且效果很好
您的纬度167.249超出了-90到90的范围,这是有效的范围。无法在Xcode 12.4上复制,尽管我不得不对您的代码进行一些猜测,因为您没有包含功能或预览本身。不过,我马上注意到您在可观察对象中使用了@State
——应该是@已发布的
,谢谢您的关注。我接受了你的建议。不同的错误,仍然没有映射:(对我来说仍然不可复制(尽管我仍然必须推断代码的某些方面)。您可以演示如何创建预览吗?它在非预览情况下在模拟器上工作吗?感谢您的查看。我使用PreviewProvider
进行了更新。如果我注释掉Map
声明,文本将按预期呈现。预览代码无法编译,因为您包含的代码中缺少所有类型ed在这里,但对我来说没有什么太不寻常的。仍然有兴趣知道这是否只发生在预览或sim/设备上。LOL。USGS会将坐标反向发送回。我非常感谢您的帮助。谢谢。
let geometry = Geometry(type: "Point",
coordinates: [167.249, -13.8633, 187.0])
let geometry = Geometry(type: "Point",
coordinates: [45, 34, 187.0])