mapType SwiftUI ButtonSegmentedControl。

问题描述 投票:0回答:1

正在寻找改变buttonsegmented控件选择的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

}

}

谢谢你的帮助

swiftui mapkit mkmapview mkmapviewdelegate segmentedcontrol
1个回答
0
投票

把代码改成更简单的形式。现在每次启动时都有相同的选项,并相应更新地图。把答案贴出来,帮助其他遇到这个问题的人。

ContentView.swift

struct MapTypeSelect {
    var title: String
    var map: MKMapType
}

struct ContentView: View {

    @State private var selectedSegment = 0
    @State private var mapSelect = [MapTypeSelect(title: "Standard", map: .standard), MapTypeSelect(title: "Satellite", map: .satellite), MapTypeSelect(title: "Hybrid", map: .hybrid)]


    var body: some View {

    VStack {
        HStack {
            Picker(selection: $selectedSegment, label: EmptyView()) {
                ForEach(0 ..< mapSelect.count) {
                    Text(self.mapSelect[$0].title).tag($0)
                }
            }.pickerStyle(SegmentedPickerStyle())

        }
            MapView(mapType: mapSelect[selectedSegment].map)
                    .edgesIgnoringSafeArea(.all)
        }
    }
}

MapView.swift

struct MapView: UIViewRepresentable {

    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.showsUserLocation = 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

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.