ios - CGPoint 是在 MKPolygonView 中吗?

标签 ios mkoverlay

我正在尝试找出一种方法来检测哪个 MKOverlayView(实际上是 MKPolygonView)被点击,然后更改其颜色。

我用这段代码运行它:

- (void)mapTapped:(UITapGestureRecognizer *)recognizer {

    MKMapView *mapView = (MKMapView *)recognizer.view;
    MKPolygonView *tappedOverlay = nil;
    for (id<MKOverlay> overlay in mapView.overlays)
    {
        MKPolygonView *view = (MKPolygonView *)[mapView viewForOverlay:overlay];

        if (view){
            // Get view frame rect in the mapView's coordinate system
            CGRect viewFrameInMapView = [view.superview convertRect:view.frame toView:mapView];
            // Get touch point in the mapView's coordinate system
            CGPoint point = [recognizer locationInView:mapView];
            // Check if the touch is within the view bounds
            if (CGRectContainsPoint(viewFrameInMapView, point))
            {

                tappedOverlay = view;
                break;
            }
        }
    }

    if([[tappedOverlay fillColor] isEqual:[[UIColor cyanColor] colorWithAlphaComponent:0.2]]){
        [listOverlays addObject:tappedOverlay];
        tappedOverlay.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.2];
    }
    else{
        [listOverlays removeObject:tappedOverlay];
        tappedOverlay.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
    }
    //tappedOverlay.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];

}

这有效,但有时,根据我点击的位置,点击的 MKPolygonView 会出错。我想是因为 CGRectContainsPoint 无法正确计算面积,因为它不是矩形,而是多边形。

还有哪些其他方法可以做到这一点?我尝试了 CGPathContainsPoint ,但结果更糟。

最佳答案

感谢 @Ana Karenina,他指出了正确的方法,这就是您必须转换手势的方法,以便方法 CGPathContainsPoint' 正常工作。

- (void)mapTapped:(UITapGestureRecognizer *)recognizer{

MKMapView *mapView = (MKMapView *)recognizer.view;

MKPolygonView *tappedOverlay = nil;
int i = 0;
for (id<MKOverlay> overlay in mapView.overlays)
{
    MKPolygonView *view = (MKPolygonView *)[mapView viewForOverlay:overlay];

    if (view){
        CGPoint touchPoint = [recognizer locationInView:mapView];
        CLLocationCoordinate2D touchMapCoordinate =
        [mapView convertPoint:touchPoint toCoordinateFromView:mapView];

        MKMapPoint mapPoint = MKMapPointForCoordinate(touchMapCoordinate);

        CGPoint polygonViewPoint = [view pointForMapPoint:mapPoint];
        if(CGPathContainsPoint(view.path, NULL, polygonViewPoint, NO)){
            tappedOverlay = view;
            tappedOverlay.tag = i;
            break;
        }
    }
    i++;
}

if([[tappedOverlay fillColor] isEqual:[[UIColor cyanColor] colorWithAlphaComponent:0.2]]){
    [listOverlays addObject:tappedOverlay];
    tappedOverlay.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.2];
}
else{
    [listOverlays removeObject:tappedOverlay];
    tappedOverlay.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
}
//tappedOverlay.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];

}

关于ios - CGPoint 是在 MKPolygonView 中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14524718/

相关文章:

ios - 核心数据|与 2 个逆元的关系

ios - MKMapView - 将 map 滚动限制为方形覆盖

ios - 用图案图像在 MKMapView 上画线

iphone - 如何在 MKMapView 上画一条线来显示用户走过的路线?

ios - 在 Map View iOS 上重新添加叠加层

ios - 为什么来自 parse.com 的推送只能到达一台设备?

iphone - 如何从 NSMutableData 对象的末尾删除字节

iphone - 如何在没有 Storyboard 的情况下使用 Core Data?

ios - 如何强制调用rendererForOverlay

ios - iPhone : How to detect the end of slider drag?