Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting Tap on MKPolygonView in MKMapView on iOS7

Based on what I found on this SO question (Touch events on MKMapView's overlays), I have implemented a way to intercept tap gesture on MKPolygon.

It was working fine in our app that was built using Xcode 4.6.3 against iOS 6. However things stopped working when I tried it on iOS 7 devices.

Specifically

    CLLocationCoordinate2D coord = [neighborhoodMap_ convertPoint:point
                                             toCoordinateFromView:neighborhoodMap_];

    // We get view from MKMapView's viewForOverlay.
    MKPolygonView *polygonView = (MKPolygonView*) view;
    CGPoint polygonViewPoint = [polygonView pointForMapPoint:mapPoint];
    BOOL mapCoordinateIsInPolygon = CGPathContainsPoint(polygonView.path,
                                                        NULL,
                                                        polygonViewPoint,
                                                        NO);

For some reason the call to CGPathContainsPoint no longer returns YES even the given coordinates is within the MKPolygonView. Not sure if anyone has hit this problem, but I would appreciate any insights you may have.

Thanks!

like image 772
dfujiwara Avatar asked Oct 21 '22 02:10

dfujiwara


1 Answers

Since iOS 7 you need to use the MKOverlayRenderer:

BOOL tapInPolygon = NO;
MKOverlayRenderer * polygonRenderer = [mapView rendererForOverlay:polygonOverlay];
if ( [polygonRenderer isKindOfClass:[MKPolygonRenderer class]]) {

    //Convert the point
    CLLocationCoordinate2D  coordinate = [self.mapView convertPoint:tapPoint
                                               toCoordinateFromView:self.mapView];
    MKMapPoint mapPoint = MKMapPointForCoordinate(coordinate);
    CGPoint polygonViewPoint = [polygonRenderer pointForMapPoint:mapPoint];

    // with iOS 7 you need to invalidate the path, this is not required for iOS 8
    [polygonRenderer invalidatePath]; 

    tapInPolygon = CGPathContainsPoint(polygonRenderer.path, NULL, polygonViewPoint, NO);
}
like image 168
mt81 Avatar answered Oct 24 '22 03:10

mt81