寻找更改按钮/分段控件选择上的mapType的最佳选项。目前,我有下面的代码在一定程度上起作用,但是选择器选项始终在旋转。如何使其与标准,卫星,混合标准(默认)或用户最后选择的选项保持一致?
ContentView.swift
struct ContentView: View {
@State private var selectedSegment = 0
@State var maptypeitems: [String: MKMapType] = ["Hybrid": .hybrid,"Standard": .standard,"Satellite": .satellite]
var body: some View {
VStack {
Picker(selection: $selectedSegment,label: Text("")) {
ForEach(0..<maptypeitems.count) { index in
Text(self.getMapType(index: index).key)
}
}.pickerStyle(SegmentedPickerStyle())
MapView(mapType: getMapType(index: self.selectedSegment).value)
.edgesIgnoringSafeArea(.all)
}
}
func getMapType(index: Int) -> (key: String,value: Binding<MKMapType>) {
let indexItem = maptypeitems.index(maptypeitems.startIndex,offsetBy: index)
return (maptypeitems.keys[indexItem],$maptypeitems.values[indexItem])
}
}
MapView.swift
struct MapView: UIViewRepresentable {
@Binding var mapType: MKMapType
let map = MKMapView(frame: .zero)
func makeCoordinator() -> MapViewCoordinator {
MapViewCoordinator(self)
}
func makeUIView(context: Context) -> MKMapView{
map.mapType = mapType
map.showsScale = true
map.showsTraffic = true
map.showsCompass = true
map.showsBuildings = true
let center = CLLocationCoordinate2D(latitude: -33.3208,longitude: 151.2336)
let region = MKCoordinateRegion(center: center,latitudinalMeters: 55000,longitudinalMeters: 55000)
map.setCameraBoundary(
MKMapView.CameraBoundary(coordinateRegion: region),animated: true)
let zoomRange = MKMapView.CamerazoomRange(maxCenterCoordinateDistance: 200000)
map.setCamerazoomRange(zoomRange,animated: true)
map.region = region
map.register(
ArtworkView.self,forAnnotationViewWithReuseIdentifier:
MKMapViewDefaultAnnotationViewReuseIdentifier)
loadInitialData()
map.addAnnotations(artworks)
return map
}
func updateUIView(_ view: MKMapView,context: Context){
view.delegate = context.coordinator
view.addAnnotations(artworks)
view.mapType = self.mapType
}
}
感谢您的帮助