检测GMSMapView缩放

时间:2014-03-29 18:10:08

标签: ios iphone objective-c google-maps gmsmapview

有没有办法在此Google地图服务组件中检测缩放(捏合和双击)?

- (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture

无论动作如何,上面的方法都会消防。

4 个答案:

答案 0 :(得分:7)

还有另一种检测缩放(或任何其他属性)的方法 - 键值观察(又名KVO)。当没有为我们提供的委托方法时,它特别有用。来自Apple docs

  

键值观察提供了一种允许对象的机制   通知其他对象的特定属性的更改。

无论您在何处设置地图视图,请添加以下代码段:

[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"];

}

快乐编码: - )

答案 1 :(得分:4)

我希望你在头文件中使用GMSMapViewDelegate

在实现文件中使用以下代码,该文件是GMSMapView object

的委托
-(void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition*)position {
   float zoom = mapView.camera.zoom;
   // handle you zoom related logic
}

答案 2 :(得分:2)

Swift 3
以下代码为我工作:
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) { print("Pinched or tapped on the map") } 当用户在屏幕上进行缩放(双击或双击)时,此方法将被调用一次。

答案 3 :(得分:0)

有些旧,但仍然......你可以通过这种方式检测,首先说mapView在视图中使用手势:

    mapView.settings.consumesGesturesInView = true

    for gestureRecognizer in mapView.gestureRecognizers! {
        gestureRecognizer.addTarget(self, action: "handleMapGesture:")
    }

其次,在您的功能上,检查两件事,状态和触摸次数。

如果状态为.Changed,则手势开始,2次触摸是缩放捏合。

困难的是双击,你必须实现某种迟到的倾听者并链接最后两个手势,以确定一个&#34; Tap&#34;只有.Begin.End只需轻轻一按,就没有.Changed这种姿态的状态。

注意:这适用于Swift 2,未在3或4上进行测试