我希望能够判断tap是否在MKPolygon中。
我有一个MKPolygon:
CLLocationCoordinate2D points[4];
points[0] = CLLocationCoordinate2DMake(41.000512, -109.050116);
points[1] = CLLocationCoordinate2DMake(41.002371, -102.052066);
points[2] = CLLocationCoordinate2DMake(36.993076, -102.041981);
points[3] = CLLocationCoordinate2DMake(36.99892, -109.045267);
MKPolygon* poly = [MKPolygon polygonWithCoordinates:points count:4];
[self.mapView addOverlay:poly];
//create UIGestureRecognizer to detect a tap
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(foundTap:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1;
[self.mapView addGestureRecognizer:tapRecognizer];
它只是科罗拉多州的基本轮廓。
我点击了lat / long转换设置:
-(IBAction)foundTap:(UITapGestureRecognizer *)recognizer
{
CGPoint point = [recognizer locationInView:self.mapView];
CLLocationCoordinate2D tapPoint = [self.mapView convertPoint:point toCoordinateFromView:self.view];
}
但如果我的分接点在MKPolygon内,我不确定如何技术。似乎没有办法做这个检查,所以我猜我需要将MKPolygon转换为CGRect并使用CGRectContainsPoint。
MKPolygon有一个.points属性,但我似乎无法让它们退出。
有什么建议吗?
编辑:
以下两种解决方案均适用于iOS 6或更低版本,但在iOS 7中有效。在iOS 7中,polygon.path
属性始终返回NULL
。安娜女士非常友好地提供a solution in another SO question here。它涉及从多边形点创建自己的路径以传递到CGPathContainsPoint()
。
我的多边形图像:
答案 0 :(得分:12)
我创建了这个MKPolygon类别,以防有人想要使用它。似乎运作良好。您必须考虑内部多边形(即多边形中的孔):
@interface MKPolygon (PointInPolygon)
-(BOOL) pointInPolygon:(CLLocationCoordinate2D) point mapView: (MKMapView*) mapView;
@end
@implementation MKPolygon (PointInPolygon)
-(BOOL) pointInPolygon:(CLLocationCoordinate2D) point mapView: (MKMapView*) mapView {
MKMapPoint mapPoint = MKMapPointForCoordinate(point);
MKPolygonView * polygonView = (MKPolygonView*)[mapView viewForOverlay:self];
CGPoint polygonViewPoint = [polygonView pointForMapPoint:mapPoint];
return CGPathContainsPoint(polygonView.path, NULL, polygonViewPoint, NO) &&
![self pointInInteriorPolygons:point mapView:mapView];
}
-(BOOL) pointInInteriorPolygons:(CLLocationCoordinate2D) point mapView: (MKMapView*) mapView {
return [self pointInInteriorPolygonIndex:0 point:point mapView:mapView];
}
-(BOOL) pointInInteriorPolygonIndex:(int) index point:(CLLocationCoordinate2D) point mapView: (MKMapView*) mapView {
if(index >= [self.interiorPolygons count])
return NO;
return [[self.interiorPolygons objectAtIndex:index] pointInPolygon:point mapView:mapView] || [self pointInInteriorPolygonIndex:(index+1) point:point mapView:mapView];
}
@end
答案 1 :(得分:9)
您的foundTap
方法:
-(IBAction)foundTap:(UITapGestureRecognizer *)recognizer
{
CGPoint point = [recognizer locationInView:self.mapView];
CLLocationCoordinate2D tapPoint = [self.mapView convertPoint:point toCoordinateFromView:self.view];
[self pointInsideOverlay:tapPoint];
if (isInside)
{
....
}
}
这是一种从前一个方法调用以检查该点是否在叠加层内的方法:
-(void)pointInsideOverlay:(CLLocationCoordinate2D )tapPoint
{
isInside = FALSE;
MKPolygonView *polygonView = (MKPolygonView *)[mapView viewForOverlay:polygonOverlay];
MKMapPoint mapPoint = MKMapPointForCoordinate(tapPoint);
CGPoint polygonViewPoint = [polygonView pointForMapPoint:mapPoint];
BOOL mapCoordinateIsInPolygon = CGPathContainsPoint(polygonView.path, NULL, polygonViewPoint, NO);
if ( !mapCoordinateIsInPolygon )
//we are finding points that are inside the overlay
{
isInside = TRUE;
}
}
答案 2 :(得分:5)
我从字符串中的xml文件中获取MKPolygon数据点。我将数据字符串解析为数组,并使用http://alienryderflex.com/polygon/
中的方法它对我有用....
-(BOOL)isPoint:(CLLocationCoordinate2D)findLocation inPloygon:(NSArray*)polygon{
NSMutableArray *tempPolygon=[NSMutableArray arrayWithArray:polygon];
int i, j=(int)tempPolygon.count-1 ;
bool oddNodes=NO;
double x=findLocation.latitude;
double y=findLocation.longitude;
for (i=0; i<tempPolygon.count; i++) {
NSString*coordString=[tempPolygon objectAtIndex:i];
NSArray*pointsOfCoordString=[coordString componentsSeparatedByString:@","];
CLLocationCoordinate2D point=CLLocationCoordinate2DMake([[pointsOfCoordString objectAtIndex:1] doubleValue], [[pointsOfCoordString objectAtIndex:0] doubleValue]);
NSString*nextCoordString=[tempPolygon objectAtIndex:j];
NSArray*nextPointsOfCoordString=[nextCoordString componentsSeparatedByString:@","];
CLLocationCoordinate2D nextPoint=CLLocationCoordinate2DMake([[nextPointsOfCoordString objectAtIndex:1] doubleValue], [[nextPointsOfCoordString objectAtIndex:0] doubleValue]);
if ((point.longitude<y && nextPoint.longitude>=y)
|| (nextPoint.longitude<y && point.longitude>=y)) {
if (point.latitude+(y-point.longitude)/(nextPoint.longitude-point.longitude)*(nextPoint.latitude-point.latitude)<x) {
oddNodes=!oddNodes; }}
j=i; }
return oddNodes;
}
我的多边形(NSArray)对象是字符串,例如@"-89.860021,44.944266,0"
答案 3 :(得分:5)
这里是swift 3更新版本,感谢@Steve Stomp
extension MKPolygon {
func contains(coordinate: CLLocationCoordinate2D) -> Bool {
let polygonRenderer = MKPolygonRenderer(polygon: self)
let currentMapPoint: MKMapPoint = MKMapPointForCoordinate(coordinate)
let polygonViewPoint: CGPoint = polygonRenderer.point(for: currentMapPoint)
return polygonRenderer.path.contains(polygonViewPoint)
}
}
答案 4 :(得分:4)
确定一个点是否在任意多边形中是非常重要的,并且Apple不提供它作为MKPolygon的一部分也就不足为奇了。您可以访问这些点,这样您就可以迭代边缘。
要确定点p是否在多边形s内,请将每个边视为s中的有向线段。如果来自p的光线在任何固定方向上(通常与X轴或Y轴平行)与线段相交,则将光线的叉积的Z分量的符号与该有向线段截取。如果Z分量> 0,将1添加到计数器。如果是&lt;实现这一点的技巧是避免边缘几乎与光线平行时出现问题,或者当光线通过一个顶点时(它必须只计数一次,而不是每个边缘计数一次)。
当您对s中的所有边缘执行此操作时,您将计算光线与多边形轮廓相交的次数,如果边缘从左向右移动,则添加,如果它是从从右到左,你减去了。如果结果总和为零,则您位于多边形之外。否则你就在里面。
有许多可能的优化。其中之一是在更完整的测试之前进行快速边界框测试。另一种方法是在所有边上都有一个带有边界的数据结构,以便轻易地丢弃不与光线相交的边。
编辑:A X B的Z分量(A与B的叉积)由下式给出:
a.x * b.y - a.y * b.x
因为你所关心的只是标志,你可以检查
a.x * b.y > a.y * b.x
答案 5 :(得分:3)
这在Swift中对我有用:
extension MKPolygon {
func isCoordinateInsidePolyon(coordinate: CLLocationCoordinate2D) -> Bool {
var inside = false
let polygonRenderer = MKPolygonRenderer(polygon: self)
let currentMapPoint: MKMapPoint = MKMapPointForCoordinate(coordinate)
let polygonViewPoint: CGPoint = polygonRenderer.pointForMapPoint(currentMapPoint)
if CGPathContainsPoint(polygonRenderer.path, nil, polygonViewPoint, true) {
inside = true
}
return inside
}
}