Khi hiển thị chỉ đường trên Maps.app tích hợp trên iPhone, bạn có thể "chọn" một trong 3 lựa chọn thay thế tuyến đường thường được hiển thị bằng cách nhấn vào nó. Tôi không thể sao chép chức năng này và kiểm tra xem một vòi nước nằm trong một MKPolyline nhất định.Làm thế nào để phát hiện vòi trên MKPolylines/Lớp phủ như Maps.app?
Hiện nay tôi phát hiện vòi trên MapView như thế này:
// 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];
tôi xử lý các vòi như sau:
- (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]];
}
}
}
}
này làm việc như mong đợi, bây giờ tôi cần phải kiểm tra xem vòi nước nằm trong lớp phủ MKPolyline đã cho (không nghiêm ngặt, tôi người dùng chạm vào một nơi nào đó gần polyline này sẽ được xử lý như một lần truy cập).
Cách hay để làm điều này là gì?
- (void)handleTapAtPoint:(CGPoint)point {
MKPolyline *polyline = self.polyline;
// TODO: detect if point lies withing polyline with some margin
}
Cảm ơn!
giải pháp tuyệt vời, làm việc tốt :), cảm ơn – polo987
Đây là một giải pháp tốt. Một câu hỏi, chính xác những gì đang được tính toán ở đây? đôi u = ((pt.x - ptA.x) * xDelta + (pt.y - ptA.y) * yDelta)/(xDelta * xDelta + yDelta * yDelta); ...Tôi bị mất từ đó, bạn có thể thêm một số ý kiến để giải thích những gì đang được tính toán từ đó và dưới đây? – Bocaxica
@ Bocaxica phần đó không phải là mã của tôi. Xin vui lòng tham khảo http://paulbourke.net/geometry/pointlineplane/ – Jensemann