如何在 SwiftUI 的 MapKit 地图中设置
mapType
,使其可以显示为 3D 地球仪?
我正在寻找一种方法将
mapType
设置为 .hybridFlyover
:
mapview.mapType = MKMapType.hybridFlyover
但是对于 SwiftUI:
import SwiftUI
import MapKit
struct LocationGlobeView: View {
@State private var cities: [City] = [
City(coordinate: .init(latitude: 40.7128, longitude: 74.0060)),
City(coordinate: .init(latitude: 37.7749, longitude: 122.4194)),
City(coordinate: .init(latitude: 47.6062, longitude: 122.3321))
]
@State private var userTrackingMode: MapUserTrackingMode = .follow
@State private var region = MKCoordinateRegion(
center: CLLocationCoordinate2D(
latitude: 25.7617,
longitude: 80.1918
),
span: MKCoordinateSpan(
latitudeDelta: 10,
longitudeDelta: 10
)
)
var body: some View {
Map(coordinateRegion: $region,
interactionModes: MapInteractionModes.all,
showsUserLocation: true,
userTrackingMode: $userTrackingMode,
annotationItems: cities) { city in
MapAnnotation(coordinate: city.coordinate,
anchorPoint: CGPoint(x: 0.5, y: 0.5)) {
Circle()
.stroke(Color.green)
.frame(width: 44, height: 44)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
struct LocationGlobeView_Previews: PreviewProvider {
static var previews: some View {
LocationGlobeView()
}
}
struct City: Identifiable {
let id = UUID()
let coordinate: CLLocationCoordinate2D
}
如何制作允许地图成为 3D 地球仪的 SwiftUI 视图?
从 macOS 14、iOS 17 开始,您可以在地图视图上使用以下修改器。
.mapStyle(.imagery())