Googlemaps 委托方法从未在 iOS SwiftUI 中触发

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

通过SPM整合Google地图,地图似乎正确加载,地图上几乎没有标记,但我想知道地图何时完全加载以及地图何时移动等..

为此,我们可以使用 GoogleMaps 委托方法,例如

mapViewDidFinishTileRendering(_ mapView: GMSMapView)
mapViewSnapshotReady(_ mapView: GMSMapView)

但是这些函数从未被调用过。

这是我的实现

import UIKit
import GoogleMaps

class MapViewController: UIViewController {
    var options = GMSMapViewOptions()
    var map = GMSMapView()
    var isAnimating: Bool = false

    override func loadView() {
      super.loadView()
      options.camera = GMSCameraPosition.camera(withLatitude: 18.659553, longitude: 78.105868, zoom: 17.0)
      options.frame = self.view.bounds
      map = GMSMapView(options: options)
      self.view = map
    }
}

import Foundation
import SwiftUI
import GoogleMaps

struct MapViewControllerBridge: UIViewControllerRepresentable {
    
    @Binding var markers: [GMSMarker]
    @Binding var selectedMarker: GMSMarker?
    var onAnimationEnded: () -> ()
    var mapViewWillMove: (Bool) -> ()

    func makeUIViewController(context: Context) -> MapViewController {
        let uiViewController = MapViewController()
        uiViewController.map.delegate = context.coordinator
        return uiViewController
    }

    func updateUIViewController(_ uiViewController: MapViewController, context: Context) {
        markers.forEach { $0.map = uiViewController.map }
        selectedMarker?.map = uiViewController.map
        animateToSelectedMarker(viewController: uiViewController)
    }
    
    func makeCoordinator() -> MapViewCoordinator {
      return MapViewCoordinator(self)
    }
    
    private func animateToSelectedMarker(viewController: MapViewController) {
      guard let selectedMarker = selectedMarker else {
        return
      }

      let map = viewController.map
      if map.selectedMarker != selectedMarker {
        map.selectedMarker = selectedMarker
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
          map.animate(toZoom: kGMSMinZoomLevel)
          DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            map.animate(with: GMSCameraUpdate.setTarget(selectedMarker.position))
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
              map.animate(toZoom: 12)
              DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
                onAnimationEnded()
              })
            })
          }
        }
      }
    }
    
    final class MapViewCoordinator: NSObject, GMSMapViewDelegate {
        var mapViewControllerBridge: MapViewControllerBridge
        
        init(_ mapViewControllerBridge: MapViewControllerBridge) {
            self.mapViewControllerBridge = mapViewControllerBridge
        }
        
        func mapView(_ mapView: GMSMapView, willMove gesture: Bool) {
            self.mapViewControllerBridge.mapViewWillMove(gesture)
        }
        
        func mapViewDidFinishTileRendering(_ mapView: GMSMapView) {
            print("Map Finished Rendering..")
        }
        
        func mapViewSnapshotReady(_ mapView: GMSMapView) {
            print("Map View Snapshot Ready..")
        }
    }
}

从ContentView文件调用MapViewControllerBridge

MapViewControllerBridge(markers: $markers, selectedMarker: $selectedMarker, onAnimationEnded: {
                  self.zoomInCenter = true
                }, mapViewWillMove: { (isGesture) in
                  guard isGesture else { return }
                  self.zoomInCenter = false
                })
                .ignoresSafeArea()
ios google-maps swiftui google-maps-sdk-ios
1个回答
0
投票

如果我删除 loadView() 并使用 init() 代替 MapViewController 类,它似乎可以工作。

class MapViewController: UIViewController {
    var options = GMSMapViewOptions()
    var map = GMSMapView()
    var isAnimating: Bool = false

    init() {
      super.init(nibName: nil, bundle: nil)
      options.camera = GMSCameraPosition.camera(withLatitude: 18.659553, longitude: 78.105868, zoom: 17.0)
      options.frame = self.view.bounds
      map = GMSMapView(options: options)
      self.view = map
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.