当在 MKMapView 上添加多个叠加层时,如何唯一地检测叠加层
How to detect an overlay uniquely when more than one overlay added on the MKMapView
在我的情况下,我必须在 MKMapView 上放置多个多边形作为叠加层,这些多边形实际上是从 JSON 响应创建的,响应实际上包含 polygon_id 以及坐标为了形成那个多边形。我只想知道如何将 polygon_id 与叠加层合并,这样每当用户单击该叠加层时,它就会 return 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- 您可以尝试在叠加层的 "title" 或 "subtitle" 属性中隐藏其 ID
2- 您可以创建一个 class 来保存 MKOverlay 及其 ID,无论何时选择它,您都可以遍历覆盖数组(class 中的覆盖数组)并添加一个 if 语句,以防所选覆盖层等于循环中的覆盖层,return 其 ID
在我的情况下,我必须在 MKMapView 上放置多个多边形作为叠加层,这些多边形实际上是从 JSON 响应创建的,响应实际上包含 polygon_id 以及坐标为了形成那个多边形。我只想知道如何将 polygon_id 与叠加层合并,这样每当用户单击该叠加层时,它就会 return 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- 您可以尝试在叠加层的 "title" 或 "subtitle" 属性中隐藏其 ID
2- 您可以创建一个 class 来保存 MKOverlay 及其 ID,无论何时选择它,您都可以遍历覆盖数组(class 中的覆盖数组)并添加一个 if 语句,以防所选覆盖层等于循环中的覆盖层,return 其 ID