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

后端 未结 7 1550
梦如初夏
梦如初夏 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 07:36

    You can use RxCoreLocation:

    import UIKit
    import GoogleMaps
    import RxCoreLocation
    import RxSwift
    
    class MapViewController: UIViewController {
        private var mapView: GMSMapView?
        private let disposeBag = DisposeBag()
        private let manager = CLLocationManager()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            manager.requestWhenInUseAuthorization()
            manager.startUpdatingLocation()
    
            let camera = GMSCameraPosition.camera(withLatitude: 0, longitude: 0, zoom: 17.0)
            mapView = GMSMapView.map(withFrame: .zero, camera: camera)
            view = mapView
    
            manager.rx
                .didUpdateLocations
                .subscribe(onNext: { [weak self] in
                    guard let location = $0.locations.last else { return }
                    let camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude, longitude: location.coordinate.longitude, zoom: 17.0)
                    self?.mapView?.animate(to: camera)
                    self?.manager.stopUpdatingLocation()
                })
                .disposed(by: disposeBag)
        }
    }
    

提交回复
热议问题