title: How to detect taps on MKPolylines/Overlays like Maps.app?
body: <p>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 replicate this functionality and check if a tap lies within a given MKPolyline.</p>
  
  <p>Currently I detect taps on the MapView like this:</p>
  
  <pre><code>// Add Gesture Recognizer to MapView to detect taps
  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleMapTap:)];
  
  // we require all gesture recognizer except other single-tap gesture recognizers to fail
  for (UIGestureRecognizer *gesture in self.gestureRecognizers) {
      if ([gesture isKindOfClass:[UITapGestureRecognizer class]]) {
          UITapGestureRecognizer *systemTap = (UITapGestureRecognizer *)gesture;
  
          if (systemTap.numberOfTapsRequired &gt; 1) {
              [tap requireGestureRecognizerToFail:systemTap];
          }
      } else {
          [tap requireGestureRecognizerToFail:gesture];
      }
  }
  
  [self addGestureRecognizer:tap];
  </code></pre>
  
  <p>I handle the taps as follows:</p>
  
  <pre><code>- (void)handleMapTap:(UITapGestureRecognizer *)tap {
      if ((tap.state &amp; UIGestureRecognizerStateRecognized) == UIGestureRecognizerStateRecognized) {
          // Check if the overlay got tapped
          if (overlayView != nil) {
              // Get view frame rect in the mapView's coordinate system
              CGRect viewFrameInMapView = [overlayView.superview convertRect:overlayView.frame toView:self];
              // Get touch point in the mapView's coordinate system
              CGPoint point = [tap locationInView:self];
  
              // Check if the touch is within the view bounds
              if (CGRectContainsPoint(viewFrameInMapView, point)) {
                  [overlayView handleTapAtPoint:[tap locationInView:self.directionsOverlayView]];
              }
          }
      }
  }
  </code></pre>
  
  <p>This works as expected, now I need to check if the tap lies within the given MKPolyline overlayView (not strict, I the user taps somewhere near the polyline this should be handled as a hit).</p>
  
  <p>What's a good way to do this?</p>
  
  <pre><code>- (void)handleTapAtPoint:(CGPoint)point {
      MKPolyline *polyline = self.polyline;
  
      // TODO: detect if point lies withing polyline with some margin
  }
  </code></pre>
  
  <p>Thanks!</p>