Search code examples
iosmkmapviewmkoverlay

How to detect an overlay uniquely when more than one overlay added on the MKMapView


In my situation I have to placed more than one polygon as the overlay on the MKMapView, those polygon are actually creating from the JSON response, the response actually containing the polygon_id along with coordinate for the to form that polygon. I just want to some how merge that polygon_id with the overlay, so that whenever user clicked on that overlay it will return the polygon_id.

This is my code:

-(void)darwPolyGon:(NSMutableArray *)polyArr polyGonAreaId:(NSString
*)areaID isAssign:(BOOL)isAssign{


    CLLocationCoordinate2D *coordinates =
(CLLocationCoordinate2D*)malloc(sizeof(CLLocationCoordinate2D) *
[polyArr count]);
    for (int i=0; i<polyArr.count; i++) {
        ModelPolygon *poly=[polyArr objectAtIndex:i];
        coordinates[i] = CLLocationCoordinate2DMake(poly.lat,poly.lon);

    }
    MKPolygon *polygon = [MKPolygon polygonWithCoordinates:coordinates
count:polyArr.count];
    strTapAreaId=areaID;
    polygon.title=strTapAreaId;

    [MyMapView addOverlay:polygon];


}
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id
<MKOverlay>)overlay
{
    if([overlay isKindOfClass:[MKPolygon class]]){
        MKPolygonView *viewPoly = [[MKPolygonView alloc]
initWithOverlay:overlay];

        viewPoly.lineWidth=3;
        if (isAssignUser) {
            viewPoly.strokeColor=[UIColor colorWithRed:255/255.0f
green:30/255.0f blue:0/255.0f alpha:1.0f];

        }else
        viewPoly.strokeColor=[UIColor colorWithRed:132/255.0f
green:0/255.0f blue:255/255.0f alpha:1.0f];
        viewPoly.tag=[strTapAreaId integerValue];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleMapTap:)];
        tap.cancelsTouchesInView = NO;
        tap.numberOfTapsRequired = 1;
        [MyMapView addGestureRecognizer:tap];
        return viewPoly ;
    }
    return nil;
}

-(void)handleMapTap:(UIGestureRecognizer*)tap{
    CGPoint tapPoint = [tap locationInView:MyMapView];

    CLLocationCoordinate2D tapCoord = [MyMapView convertPoint:tapPoint
toCoordinateFromView:MyMapView];
    MKMapPoint mapPoint = MKMapPointForCoordinate(tapCoord);
    CGPoint mapPointAsCGP = CGPointMake(mapPoint.x, mapPoint.y);

    for (id<MKOverlay> overlay in MyMapView.overlays) {
        if([overlay isKindOfClass:[MKPolygon class]]){
            MKPolygon *polygon = (MKPolygon*) overlay;

            CGMutablePathRef mpr = CGPathCreateMutable();

            MKMapPoint *polygonPoints = polygon.points;

            for (int p=0; p < polygon.pointCount; p++){
                MKMapPoint mp = polygonPoints[p];
                if (p == 0)
                    CGPathMoveToPoint(mpr, NULL, mp.x, mp.y);
                else
                    CGPathAddLineToPoint(mpr, NULL, mp.x, mp.y);
            }

            if(CGPathContainsPoint(mpr , NULL, mapPointAsCGP, FALSE)){
                MKPolygonView *viewPoly = [[MKPolygonView alloc]
initWithOverlay:overlay];
                NSLog(@"tag=%d",viewPoly.tag);
            }

            CGPathRelease(mpr);
        }
    }
}

Solution

  • The only methods I could think off are:

    1- You can try sneaking in the overlay's ID in its "title" or "subtitle" attribute

    2- You can create a class that holds and MKOverlay and its ID and whenever it is selected you can loop over your array of overlays(the ones inside the class) and you add an if statement in case the selected overlay is equal to the overlay in the loop, return its ID