【问题标题】:Update iOS Google Maps mapStyle on environment colorScheme change (dark mode)更新 iOS Google Maps mapStyle on environment colorScheme change (dark mode)
【发布时间】:2021-12-03 13:40:05
【问题描述】:

在浅色模式和深色模式之间更改设备设置时,我的 Google 地图 mapStyle 没有更新。其他视图成功切换到所选模式,只是地图没有变化。

当我的应用重新启动时,mapStyle 确实会正确更改。

struct MapView: UIViewRepresentable {

  @Environment(\.colorScheme) var colorScheme

  private let mapView = GMSMapView(frame: .zero)
  private let defaultZoomLevel: Float = 10

  func makeUIView(context: Context) -> GMSMapView {
    
    mapView.delegate = context.coordinator
    applyColorSchemeToMap()
  
    return mapView
  }

  func updateUIView(_ mapView: GMSMapView, context: Context) {
    
    applyColorSchemeToMap()
  }
  
  func applyColorSchemeToMap() {
    do {
      if let styleURL = Bundle.main.url(forResource: colorScheme == .dark ? "night_map_style" : "map_style", withExtension: "json") {
        mapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
      } else {
        NSLog("Unable to find style.json")
      }
    } catch {
      NSLog("One or more of the map styles failed to load. \(error)")
    }
  }

【问题讨论】:

    标签: swift swiftui google-maps-sdk-ios


    【解决方案1】:

    这里有一个适合你的方法:

    struct ContentView: View {
    
        @Environment(\.colorScheme) var colorScheme
    
        var body: some View {
    
            MapView(colorScheme: colorScheme)
            
        }
    }
    
    
    
    struct MapView: UIViewRepresentable {
    
        var colorScheme: ColorScheme
        private let mapView = GMSMapView(frame: .zero)
        private let defaultZoomLevel: Float = 10
    
        func makeUIView(context: Context) -> GMSMapView {
    
            mapView.delegate = context.coordinator
            applyColorSchemeToMap()
    
            return mapView
        }
    
        func updateUIView(_ mapView: GMSMapView, context: Context) {
            applyColorSchemeToMap(colorScheme: colorScheme)
        }
    
        func applyColorSchemeToMap(colorScheme: ColorScheme) {
            do {
                if let styleURL = Bundle.main.url(forResource: (colorScheme == .dark) ? "night_map_style" : "map_style", withExtension: "json") {
                    mapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
                } else {
                    NSLog("Unable to find style.json")
                }
            } catch {
                NSLog("One or more of the map styles failed to load. \(error)")
            }
        }
    }
    

    【讨论】:

    • 这似乎没有任何改变。我正在模拟器而不是物理设备上进行测试。不知道有没有影响?
    【解决方案2】:

    我发现了问题。 MapView 必须符合 UIViewControllerRepresentable,而不是 UIViewRepresentable。

    可以在此处找到有关如何实现此目的的指南(使用 ViewControllerBridge):

    https://developers.google.com/codelabs/maps-platform/maps-platform-ios-swiftui#0

    【讨论】:

      猜你喜欢
      • 2021-03-05
      • 2022-12-27
      • 2016-03-11
      • 2022-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-01
      相关资源
      最近更新 更多