视图左上角的 Swift GMSMarker 图标

jam*_*z77 2 google-maps cllocationmanager swift

标题是不言自明的。我试过viewDidLayoutSubviews()按照这里的建议调用它,但它没有给我带来任何乐趣。

这是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.requestWhenInUseAuthorization()

    mapView.mapType = .normal
    mapView.settings.zoomGestures   = true
    mapView.settings.tiltGestures   = true
    mapView.settings.rotateGestures = true
    mapView?.isMyLocationEnabled = true
    locationManager.startUpdatingLocation()
    locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
    locationManager.delegate = self


    // Do any additional setup after loading the view, typically from a nib.
}

override func viewDidAppear(_ animated: Bool) {
    if(locationManager.location != nil){
        centerMapOnLocation(location: locationManager.location!)
    }
}

func centerMapOnLocation(location: CLLocation)
{
    let camera = GMSCameraPosition.camera(withLatitude: locationManager.location!.coordinate.latitude, longitude: locationManager.location!.coordinate.longitude, zoom: zoom)
        mapView?.animate(to: camera)
}
Run Code Online (Sandbox Code Playgroud)

jam*_*z77 5

我已经找到了一个修复方法,所以我把它放在这里以防其他人遇到这个问题。

我从使用mapView?.animate改为GMSCameraPosition.camera,它似乎工作正常。

func centerMapOnLocation(location: CLLocation)
{
    let target = CLLocationCoordinate2D(latitude: locationManager.location!.coordinate.latitude, longitude: locationManager.location!.coordinate.longitude)
    mapView.camera = GMSCameraPosition.camera(withTarget: target, zoom: zoom)
}
Run Code Online (Sandbox Code Playgroud)