如何set/update缩放到Swift中的GMSMapView?

How to set/update zoom level to GMSMapView in Swift?

以下代码是我如何获得新标记位置和更新地图视图的。

if self.state.dropOff != nil {   
                let loc = response
                let position = CLLocationCoordinate2D(latitude: loc.latitude!, longitude: loc.longitude!)
                self.getPolylineRoute(from: self.state.pickUp!.coordinate, to: self.state.dropOff!.coordinate)
                CATransaction.begin()
                CATransaction.setAnimationDuration(1.0)

                if self.acceptedCabMarker == nil {
                    self.acceptedCabMarker = GMSMarker(position: position)
                }

                self.acceptedCabMarker!.position = position
                self.acceptedCabMarker!.isFlat = true
                self.acceptedCabMarker!.icon = UIImage(named: markerIcon)
                self.acceptedCabMarker!.setIconSize(scaledToSize: .init(width: 40, height: 40))
                self.acceptedCabMarker!.appearAnimation = .pop
                self.acceptedCabMarker!.rotation = CLLocationDegrees(loc.bearing ?? 0)


                CATransaction.commit()
                DispatchQueue.main.async {
                    self.acceptedCabMarker!.map = self.mapView
                }
            }

问题是每次执行此代码时,地图视图缩放级别都会变为其原始状态。这意味着用户不能长时间缩放地图。 我尝试使用方法保存缩放。

extension SomeHomeViewController: GMSMapViewDelegate {

func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
    print("Camera Zoom: \(position.zoom)")
    currentPosition = position
}
}

但我不能重复使用 currentPosition 因为

self.mapView?.camera.zoom = currentPosition?.zoom

不允许。

您可以通过这种方式简单地设置 GMSMapView 的缩放

let camera = GMSCameraPosition.camera(withLatitude: loc.latitude, longitude: loc.longitude, zoom: 10.0)
self.mapView.camera = camera

您需要使用

- (void)animateToZoom:(float)zoom;

在 GMSMapView(动画)类别中定义的方法。有关更多信息,您可以参考此 link.