在- (void)viewDidLoad添加如下方法:
[self long_pressed_screen];
其实现代码如下:并在.h文件中添加此方法。这里就不用多说了。
- (void)long_pressed_screen
{
UILongPressGestureRecognizer *lpress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
lpress.minimumPressDuration = 0.5;//按0.5秒响应longPress方法
lpress.allowableMovement = 10.0;
//map 为mkmapview的实例
[map addGestureRecognizer:lpress];
[lpress release];
}
- (void)longPress:(UIGestureRecognizer*)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
//坐标转换
CGPoint touchPoint = [gestureRecognizer locationInView:map];
//得到经纬度,指触摸区域
CLLocationCoordinate2D touchMapCoordinate = [map convertPoint:touchPoint toCoordinateFromView:map];
pointAnnotation = [[MKPointAnnotation alloc] init];
pointAnnotation.coordinate = touchMapCoordinate;
pointAnnotation.title = @"点击查看详细信息";
[map addAnnotation:pointAnnotation];
[pointAnnotation release];
}
}
通过上述代码基本上就实现了,自己可以再进行扩展修改。