使用未解析的标识符“地图"SwiftUI [英] Use of unresolved identifier 'Map' SwiftUI
问题描述
我正在关注 SwiftUI 的官方教程和遇到错误消息使用未解析的标识符地图".即使我复制并粘贴教程中的代码,它仍然给我错误.我已经查看了一些类似问题的解决方案,但似乎找不到任何可行的方法.代码如下.
I am following the official tutorial for SwiftUI and have run into the error message 'Use of unresolved identifier 'Map'. Even when I copy and and paste the code from the tutorial, it is still giving me the error. I have looked at a few solutions for similar issues and can't seem to find anything that will work. Code below.
import SwiftUI
import MapKit
struct MapView: View {
@State private var region = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 34.011_286, longitude: -116.166_868),
span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2)
)
var body: some View {
Map(coordinateRegion: $region)
}
}
struct MapView_Previews: PreviewProvider {
static var previews: some View {
MapView()
}
}
如果这真的很明显,我很抱歉 - 但我是 Swift/SwiftUI 的新手,看不出问题出在哪里.提前致谢!
I apologise if this is really obvious - but I'm new to Swift/SwiftUI, and can't see where the issue is coming from. Thanks in advance!
推荐答案
在这种情况下,我也遇到了麻烦.因此,我使用 UIViewRepresentable 和 2 种方法创建 View 方法:'makeUIView' 和 'updateUIView'.
I also got into trouble in this case. So, I make View method using UIViewRepresentable with 2 methods: 'makeUIView' and 'updateUIView'.
地图视图
import SwiftUI
import MapKit
struct MapViewUI: UIViewRepresentable {
func makeUIView(context: Context) -> MKMapView {
MKMapView(frame: .zero)
}
func updateUIView(_ view: MKMapView, context: Context) {
let coordinate = CLLocationCoordinate2D(
latitude: 2.6540427, longitude: 98.8932576)
let span = MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 2.0)
let region = MKCoordinateRegion(center: coordinate, span: span)
view.setRegion(region, animated: true)
}
}
而且,你可以像这样调用你的视图
And, You can call your View looks like this
struct MapPageView: View {
var body: some View {
VStack {
MapViewUI()
.frame(height: 300)
}
}
}
这篇关于使用未解析的标识符“地图"SwiftUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!