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