【问题标题】:Google Maps iOS SDK Touch Events谷歌地图 iOS SDK 触摸事件
【发布时间】:2015-03-14 10:27:48
【问题描述】:

我正在尝试将 UIGestureRecognizer 添加到整个谷歌地图视图中。

如果我触摸地图(不是标记),我想得到通知,但我不知道如何。我所做的是在 viewDidLoad 里面:

UITapGestureRecognizer* tapRec = [[UITapGestureRecognizer alloc]
                                  initWithTarget:self action:@selector(didTapMap:)];
[mapView_ addGestureRecognizer:tapRec];

和外面的viewDidLoad:

- (void)didTapMap:(UITapGestureRecognizer *)recognizer {
    NSLog(@"Touched map");
}

但是这种方法不起作用,并且不会在控制台窗口上打印任何内容..

请帮帮我,告诉我怎么做

【问题讨论】:

    标签: gmsmapview


    【解决方案1】:

    我认为您需要的已经是地图委托的一部分

     /**
     * Called after a tap gesture at a particular coordinate, but only if a marker
     * was not tapped.  This is called before deselecting any currently selected
     * marker (the implicit action for tapping on the map).
     */
    - (void)mapView:(GMSMapView *)mapView
        didTapAtCoordinate:(CLLocationCoordinate2D)coordinate;
    

    该委托中还有其他方法,请查看它们。

    为 Swift 使用以下函数:

    func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) 
    

    【讨论】:

    • 有人能把它转换成 Swift 3 吗?最新版的 google maps sdk 和 Swift 还是这个样子吗?
    【解决方案2】:
    func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
        // In My Case Im adding one overlay view on marker tap.
        // infoWindow is a subclass of UIview in my code
    
        infoWindow.removeFromSuperview()
        infoWindow = loadNiB()
        infoWindow.center = mapView.projection.point(for: marker.position)
        infoWindow.center.y = infoWindow.center.y - sizeForOffset(view: infoWindow)
        self.view.addSubview(infoWindow)
    
        return false
    }
    

    【讨论】:

    • 虽然这个纯代码的答案可能会回答这个问题,但请添加解释为什么这样做。这将帮助未来的用户根据他们的情况评估答案。
    【解决方案3】:

    对于 Swift 3

    func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
        NSLog(@"Touched map");
    }
    

    【讨论】:

      【解决方案4】:
      - (void) mapView:(GMSMapView *)mapView didTapAtCoordinate (CLLocationCoordinate2D)coordinate{
      NSLog(@"User did tap at coordinate %f, %f", coordinate.latitude, coordinate.longitude) ;
      NSLog(@"Map view center %f %f and zoom is %f",  mapView.camera.target.latitude, mapView.camera.target.longitude, mapView.camera.zoom) ;
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-21
        • 1970-01-01
        • 2019-06-14
        • 1970-01-01
        相关资源
        最近更新 更多