29

在 iPhone 上的内置 Maps.app 上显示方向时,您可以通过点击它来“选择”通常显示的 3 条路线选择之一。我不想复制此功能并检查水龙头是否位于给定的 MKPolyline 内。

目前我像这样检测 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 覆盖视图内(不严格,我用户点击折线附近的某处,这应该被视为命中)。

有什么好方法可以做到这一点?

- (void)handleTapAtPoint:(CGPoint)point {
    MKPolyline *polyline = self.polyline;

    // TODO: detect if point lies withing polyline with some margin
}

谢谢!

4

8 回答 8

48

这个问题相当古老,但我的回答可能对寻求解决这个问题的其他人有用。

此代码在每个缩放级别检测最大距离为 22 像素的折线上的触摸。只需将您UITapGestureRecognizer指向handleTap

/** Returns the distance of |pt| to |poly| in meters
 *
 * from http://paulbourke.net/geometry/pointlineplane/DistancePoint.java
 *
 */
- (double)distanceOfPoint:(MKMapPoint)pt toPoly:(MKPolyline *)poly
{
    double distance = MAXFLOAT;
    for (int n = 0; n < poly.pointCount - 1; n++) {

        MKMapPoint ptA = poly.points[n];
        MKMapPoint ptB = poly.points[n + 1];

        double xDelta = ptB.x - ptA.x;
        double yDelta = ptB.y - ptA.y;

        if (xDelta == 0.0 && yDelta == 0.0) {

            // Points must not be equal
            continue;
        }

        double u = ((pt.x - ptA.x) * xDelta + (pt.y - ptA.y) * yDelta) / (xDelta * xDelta + yDelta * yDelta);
        MKMapPoint ptClosest;
        if (u < 0.0) {

            ptClosest = ptA;
        }
        else if (u > 1.0) {

            ptClosest = ptB;
        }
        else {

            ptClosest = MKMapPointMake(ptA.x + u * xDelta, ptA.y + u * yDelta);
        }

        distance = MIN(distance, MKMetersBetweenMapPoints(ptClosest, pt));
    }

    return distance;
}


/** Converts |px| to meters at location |pt| */
- (double)metersFromPixel:(NSUInteger)px atPoint:(CGPoint)pt
{
    CGPoint ptB = CGPointMake(pt.x + px, pt.y);

    CLLocationCoordinate2D coordA = [mapView convertPoint:pt toCoordinateFromView:mapView];
    CLLocationCoordinate2D coordB = [mapView convertPoint:ptB toCoordinateFromView:mapView];

    return MKMetersBetweenMapPoints(MKMapPointForCoordinate(coordA), MKMapPointForCoordinate(coordB));
}


#define MAX_DISTANCE_PX 22.0f
- (void)handleTap:(UITapGestureRecognizer *)tap
{
    if ((tap.state & UIGestureRecognizerStateRecognized) == UIGestureRecognizerStateRecognized) {

        // Get map coordinate from touch point
        CGPoint touchPt = [tap locationInView:mapView];
        CLLocationCoordinate2D coord = [mapView convertPoint:touchPt toCoordinateFromView:mapView];

        double maxMeters = [self metersFromPixel:MAX_DISTANCE_PX atPoint:touchPt];

        float nearestDistance = MAXFLOAT;
        MKPolyline *nearestPoly = nil;

        // for every overlay ...
        for (id <MKOverlay> overlay in mapView.overlays) {

            // .. if MKPolyline ...
            if ([overlay isKindOfClass:[MKPolyline class]]) {

                // ... get the distance ...
                float distance = [self distanceOfPoint:MKMapPointForCoordinate(coord)
                                                toPoly:overlay];

                // ... and find the nearest one
                if (distance < nearestDistance) {

                    nearestDistance = distance;
                    nearestPoly = overlay;
                }
            }
        }

        if (nearestDistance <= maxMeters) {

            NSLog(@"Touched poly: %@\n"
                   "    distance: %f", nearestPoly, nearestDistance);
        }
    }
}
于 2013-12-06T13:48:43.090 回答
24

@Jensemanns 在 Swift 4 中的回答,顺便说一句,这是我发现的唯一一个对我有用的解决方案来检测 a 上的点击MKPolyline

let map = MKMapView()
let mapTap = UITapGestureRecognizer(target: self, action: #selector(mapTapped(_:)))
map.addGestureRecognizer(mapTap)

func mapTapped(_ tap: UITapGestureRecognizer) {
    if tap.state == .recognized {
        // Get map coordinate from touch point
        let touchPt: CGPoint = tap.location(in: map)
        let coord: CLLocationCoordinate2D = map.convert(touchPt, toCoordinateFrom: map)
        let maxMeters: Double = meters(fromPixel: 22, at: touchPt)
        var nearestDistance: Float = MAXFLOAT
        var nearestPoly: MKPolyline? = nil
        // for every overlay ...
        for overlay: MKOverlay in map.overlays {
            // .. if MKPolyline ...
            if (overlay is MKPolyline) {
                // ... get the distance ...
                let distance: Float = Float(distanceOf(pt: MKMapPointForCoordinate(coord), toPoly: overlay as! MKPolyline))
                // ... and find the nearest one
                if distance < nearestDistance {
                    nearestDistance = distance
                    nearestPoly = overlay as! MKPolyline
                }

            }
        }

        if Double(nearestDistance) <= maxMeters {
            print("Touched poly: \(nearestPoly) distance: \(nearestDistance)")

        }
    }
}

func distanceOf(pt: MKMapPoint, toPoly poly: MKPolyline) -> Double {
    var distance: Double = Double(MAXFLOAT)
    for n in 0..<poly.pointCount - 1 {
        let ptA = poly.points()[n]
        let ptB = poly.points()[n + 1]
        let xDelta: Double = ptB.x - ptA.x
        let yDelta: Double = ptB.y - ptA.y
        if xDelta == 0.0 && yDelta == 0.0 {
            // Points must not be equal
            continue
        }
        let u: Double = ((pt.x - ptA.x) * xDelta + (pt.y - ptA.y) * yDelta) / (xDelta * xDelta + yDelta * yDelta)
        var ptClosest: MKMapPoint
        if u < 0.0 {
            ptClosest = ptA
        }
        else if u > 1.0 {
            ptClosest = ptB
        }
        else {
            ptClosest = MKMapPointMake(ptA.x + u * xDelta, ptA.y + u * yDelta)
        }

        distance = min(distance, MKMetersBetweenMapPoints(ptClosest, pt))
    }
    return distance
}

func meters(fromPixel px: Int, at pt: CGPoint) -> Double {
    let ptB = CGPoint(x: pt.x + CGFloat(px), y: pt.y)
    let coordA: CLLocationCoordinate2D = map.convert(pt, toCoordinateFrom: map)
    let coordB: CLLocationCoordinate2D = map.convert(ptB, toCoordinateFrom: map)
    return MKMetersBetweenMapPoints(MKMapPointForCoordinate(coordA), MKMapPointForCoordinate(coordB))
}

斯威夫特 5.x 版本

let map = MKMapView()
let mapTap = UITapGestureRecognizer(target: self, action: #selector(mapTapped))
map.addGestureRecognizer(mapTap)

@objc func mapTapped(_ tap: UITapGestureRecognizer) {
    if tap.state == .recognized {
        // Get map coordinate from touch point
        let touchPt: CGPoint = tap.location(in: map)
        let coord: CLLocationCoordinate2D = map.convert(touchPt, toCoordinateFrom: map)
        let maxMeters: Double = meters(fromPixel: 22, at: touchPt)
        var nearestDistance: Float = MAXFLOAT
        var nearestPoly: MKPolyline? = nil
        // for every overlay ...
        for overlay: MKOverlay in map.overlays {
            // .. if MKPolyline ...
            if (overlay is MKPolyline) {
                // ... get the distance ...
                let distance: Float = Float(distanceOf(pt: MKMapPoint(coord), toPoly: overlay as! MKPolyline))
                // ... and find the nearest one
                if distance < nearestDistance {
                    nearestDistance = distance
                    nearestPoly = overlay as? MKPolyline
                }

            }
        }

        if Double(nearestDistance) <= maxMeters {
            print("Touched poly: \(String(describing: nearestPoly)) distance: \(nearestDistance)")

        }
    }
}

private func distanceOf(pt: MKMapPoint, toPoly poly: MKPolyline) -> Double {
    var distance: Double = Double(MAXFLOAT)
    for n in 0..<poly.pointCount - 1 {
        let ptA = poly.points()[n]
        let ptB = poly.points()[n + 1]
        let xDelta: Double = ptB.x - ptA.x
        let yDelta: Double = ptB.y - ptA.y
        if xDelta == 0.0 && yDelta == 0.0 {
            // Points must not be equal
            continue
        }
        let u: Double = ((pt.x - ptA.x) * xDelta + (pt.y - ptA.y) * yDelta) / (xDelta * xDelta + yDelta * yDelta)
        var ptClosest: MKMapPoint
        if u < 0.0 {
            ptClosest = ptA
        }
        else if u > 1.0 {
            ptClosest = ptB
        }
        else {
            ptClosest = MKMapPoint(x: ptA.x + u * xDelta, y: ptA.y + u * yDelta)
        }

        distance = min(distance, ptClosest.distance(to: pt))
    }
    return distance
}

private func meters(fromPixel px: Int, at pt: CGPoint) -> Double {
    let ptB = CGPoint(x: pt.x + CGFloat(px), y: pt.y)
    let coordA: CLLocationCoordinate2D = map.convert(pt, toCoordinateFrom: map)
    let coordB: CLLocationCoordinate2D = map.convert(ptB, toCoordinateFrom: map)
    return MKMapPoint(coordA).distance(to: MKMapPoint(coordB))
}
于 2017-09-22T19:36:52.600 回答
2

为 Swift 3 更新

func isTappedOnPolygon(with tapGesture:UITapGestureRecognizer, on mapView: MKMapView) -> Bool {
    let tappedMapView = tapGesture.view
    let tappedPoint = tapGesture.location(in: tappedMapView)
    let tappedCoordinates = mapView.convert(tappedPoint, toCoordinateFrom: tappedMapView)
    let point:MKMapPoint = MKMapPointForCoordinate(tappedCoordinates)

    let overlays = mapView.overlays.filter { o in
        o is MKPolygon
    }

    for overlay in overlays {
        let polygonRenderer = MKPolygonRenderer(overlay: overlay)
        let datPoint = polygonRenderer.point(for: point)
        polygonRenderer.invalidatePath()

        return polygonRenderer.path.contains(datPoint)
    }
    return false
}
于 2016-12-09T08:12:02.683 回答
2

您可以参考我的回答,它可能会帮助您找到所需的解决方案。

我在我的 MKMapView 上添加了手势。

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

这就是我处理手势并确定点击是否在叠加视图上的方式。

   - (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<MKOverlay> 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;
                   }

              }

         }

       }
    }

谢谢。这可能会对您有所帮助。

于 2016-05-12T11:15:14.473 回答
2

Jensemann 下面提出的解决方案效果很好。请参阅以下适用于 Swift 2 的代码,在 IOS 8 和 9 (XCode 7.1) 上成功测试。

func didTapMap(gestureRecognizer: UIGestureRecognizer) {
    tapPoint = gestureRecognizer.locationInView(mapView)
    NSLog("tapPoint = %f,%f",tapPoint.x, tapPoint.y)
    //convert screen CGPoint tapPoint to CLLocationCoordinate2D...
    let tapCoordinate = mapView.convertPoint(tapPoint, toCoordinateFromView: mapView)
    let tapMapPoint = MKMapPointForCoordinate(tapCoordinate)
    print("tap coordinates = \(tapCoordinate)")
    print("tap map point = \(tapMapPoint)")

    // Now we test to see if one of the overlay MKPolyline paths were tapped
    var nearestDistance = Double(MAXFLOAT)
    let minDistance = 2000      // in meters, adjust as needed
    var nearestPoly = MKPolyline()
    // arrayPolyline below is an array of MKPolyline overlaid on the mapView
    for poly in arrayPolyline {                
        // ... get the distance ...
        let distance = distanceOfPoint(tapMapPoint, poly: poly)
        print("distance = \(distance)")
        // ... and find the nearest one
        if (distance < nearestDistance) {
            nearestDistance = distance
            nearestPoly = poly
        }
    }
    if (nearestDistance <= minDistance) {
        NSLog("Touched poly: %@\n    distance: %f", nearestPoly, nearestDistance);
    }
}


func distanceOfPoint(pt: MKMapPoint, poly: MKPolyline) -> Double {
    var distance: Double = Double(MAXFLOAT)
    var linePoints: [MKMapPoint] = []
    var polyPoints = UnsafeMutablePointer<MKMapPoint>.alloc(poly.pointCount)
    for point in UnsafeBufferPointer(start: poly.points(), count: poly.pointCount) {
        linePoints.append(point)
        print("point: \(point.x),\(point.y)")
    }
    for n in 0...linePoints.count - 2 {
        let ptA = linePoints[n]
        let ptB = linePoints[n+1]
        let xDelta = ptB.x - ptA.x
        let yDelta = ptB.y - ptA.y
        if (xDelta == 0.0 && yDelta == 0.0) {
            // Points must not be equal
            continue
        }
        let u: Double = ((pt.x - ptA.x) * xDelta + (pt.y - ptA.y) * yDelta) / (xDelta * xDelta + yDelta * yDelta)
        var ptClosest = MKMapPoint()
        if (u < 0.0) {
            ptClosest = ptA
        } else if (u > 1.0) {
            ptClosest = ptB
        } else {
            ptClosest = MKMapPointMake(ptA.x + u * xDelta, ptA.y + u * yDelta);
        }
        distance = min(distance, MKMetersBetweenMapPoints(ptClosest, pt))
    }
    return distance
}
于 2015-12-23T16:04:06.873 回答
1

@Rashwan L:更新了他对 Swift 4.2 的回答

let map = MKMapView()
let mapTap = UITapGestureRecognizer(target: self, action: #selector(mapTapped(_:)))
 map.addGestureRecognizer(mapTap)

 @objc private func mapTapped(_ tap: UITapGestureRecognizer) {
    if tap.state == .recognized && tap.state == .recognized {
        // Get map coordinate from touch point
        let touchPt: CGPoint = tap.location(in: skyMap)
        let coord: CLLocationCoordinate2D = skyMap.convert(touchPt, toCoordinateFrom: skyMap)
        let maxMeters: Double = meters(fromPixel: 22, at: touchPt)
        var nearestDistance: Float = MAXFLOAT
        var nearestPoly: MKPolyline? = nil
        // for every overlay ...
        for overlay: MKOverlay in skyMap.overlays {
            // .. if MKPolyline ...
            if (overlay is MKPolyline) {
                // ... get the distance ...
                let distance: Float = Float(distanceOf(pt: MKMapPoint(coord), toPoly: overlay as! MKPolyline))
                // ... and find the nearest one
                if distance < nearestDistance {
                    nearestDistance = distance
                    nearestPoly = overlay as? MKPolyline
                }

            }
        }

        if Double(nearestDistance) <= maxMeters {
            print("Touched poly: \(nearestPoly) distance: \(nearestDistance)")

        }
    }
}

private func distanceOf(pt: MKMapPoint, toPoly poly: MKPolyline) -> Double {
    var distance: Double = Double(MAXFLOAT)
    for n in 0..<poly.pointCount - 1 {
        let ptA = poly.points()[n]
        let ptB = poly.points()[n + 1]
        let xDelta: Double = ptB.x - ptA.x
        let yDelta: Double = ptB.y - ptA.y
        if xDelta == 0.0 && yDelta == 0.0 {
            // Points must not be equal
            continue
        }
        let u: Double = ((pt.x - ptA.x) * xDelta + (pt.y - ptA.y) * yDelta) / (xDelta * xDelta + yDelta * yDelta)
        var ptClosest: MKMapPoint
        if u < 0.0 {
            ptClosest = ptA
        }
        else if u > 1.0 {
            ptClosest = ptB
        }
        else {
            ptClosest = MKMapPoint(x: ptA.x + u * xDelta, y: ptA.y + u * yDelta)
        }

        distance = min(distance, ptClosest.distance(to: pt))
    }
    return distance
}

private func meters(fromPixel px: Int, at pt: CGPoint) -> Double {
    let ptB = CGPoint(x: pt.x + CGFloat(px), y: pt.y)
    let coordA: CLLocationCoordinate2D = skyMap.convert(pt, toCoordinateFrom: skyMap)
    let coordB: CLLocationCoordinate2D = skyMap.convert(ptB, toCoordinateFrom: skyMap)
    return MKMapPoint(coordA).distance(to: MKMapPoint(coordB))
}
于 2018-09-25T18:55:09.083 回答
0

这是一个旧线程,但是我发现了一种可以帮助任何人的不同方式。在 Swift 4.2 中的多个路由覆盖上进行了测试。

 @IBAction func didTapGesture(_ sender: UITapGestureRecognizer) {
        let touchPoint = sender.location(in: mapView)
        let touchCoordinate = mapView.convert(touchPoint, toCoordinateFrom: mapView)
        let mapPoint = MKMapPoint(touchCoordinate)

        for overlay in mapView.overlays {
            if overlay is MKPolyline {
                if let polylineRenderer = mapView.renderer(for: overlay) as? MKPolylineRenderer {
                    let polylinePoint = polylineRenderer.point(for: mapPoint)

                    if polylineRenderer.path.contains(polylinePoint) {
                        print("polyline was tapped")
                    }
                }
            }
        }
 }
于 2019-01-08T16:04:49.360 回答
0

这段代码中真正的“cookie”是点 -> 线距离函数。我很高兴找到它,而且效果很好(swift 4,iOS 11)。感谢大家,尤其是@Jensemann。这是我对它的重构:

public extension MKPolyline {

    // Return the point on the polyline that is the closest to the given point
    // along with the distance between that closest point and the given point.
    //
    // Thanks to:
    // http://paulbourke.net/geometry/pointlineplane/
    // https://stackoverflow.com/questions/11713788/how-to-detect-taps-on-mkpolylines-overlays-like-maps-app

    public func closestPoint(to: MKMapPoint) -> (point: MKMapPoint, distance: CLLocationDistance) {

        var closestPoint = MKMapPoint()
        var distanceTo = CLLocationDistance.infinity

        let points = self.points()
        for i in 0 ..< pointCount - 1 {
            let endPointA = points[i]
            let endPointB = points[i + 1]

            let deltaX: Double = endPointB.x - endPointA.x
            let deltaY: Double = endPointB.y - endPointA.y
            if deltaX == 0.0 && deltaY == 0.0 { continue } // Points must not be equal

            let u: Double = ((to.x - endPointA.x) * deltaX + (to.y - endPointA.y) * deltaY) / (deltaX * deltaX + deltaY * deltaY) // The magic sauce. See the Paul Bourke link above.

            let closest: MKMapPoint
            if u < 0.0 { closest = endPointA }
            else if u > 1.0 { closest = endPointB }
            else { closest = MKMapPointMake(endPointA.x + u * deltaX, endPointA.y + u * deltaY) }

            let distance = MKMetersBetweenMapPoints(closest, to)
            if distance < distanceTo {
                closestPoint = closest
                distanceTo = distance
            }
        }

        return (closestPoint, distanceTo)
    }
}
于 2017-11-01T01:12:34.207 回答