How to detect taps on MKPolylines/Overlays like Maps.app?

后端 未结 8 882
梦谈多话
梦谈多话 2020-12-23 02:09

When displaying directions on the built-in Maps.app on the iPhone you can \"select\" one of the usually 3 route alternatives that are displayed by tapping on it. I wan\'t to

8条回答
  •  庸人自扰
    2020-12-23 02:50

    You can refer my answer may it will help you to find desired solution.

    I've added gesture on my MKMapView.

    [mapV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mapTapped:)]];
    

    This is how i handled my gesture and find out whether the tap is on Overlay view or not.

       - (void)mapTapped:(UITapGestureRecognizer *)recognizer
        {
    
             MKMapView *mapView = (MKMapView *)recognizer.view;
    
             CGPoint tapPoint = [recognizer locationInView:mapView];
             NSLog(@"tapPoint = %f,%f",tapPoint.x, tapPoint.y);
    
             //convert screen CGPoint tapPoint to CLLocationCoordinate2D...
             CLLocationCoordinate2D tapCoordinate = [mapView convertPoint:tapPoint toCoordinateFromView:mapView];
    
             //convert CLLocationCoordinate2D tapCoordinate to MKMapPoint...
             MKMapPoint point = MKMapPointForCoordinate(tapCoordinate);
    
             if (mapView.overlays.count > 0 ) {
                  for (id overlay in mapView.overlays)
                  {
    
                       if ([overlay isKindOfClass:[MKCircle class]])
                       {
                            MKCircle *circle = overlay;
                            MKCircleRenderer *circleRenderer = (MKCircleRenderer *)[mapView rendererForOverlay:circle];
    
                            //convert MKMapPoint tapMapPoint to point in renderer's context...
                            CGPoint datpoint = [circleRenderer pointForMapPoint:point];
                            [circleRenderer invalidatePath];
    
    
                            if (CGPathContainsPoint(circleRenderer.path, nil, datpoint, false)){
    
                                 NSLog(@"tapped on overlay");
                                 break;
                       }
    
                  }
    
             }
    
           }
        }
    

    Thanks. This may help you hopefully.

提交回复
热议问题