为什么未将GMSMapView设置为适当的坐标?

时间:2019-03-04 17:04:08

标签: ios swift google-maps

这可能是一个经常被问到的问题,但是不管您相信与否,我没有找到可以帮助我解决问题的答案。

尽管如此,我还是尝试了多种解决方案,但我不明白为什么有时GMSMapView对象的摄影机无法在正确的位置初始化。

我已从界面生成器中将UIView场景中的UIViewController拖动到了GMSMapView对象上。

这是我的setCamera方法:

private func setCamera(lat: Double, long: Double, zoom: Float) {

    let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: long, zoom: zoom)
    mapView.camera = camera
    mapView.setMinZoom(5, maxZoom: 15)
}

我试图用viewDidLoadviewWillAppear来称呼它,这是相同的行为。

大约有5次发射中的1次,相机未居中在正确的坐标上,感觉GMSMapView是用随机相机初始化的。

我该如何解决?

1 个答案:

答案 0 :(得分:2)

这是我对GMSMapView的初始化:

let camera = GMSCameraPosition.camera(withTarget: myLocation.coordinate, 
                                      zoom: mapDefaultZoom)
let mapView = GMSMapView.map(withFrame: view.bounds, 
                             camera: camera)
view.addSubview(mapView)

它是相机位置的更新:

let bounds = GMSCoordinateBounds().includingCoordinate(position)
let update = GMSCameraUpdate.fit(bounds, 
                                 withPadding: 60)
mapView.animate(with: update)

希望,它将为您提供帮助。