如何在MKMapView

时间:2015-09-01 10:48:57

标签: ios mkmapview mkoverlay

在我的情况下,我必须在MKMapView上放置多个多边形作为叠加层,这些多边形实际上是从JSON响应创建的,实际上包含polygon_id的响应以及用于形成该多边形的坐标。我只想知道如何将polygon_id与叠加层合并,这样每当用户点击该叠加层时,它将返回polygon_id。

这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

我能想到的唯一方法是:

1-您可以尝试在其&#34;标题&#34;中隐藏叠加层ID。或&#34;副标题&#34;属性

2-您可以创建一个包含MKOverlay及其ID的类,无论何时选择它,您都可以遍历覆盖数组(类中的那些),并在所选叠加层相等的情况下添加if语句到循环中的叠加层,返回其ID