我使用具有数字值的方法相机,如下所示:
camera = [GMSCameraPosition cameraWithLatitude:37.35 longitude:-122.0 zoom:6];
我需要通过计时器自动重绘当前位置的地图:
-(void)gmapRedraw{
NSLog(@"gmapRedraw");
// self.MapView.camera
[locationManager startUpdatingLocation];
NSLog(@"lat %f, lon %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude);
camera = [GMSCameraPosition cameraWithLatitude:locationManager.location.coordinate.latitude//37.36//-37.81319//-33.86
longitude:locationManager.location.coordinate.longitude//-122.0//144.96298//151.20
zoom:6];
self.MapView.camera = camera;
}
-(void)timer{
[NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(gmapRedraw)
userInfo:nil
repeats:YES];
}
但如果我通过触摸改变它,我怎么能得到当前的变焦?
好的,我找到解决方案,获得当前缩放:
CGFloat currentZoom = self.MapView.camera.zoom;
然后在相机中使用它:
camera = [GMSCameraPosition cameraWithLatitude:locationManager.location.coordinate.latitude
longitude:locationManager.location.coordinate.longitude
zoom:currentZoom];
self.MapView.camera = camera;
如果要在用户与地图交互时检测缩放级别。我们可以使用Google Map委托方法
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
let zoom = mapView.camera.zoom
print("map zoom is ",String(zoom))
}
别忘了添加GMSMapViewDelegate
你也可以使用animateToZoom。
self.mapView.animateToZoom(10)
swift 4.x的两分钱:
let currZoom = self.googleMapView.camera.zoom
但是在第一个objC样本中是浮点数,而不是CGFloat。在Swift中,Float不是CGFloat。