在selectedSegment中显示地图类型 SwiftUi

Can anybody help me.with this code.I need change type of map like hybrid or standard in selectedSegmented but I cannot.

struct MapView: View {

    @State private var selectedSegment = 0
    @State private var kind_of_Map = ["hybridFlyover", "standard"]

    var body: some View {

        VStack {
            Picker(selection: $selectedSegment, label: Text("")) {
                ForEach(0..<kind_of_Map.count) { index in
                    Text(self.kind_of_Map[index]).tag(index)
                }
            }.pickerStyle(SegmentedPickerStyle())

            Text("Selected: \(kind_of_Map[selectedSegment])")

            MapViewController()
        }
    }
}

struct MapViewController: UIViewRepresentable {


    func makeUIView(context: Context) -> MKMapView {

        let mapView = MKMapView(frame: .zero)
        mapView.mapType = .kind_of_Map
        mapView.showsScale = true
        mapView.showsTraffic = true
        mapView.showsCompass = true
        mapView.showsUserLocation = true
        mapView.showsBuildings = true
        return mapView

    }

非常感谢你。

解决方案:

我下面写的代码就可以了。

struct ContentView: View {

    @State private var selectedSegment = 0
    @State var mapTypeItems: [String: MKMapType] = ["Hybrid": .hybrid, "Standart": .standard, "Hybrid Flyover": .hybridFlyover]

    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])
    }
}

struct MapView: UIViewRepresentable {

    @Binding var mapType: MKMapType

    func makeUIView(context: Context) -> MKMapView {
        let mapView = MKMapView()
        mapView.mapType = mapType
        mapView.showsScale = true
        mapView.showsTraffic = true
        mapView.showsCompass = true
        mapView.showsUserLocation = true
        mapView.showsBuildings = true
        return mapView
    }

    func updateUIView(_ uiView: MKMapView, context: Context) {
        uiView.mapType = self.mapType
    }
}

给TA打赏
共{{data.count}}人
人已打赏
未分类

即使在我的边距上使用了大众单位,Div也会偏离屏幕。

2022-9-14 4:46:42

未分类

使用Firebase扩展更新图片大小后的个人资料照片。

2022-9-14 4:57:19

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索