How to “Show my current location on google maps, when I open the ViewController?” in Swift?

后端 未结 7 1544
梦如初夏
梦如初夏 2020-12-14 07:28

I am using Google maps sdk of iOS(Swift).

Has anyone know how to \"Show my current location on google maps, when I open the ViewController\"?

Actually it jus

相关标签:
7条回答
  • 2020-12-14 08:00

    SwiftUI

    struct GoogleMapView: UIViewRepresentable {
      @State var coordinator = Coordinator()
    
      func makeUIView(context _: Context) -> GMSMapView {
        let view = GMSMapView(frame: .zero)
        view.isMyLocationEnabled = true
        view.animate(toZoom: 18)
        view.addObserver(coordinator, forKeyPath: "myLocation", options: .new, context: nil)
      }
    
      func updateUIView(_ uiView: GMSMapView, context _: UIViewRepresentableContext<GoogleMapView>) {}
    
      func makeCoordinator() -> GoogleMapView.Coordinator {
        return coordinator
      }
    
      static func dismantleUIView(_ uiView: GMSMapView, coordinator: GoogleMapView.Coordinator) {
        uiView.removeObserver(coordinator, forKeyPath: "myLocation")
      }
    
      final class Coordinator: NSObject {
        override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
          if let location = change?[.newKey] as? CLLocation, let mapView = object as? GMSMapView {
            mapView.animate(toLocation: location.coordinate)
          }
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题