ios - 如何检测 singleTap 是否被点击或在 mapbox 的 mapView 上进行注释?

标签 ios objective-c mapbox mapbox-gl

iOS 上对 mapBox map 的要求。(我不是在说 MKMapView) 我们如何检测 singleTap 是否被点击或 mapView 上的注释?我需要 singleTap 仅在 map 的空白区域(没有图钉)上处理,并且在我点击图钉时调用 didSelectAnnotation。

但是我发现在android上我们有这样的方法

mapboxMap.setOnMapClickListener(new MapboxMap.OnMapClickListener() {
            public void onMapClick(@NonNull LatLng point) {
                Toast.makeText(getActivity(),"on Tap "+point.getLatitude(),Toast.LENGTH_LONG).show();
            }
        });

还有那个

mapboxMap.setInfoWindowAdapter(new MapboxMap.InfoWindowAdapter() { ... }) 将显示注释。

难道我们在 iOS 中没有同样的概念吗?

iOS 中的实际问题是,当我在 Mapbox 的 mapView 上添加 singleTapGesture

UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[self.mapView addGestureRecognizer:singleTapGesture];

不会调用 ma​​pbox 的 mapView 的委托(delegate)方法。

- (nullable UIView <MGLCalloutView> *)mapView:(MGLMapView *)mapView calloutViewForAnnotation:(id <MGLAnnotation>)annotation;

为了确保委托(delegate)方法必须调用,那么我就不必使用singleTapGesture

这里的情况不是这样就是那样,但根据我的需要,我需要两者。

期待任何解决方案。 谢谢,

最佳答案

子类 MGLMapView 并委托(delegate)其“touchesEnded

protocol MapViewTapDelegate: class {

    func mapViewTapped(withTouchLocation touchLocation: CLLocationCoordinate2D)
}

class MapView: MGLMapView {

    weak var tapDelegate: MapViewTapDelegate?

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)

        guard let touch = touches.first else { return }

        let touchLocation = convert(touch.location(in: self), toCoordinateFrom: nil)

        tapDelegate?.mapViewTapped(withTouchLocation: touchLocation)
    }

}

因为 touchesEnded 在调用 didSelect 注释 时不会被触发,反之亦然,这就是您所需要的。

class ViewController: UIViewController {
    @IBOutlet weak var mapView: MapView! {

        didSet {

            mapView.delegate = self; mapView.tapDelegate = self
        }
    }

}

extension ViewController: MGLMapViewDelegate {

    func mapView(_ mapView: MGLMapView, didSelect annotation: MGLAnnotation) {

        print("didSelect annotationWithCoordinate: \(annotation.coordinate)")
    }

}

extension ViewController: MapViewTapDelegate {

    func mapViewTapped(withTouchLocation touchLocation: CLLocationCoordinate2D) {

        print("mapViewTapped touchLocation: \(touchLocation)")
    }

}

关于ios - 如何检测 singleTap 是否被点击或在 mapbox 的 mapView 上进行注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38069458/

相关文章:

ios - 单个 UIView 中的 2 个不同的 UITableView

ios - 选项卡栏高度和按钮大小 - 已修复?

ios - 你如何以编程方式制作这个 plist 文件?

objective-c - Swift 5 中的 NSData.bytes[下标]

android - 如何添加 Mapbox 遥测 2.0.0-SNAPSHOT 作为依赖项?

ios - 在 tableView Cell 中看不到按钮的图像

objective-c - 如何简化包含 NSUInteger 的 ocunit 断言?

javascript - 添加额外的 onClick() 到导航控件

iOS MapBox map 滞后

ios - 向 UILabel 添加左/右水平填充