このdeactivateAllRadars
メソッドでは、がタップされたhitTest:withEvent:
かどうかを判断するために使用できます。MKAnnotationView
この例は、MapView でタップをキャッチしてデフォルトのジェスチャ認識エンジンに渡すにはどうすればよいですか? に示されています。(これは 2 番目のコード サンプルです)。
これにより、注釈がタップされた場合に円を削除することを回避できます。
注釈がタップされていない場合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...
}
}
}
}