如何在mapbox的mapView上检测是否点击了singleTap?

Nas*_*sir 7 objective-c ios mapbox mapbox-gl

iOS上的mapBox地图需求.(我不是在谈论MKMapView)我们如何检测是否在mapView上点击了singleTap或注释?我需要singleTap仅在地图的空白区域(没有引脚)处理,并且当我点击引脚时调用didSelectAnnotation.

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

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

以及那个

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

我们在iOS中没有相同的概念吗?

实际的问题是,在iOS的是,当我添加singleTapGestureMapbox的MapView的

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

mapboxmapView的委托方法不会调用.

- (nullable UIView <MGLCalloutView> *)mapView:(MGLMapView *)mapView calloutViewForAnnotation:(id <MGLAnnotation>)annotation;
Run Code Online (Sandbox Code Playgroud)

要确保委托方法必须调用,那么我不必使用 singleTapGesture

这里的情况要么是这个,要么就是这样,但根据我的需要,我需要两者.

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

IT *_*psy 7

子类 MGLMapView 并委托其' 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)
    }

}
Run Code Online (Sandbox Code Playgroud)

由于touchesEndeddidSelect annotation调用时不会触发,反之亦然,这就是您所需要的。

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)")
    }

}
Run Code Online (Sandbox Code Playgroud)


Nas*_*sir 2

我在这里做了另一个解决方法

我克隆了release-ios-v3.3.0并使用Building the SDK创建了包,并MGLMapvViewDelegate.h 根据我的需要 添加了一个委托方法 。-(void)mapView:(MGLMapView *)mapView tapOnNonAnnotationAreaWithPoints:(CGPoint)points

在 MGLMapView.mm 中我更新了这样的代码,

else{
if(self.selectedAnnotation)
      [self deselectAnnotation:self.selectedAnnotation animated:YES];
else if([self.delegate respondsToSelector:@selector(mapView:tapOnNonAnnotationAreaWithPoints:)])
      [self.delegate mapView:self tapOnNonAnnotationAreaWithPoints:tapPoint];
}
Run Code Online (Sandbox Code Playgroud)

这是在-(void)handleSingleTapGesture:(UITapGestureRecognizer *)singleTap 方法中。

它对我有用,因为我能够检测到非注释区域上的单击。后来我将传递的坐标转换points为地理坐标,以便我使用新点击的坐标。

笔记

  1. 48.7 MB新创建的库大小与下载库的大小差不多38.3 MB
  2. 样式无法正常工作,您可以在附图中看到。在此输入图像描述在此输入图像描述(尝试使用不同的样式 URL,但没有一个与之前的相同)
  3. 我觉得应用程序性能有所欠缺,缩放和平移不如Mapbox提供的动态库流畅。

我还在探索中。让我知道你是否想让我检查/探索。

如果有帮助,请采纳此答案。

谢谢你,

快乐编码。