我正在尝试使用新的 Mapkit SwiftUI view我可以显示某个区域的 map ,但我不知道如何显示 Map Marker .
这就是我所拥有的:
import SwiftUI
import MapKit
struct ContentView: View {
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
var location1 = MapMarker(coordinate: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), tint: .red)
var body: some View {
Map(coordinateRegion: $region, showsUserLocation: true).edgesIgnoringSafeArea(.all)
}
}
有谁知道怎么加location1
到 map ?我找到了 this但我无法让它发挥作用
最佳答案
这是一个简单的演示。使用 Xcode 12/iOS 14 测试
import CoreLocation
import MapKit
struct Marker: Identifiable {
let id = UUID()
var location: MapMarker
}
struct DemoView: View {
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
let markers = [Marker(location: MapMarker(coordinate: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), tint: .red))]
var body: some View {
Map(coordinateRegion: $region, showsUserLocation: true,
annotationItems: markers) { marker in
marker.location
}.edgesIgnoringSafeArea(.all)
}
}
关于ios - 在 swiftUI 2 中将 MapMarker 添加到 MapKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62989013/