我正在尝试找出如何在用户触摸的位置上放置一个注释标记到地图上。
我曾尝试过子类化 MKMapView
并寻找触摸事件的触发点,但事实证明,MKMapView
不使用标准的touches方法。
我还尝试了子类化 UIView
,将MKMapView
添加为子视图,然后侦听 HitTest 和 touchesBegan
。这在某种程度上能够工作。
如果我将地图设置为 UIView
的全屏大小,然后像这样做:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
return map;
}
这很有效,我的touchesBegan
可以使用
获取该点。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches){
CGPoint pt = [touch locationInView:map];
CLLocationCoordinate2D coord= [map convertPoint:pt toCoordinateFromView:map];
NSLog([NSString stringWithFormat:@"x=%f y=%f - lat=%f long = %f",pt.x,pt.y,coord.latitude,coord.longitude]);
}
}
但是这个地图有些奇怪的行为,比如它不能滚动,除非双击缩放,但可以缩小。只有当我将地图返回为视图时才能正常工作。如果没有hit test方法,地图就能正常工作,但显然得不到任何数据。
我获取坐标的方式有误吗?请告诉我是否有更好的方法。我知道如何添加注释,但我找不到任何在用户触摸地图时添加注释的示例。