检测一个点是否在iPhone sdk ios 7中的MKCircle内部

时间:2014-03-30 11:00:38

标签: ios iphone mapkit geometry

我在MKCircle添加了mapview叠加层,我想知道一个点(点击屏幕)是否在圈内。这是我的代码:

- (BOOL)pointInsideOverlay:(CLLocationCoordinate2D )tapPoint overlay:(id<MKOverlay>)overlay  {

   BOOL isInside = FALSE;
   MKPolygonView *polygonView = (MKPolygonView *)[self.mapView viewForOverlay:overlay];
   MKMapPoint mapPoint = MKMapPointForCoordinate(tapPoint);
   CGPoint polygonViewPoint = [polygonView pointForMapPoint:mapPoint];
   BOOL mapCoordinateIsInPolygon = CGPathContainsPoint(polygonView.path, NULL, polygonViewPoint, NO);

   if (mapCoordinateIsInPolygon) {
       isInside = TRUE;
   }
   return isInside;
}

viewForOverlaypointForMapPoint&amp;不推荐使用path。这是问题吗?

谢谢。

3 个答案:

答案 0 :(得分:11)

这种方法也可以使用MKCircleRenderer

    MKCircleRenderer *circleRenderer = (MKCircleRenderer *)[mapview rendererForOverlay:circleOverlay];
    [circleRenderer invalidatePath];

    MKMapPoint mapPoint = MKMapPointForCoordinate(tapPoint);
    CGPoint circlePoint = [circleRenderer pointForMapPoint:mapPoint];
    BOOL mapCoordinateIsInCircle = CGPathContainsPoint(circleRenderer.path, NULL, circlePoint, NO);

    if ( mapCoordinateIsInCircle )

    {
        //do something
    }

答案 1 :(得分:0)

如果您知道圆的中心和半径,那么您可以做类似的事情......

-(BOOL)isPoint:(CGPoint)point insideCircleCentre:(CGPoint)centre radius:(CGFloat)radius
{
    CGFloat dx = point.x - centre.x;
    CGFloat dy = point.y - centre.y;

    CGFloat distance = sqrtf(dx * dx + dy * dy);

    return distance <= radius;
}

答案 2 :(得分:0)

Fogmeisters答案不正确,因为表达点和半径的单位系统不同。该点表示为度数,半径通常以米为单位表示。 Aminovic09的答案是正确的,是可取的答案,因为它改进了iOS自己的距离计算方法。