Question

I add a MKCircle overlay to my mapview and I want to know if a point (tap in screen) is inside the circle. This is my code :

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

viewForOverlay, pointForMapPoint & path are deprecated. Is this the problem?

Thank you.

Was it helpful?

Solution

This apporach should work too, using 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
    }

OTHER TIPS

If you know the centre of the circle and the radius then you could do something like...

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

Fogmeisters answer is incorrect because the unit system for expressing the point and radius are different. The point is expressed as degrees and a radius is typically expressed in meters. Aminovic09's answer is correct and is the advisable answer because it levers iOS' own method of distance calculation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top