在该deactivateAllRadars
方法中,您可以使用hitTest:withEvent:
来判断一个是否MKAnnotationView
已被点击。
我如何在 MapView 上点击然后将其传递给默认手势识别器中显示了一个示例?(这是第二个代码示例)。
如果已点击注释,这将使您避免删除圆圈。
如果尚未点击注释,则可以MKCircle
通过获取触摸的坐标来检查是否被点击(请参阅如何在 MKMapView 上捕获点击手势)并查看从触摸到圆心的距离是否更大比它的半径。
请注意,deactivateAllRadars
应该将其更改为,deactivateAllRadars:(UITapGestureRecognizer *)tgr
因为它需要来自相关手势识别器的信息。还要确保在您 alloc+init 的方法选择器的末尾添加一个冒号tap
。
例如:
-(void)deactivateAllRadars:(UITapGestureRecognizer *)tgr
{
CGPoint p = [tgr locationInView:mapView];
UIView *v = [mapView hitTest:p withEvent:nil];
id<MKAnnotation> ann = nil;
if ([v isKindOfClass:[MKAnnotationView class]])
{
//annotation view was tapped, select it...
ann = ((MKAnnotationView *)v).annotation;
[mapView selectAnnotation:ann animated:YES];
}
else
{
//annotation view was not tapped, deselect if some ann is selected...
if (mapView.selectedAnnotations.count != 0)
{
ann = [mapView.selectedAnnotations objectAtIndex:0];
[mapView deselectAnnotation:ann animated:YES];
}
//remove circle overlay if it was not tapped...
if (mapView.overlays.count > 0)
{
CGPoint touchPoint = [tgr locationInView:mapView];
CLLocationCoordinate2D touchMapCoordinate
= [mapView convertPoint:touchPoint toCoordinateFromView:mapView];
CLLocation *touchLocation = [[CLLocation alloc]
initWithLatitude:touchMapCoordinate.latitude
longitude:touchMapCoordinate.longitude];
CLLocation *circleLocation = [[CLLocation alloc]
initWithLatitude:circleCenterLatitude
longitude:circleCenterLongitude];
CLLocationDistance distFromCircleCenter
= [touchLocation distanceFromLocation:circleLocation];
if (distFromCircleCenter > circleRadius)
{
//tap was outside the circle, call removeOverlay...
}
}
}
}