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];
不会调用 mapbox 的 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/