我试图弄清楚如何根据用户触摸的位置在地图上添加注释。
我尝试MKMapView
对 the 进行子类化并寻找touchesBegan
触发,但事实证明,MKMapView
它不使用标准的触摸方法。
我也尝试过将 a 子类化UIView
,将 a 添加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]);
}
}
但是地图有一些疯狂的行为,比如它不会滚动,除非双击它不会放大,但你可以缩小。并且仅当我将地图作为视图返回时才有效。如果我没有命中测试方法,地图工作正常,但显然没有得到任何数据。
我是不是要弄错坐标了?请告诉我有更好的方法。我知道如何添加注释就好了,我只是找不到任何在用户触摸地图的位置和时间添加注释的示例。