Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if a point is inside of MKCircle in iPhone sdk ios 7

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.

like image 275
aminovic09 Avatar asked Mar 30 '14 11:03

aminovic09


1 Answers

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
    }
like image 146
Hari Avatar answered Oct 19 '22 01:10

Hari