用iOS7检测MKPolygon中的一个点(CGPathContainsPoint) [英] Detecting a point in a MKPolygon broke with iOS7 (CGPathContainsPoint)

查看:331
本文介绍了用iOS7检测MKPolygon中的一个点(CGPathContainsPoint)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SO问题中,我之前提到过今年,我得到了这段代码:

In a SO question I asked earlier this year, I got this chunk of code:

MKPolygonView *polygonView = (MKPolygonView *)[self.mapView viewForOverlay:polygon];
MKMapPoint mapPoint = MKMapPointForCoordinate(tapCoord);
CGPoint polygonViewPoint = [polygonView pointForMapPoint:mapPoint];

if (CGPathContainsPoint(polygonView.path, NULL, polygonViewPoint, FALSE)) {
    // do stuff
}

直到iOS7才能正常运行。它现在总是返回false并且不会检测带路径的点。

This works great up until iOS7. It now always returns false and will not detect a point with the path.

我正在尝试查找任何说明方法更改的文档,但找不到任何。

I'm trying to find any documentation stating that the method change, but can't find any.

任何有关它破裂的想法?还是一个新的解决方案?

Any ideas why it broke? Or a new solution?

推荐答案

出于某种原因(可能是一个错误),路径属性在当前版本的iOS 7中返回 NULL

For some reason (possibly a bug), the path property returns NULL in the current release of iOS 7.

解决方法是构建自己的 CGPathRef 来自多边形的 points

使用此方法,您不需要引用 MKPolygonView MKPolygonRenderer

A workaround is to construct your own CGPathRef from the points of the polygon.
With this method, you don't need a reference to the MKPolygonView or the MKPolygonRenderer.

例如:

CGMutablePathRef mpr = CGPathCreateMutable();

MKMapPoint *polygonPoints = myPolygon.points;
//myPolygon is the MKPolygon

for (int p=0; p < myPolygon.pointCount; p++)
{
    MKMapPoint mp = polygonPoints[p];
    if (p == 0)
        CGPathMoveToPoint(mpr, NULL, mp.x, mp.y);
    else
        CGPathAddLineToPoint(mpr, NULL, mp.x, mp.y);
}

CGPoint mapPointAsCGP = CGPointMake(mapPoint.x, mapPoint.y);
//mapPoint above is the MKMapPoint of the coordinate we are testing.
//Putting it in a CGPoint because that's what CGPathContainsPoint wants.

BOOL pointIsInPolygon = CGPathContainsPoint(mpr, NULL, mapPointAsCGP, FALSE);

CGPathRelease(mpr);

这也适用于iOS 6。

但是,你可能想要仅当叠加视图的路径属性返回 NULL 时才执行此手动构造。

This should work on iOS 6 as well.
However, you may want to do this manual construction only if the overlay view's path property returns NULL.

这篇关于用iOS7检测MKPolygon中的一个点(CGPathContainsPoint)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆