Objective c 在mkmapview上检测到非';注释是什么?
如何检测正在点击的地图视图是否不是obj-c中的注释?请尝试UITapGestureRecognitizer:Objective c 在mkmapview上检测到非';注释是什么?,objective-c,mkmapview,Objective C,Mkmapview,如何检测正在点击的地图视图是否不是obj-c中的注释?请尝试UITapGestureRecognitizer: UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mapDidTap:)]; [mapView addGestureRecognizer: tapGesture]; 及 以下代码检测地图中的点击并在该位置添加地图标记: -
UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(mapDidTap:)];
[mapView addGestureRecognizer: tapGesture];
及
以下代码检测地图中的点击并在该位置添加地图标记:
- (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];
}
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) { }
因此,将使用轻触手势处理所有管脚选择。
您可以分别检测map和PIN点按。我使用的正是这段代码,mapDidTap:从未被调用。奇怪的是,您只添加了通常的点按检测代码-似乎MapView没有“内置”的概念!
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) { }