在这个Google地图服务组件中,有没有一种方法可以检测缩放(捏和双击)?
无论进行何种移动,上述方法都会触发。
- (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture
无论进行何种移动,上述方法都会触发。
- (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture
键值观察提供了一种机制,允许对象被通知其他对象特定属性的更改。
在设置地图视图的任何位置添加此片段:
[self.mapView addObserver:self forKeyPath:@"camera.zoom" options:0 context:nil];
现在你只需要实现-observeValueForKeyPath:ofObject:change:context:
方法来接收回调。像这样:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"camera.zoom"]) {
// this static variable will hold the last value between invocations.
static CGFloat lastZoom = 0;
GMSMapView *mapView = (GMSMapView *)object;
CGFloat currentZoom = [[mapView camera] zoom];
if (!(fabs((lastZoom) - (currentZoom)) < FLT_EPSILON)) {
//Zoom level has actually changed!
NSLog(@"Zoom changed to: %.2f", [[mapView camera] zoom]);
}
//update last zoom level value.
lastZoom = currentZoom;
}
}
根据您的需求,在-dealloc
或-viewDidDissapear
中不要忘记移除观察者:
- (void)dealloc {
[self.mapView removeObserver:self forKeyPath:@"camera.zoom"];
}
愉快的编码 :-)
Swift 3
下面的代码对我有效:
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
print("Pinched or tapped on the map")
}
我希望您已经在头文件中使用了GMSMapViewDelegate
在实现文件中使用以下代码,该代码是GMSMapView
对象的代理:
-(void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition*)position {
float zoom = mapView.camera.zoom;
// handle you zoom related logic
}
一些旧的但仍然有用的方法...您可以通过以下方式检测:首先告诉mapView在视图中消耗手势:
mapView.settings.consumesGesturesInView = true
for gestureRecognizer in mapView.gestureRecognizers! {
gestureRecognizer.addTarget(self, action: "handleMapGesture:")
}
.first
就足够了。 - mylogon