當在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