5

我在通过手势识别器在 GMSMapView 上捕获拖动/平移手势时遇到了一个奇怪的问题。此问题仅在从 GMS 1.2 更新到 1.3.1 后才出现,其中(引用文档),

GMSMapView 更积极地消耗触摸

我有一个 UIViewController 在其主视图下持有一个 GMSMapView 。我发现 GMSMapDelegate 不提供处理拖动/平移手势的方法,所以我向 UIViewController 添加了一个 UIPanGestureRecognizer,将其链接到 IBAction 选择器,并设置引用插座和插座集合,根据此处链接的屏幕截图:http://i .stack.imgur.com/gktoa.png

所以任何拖动动作都会简单地触发recognizeDragOnMap:选择器,如下所示:

-(IBAction)recognizeDragOnMap:(id)sender {
    NSLog(@"recognizeDragOnMap");

    UIGestureRecognizer *gestureRecognizer = (UIGestureRecognizer *)sender;
    if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
        NSLog(@"Still dragging");
        return;
    }
    NSLog(@"DragEnded");

    GMSCameraPosition *position;

    if ((position = self.mapView.camera)) {
        self.automaticCameraPositionChange = NO;
        CLLocationCoordinate2D coordinate = [position targetAsCoordinate];
        CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
        [self.origin dragPinToLocation:location];
    } else {
        NSLog(@"No map camera");
    }
}

此设置曾经在 GMS 1.2.0 下完美运行。更新后,GMSMapView 会像以前一样响应手势,但是上面的方法永远不会被调用!

任何人都知道发生了什么和/或如何解决它?

4

6 回答 6

12

对于 1.4 或更高版本,您只需consumesGesturesInView = NO在 GMSUISettings 对象中进行设置。

如果您这样做,请注意,当您只想与地图交互时,您将不得不处理可能使您的超级视图执行某些操作的事件......我的意思是,例如,将 GMSMapView 添加到滚动视图将在拖动时滚动滚动视图!

于 2013-11-07T13:37:22.577 回答
10
mapView.settings.consumesGesturesInView = YES;

也很有帮助。我的父母视图正在消耗我的手势识别器

再加上

for (UIGestureRecognizer *gestureRecognizer in mapView.gestureRecognizers) {
    [gestureRecognizer addTarget:self action:@selector(handlePan:)];
}


//////



-(IBAction) handlePan:(UIPanGestureRecognizer*)sender {



    if (sender.state == UIGestureRecognizerStateEnded) {
        CGSize size = mapView.frame.size;
        CGPoint tp = CGPointMake(size.width/2, size.height/2);
        CLLocationCoordinate2D loc = [mapView.projection coordinateForPoint:tp];
        [mapView animateToCameraPosition:[GMSCameraPosition cameraWithTarget:loc zoom:mapView.camera.zoom]];



    }


}

太好了!你甚至不需要添加自己的平移手势识别器

于 2014-02-25T06:40:12.887 回答
3

事实证明,一个GMSMapView实例现在拥有一个GMSBlockingGestureRecognizer吞噬所有手势的 a。所以有两个选择:

  • 加载后删除这个识别GMSMapView器(可能会破坏依赖它的内部功能)(像这样);或者
  • 将我自己的目标/动作附加到识别器。

UIViewController使用第二种方法, 's中的以下代码viewDidLoad使事情恢复正常:

self.mapView = (RAMapView *)[self.view viewWithTag:1];

for (UIGestureRecognizer *gestureRecognizer in self.mapView.gestureRecognizers) {
    [gestureRecognizer addTarget:self action:@selector(recognizeDragOnMap:)];
}

老实说,这是一个丑陋、邪恶的组合,但它确实有效。:)

于 2013-07-02T21:15:25.580 回答
0

A better alternative would be to set the delegate of your own gesture recogniser and then add the delegate method as follows :

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

It will then work as before.

于 2013-07-22T11:13:49.493 回答
0

通过google api和其他方法的分析,我没有得到正确的。这个问题的最佳答案是使用平移手势。

将平移手势添加到 mapView 为

self.mapView.settings.consumesGesturesInView = false
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self. panHandler(_:)))
self.mapView.addGestureRecognizer(panGesture)

平移手势方法的实现为

@objc private func panHandler(_ pan : UIPanGestureRecognizer){

        if pan.state == .ended{
            let mapSize = self.mapView.frame.size
            let point = CGPoint(x: mapSize.width/2, y: mapSize.height/2)
            let newCoordinate = self.mapView.projection.coordinate(for: point)
            print(newCoordinate)
             //do task here
        }
}
于 2020-04-06T10:30:03.147 回答
0

嘿,谷歌地图中有一个委托方法。

- (void)mapView:(GMSMapView *)mapView    didTapAtCoordinate:(CLLocationCoordinate2D)coordinate;
于 2015-07-13T12:16:14.670 回答