如何检测是否正在点击地图视图,这不是obj-c中的注释?
答案 0 :(得分:0)
以下代码检测地图中的点按,并在该位置添加地图标记:
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *fingerTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleMapFingerTap:)];
fingerTap.numberOfTapsRequired = 1;
fingerTap.numberOfTouchesRequired = 1;
[self.mapView addGestureRecognizer:fingerTap];
}
- (void)handleMapFingerTap:(UIGestureRecognizer *)gestureRecognizer {
NSLog(@"handleMapFingerTap gesture: %@", gestureRecognizer);
if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
return;
}
CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];
CLLocationCoordinate2D touchMapCoordinate =
[self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];
MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = touchMapCoordinate;
pa.title = @"Hello";
[self.mapView addAnnotation:pa];
}
答案 1 :(得分:0)
Swift 4,
我在我们的案例中解决了这个问题, 通过向注释视图和地图视图添加点按手势。
添加点按手势以映射
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.hideFilterView))
self.mapView.addGestureRecognizer(tapGesture)
//override "viewFor annotation:" something like this
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: "Pin")
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "Pin"
annotationView?.canShowCallout = false
} else {
annotationView?.annotation = annotation
}
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.didTapPin(onMap:)))
annotationView?.addGestureRecognizer(tapGesture)
return annotationView
}
//then don't override
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { }
因此,将使用轻击手势处理所有引脚选择。 你可以分别检测地图和别针点击。
答案 2 :(得分:-1)
尝试 UITapGestureRecognizer :
UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(mapDidTap:)];
[mapView addGestureRecognizer: tapGesture];
和
-(void)mapDidTap:(UITapGestureRecognizer *)gestureRecognizer {
//do something when map is tapped
}