【问题标题】:mapType SwiftUI Button/SegmentedControlmapType SwiftUI 按钮/SegmentedControl
【发布时间】:2020-05-27 06:25:55
【问题描述】:

寻找在按钮/分段控件选择上更改 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】:

    将代码更改为更简单的形式。现在每次启动时都可以使用相同的选项,并相应地更新地图。发布答案以帮助遇到此问题的其他人

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

    【讨论】:

      猜你喜欢
      • 2020-02-09
      • 1970-01-01
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      • 2019-12-12
      • 2020-04-11
      • 1970-01-01
      相关资源
      最近更新 更多