当在iPhone上的内置Maps.app上显示方向时,您可以通过点击它来“选择”通常显示的3种路线替代方案之一。我不想复制这个功能,并检查一个水龙头是否在给定的MKPolyline中。如何检测MKPolylines /覆盖像Maps.app上的水龙头?
目前,我发现水龙头上的MapView这样的:
// 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 > 1) {
[tap requireGestureRecognizerToFail:systemTap];
}
} else {
[tap requireGestureRecognizerToFail:gesture];
}
}
[self addGestureRecognizer:tap];
我把手水龙头如下:
- (void)handleMapTap:(UITapGestureRecognizer *)tap {
if ((tap.state & 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]];
}
}
}
}
可正常工作,现在我需要检查,如果水龙头在于内给定的MKPolyline overlayView(不严格,我用户点击折线附近的某个地方,这应该作为一个命中来处理)。
这样做的好方法是什么?
- (void)handleTapAtPoint:(CGPoint)point {
MKPolyline *polyline = self.polyline;
// TODO: detect if point lies withing polyline with some margin
}
谢谢!
伟大的解决方案,工作正常:),谢谢 – polo987 2014-08-22 13:12:56
这是一个很好的解决方案。一个问题,这里究竟计算了什么? double u =((pt.x - ptA.x)* xDelta +(pt.y - ptA.y)* yDelta)/(xDelta * xDelta + yDelta * yDelta); ...我有种从那里迷路,你能否添加一些评论来解释从哪里和下面计算出来的? – Bocaxica 2017-01-08 10:49:07
@Bocaxica那部分不是我的代码。请参考http://paulbourke.net/geometry/pointlineplane/ – Jensemann 2017-01-08 12:37:40