标题很容易说明。我曾尝试按照建议的here在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)
}
答案 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)
}