视图左上角的Swift GMSMarker图标

时间:2019-02-05 19:30:09

标签: swift google-maps cllocationmanager

标题很容易说明。我曾尝试按照建议的hereviewDidLayoutSubviews()中调用它,但它并没有给我带来欢乐。

这是我的代码:

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)
}

1 个答案:

答案 0 :(得分:0)

我已经找到了解决方法,因此将其放在此处,以防其他人遇到此问题。

我从使用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)
}