Search code examples
iosobjective-cmapboxmapbox-gl

how can I detect if singleTap was tapped or annotation on mapView of mapbox?


Requirment on iOS for mapBox map. (I am not talking about MKMapView) how can we detect if singleTap was tapped or annotation on mapView? I need that singleTap will be handled only on empty area of map (without pins), and didSelectAnnotation called when i tap on pin.

But I found on android we have method like this

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

and along with that

mapboxMap.setInfoWindowAdapter(new MapboxMap.InfoWindowAdapter() { ... }) will display the annotation.

Don't we have same kind of concept in iOS ?

The actual problem is in iOS is, when I add singleTapGesture on mapView of Mapbox

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

the delegate method of mapView of mapbox will not call.

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

to make sure the delegate method must call, then I dont have to use singleTapGesture

Here the situation is either this or that, but as per me need I needed both.

Looking forward of any solution. Thanks,


Solution

  • I made another workaround here

    I cloned the release-ios-v3.3.0 and created package using Building the SDK and added one delegate method in MGLMapvViewDelegate.h as per my need something like that. -(void)mapView:(MGLMapView *)mapView tapOnNonAnnotationAreaWithPoints:(CGPoint)points

    and in MGLMapView.mm I updated the code like that,

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

    which is in -(void)handleSingleTapGesture:(UITapGestureRecognizer *)singleTap method.

    Its working for me, as I am able to detect single tap on non annotation area. later I convert the passed points to geo coordinates, so that I work with newly tapped coordinate.

    Note

    1. Newly Created library size is about 48.7 MB where as downloaded library was 38.3 MB.
    2. Style is not working properly, you can see in attached pic.enter image description hereenter image description here (Tried with different Style URL but none of it is same as previous)
    3. I feel lack in App performance, Zooming and Panning is not smooth as the dynamic library gave by Mapbox guys.

    I am still exploring. lemme know if you wanna me to check/explore.

    Please accept this answer, if it is helpful.

    Thank you,

    Happy Coding.