将长按手势识别器添加到iOS中的Google Map

时间:2014-03-28 04:46:30

标签: ios objective-c ios7 ios6

我在UIScrollView中添加了GMSMapView,并且还将表视图添加到了UIScrollView。没有我的任务是如果长按在地图上的任何位置我将获得该地址并将该地址添加到表视图,并且我还想在该位置添加标记。

我编写了以下代码,用于将长按手势识别器添加到地图中,但它无效。

- (void)viewDidLoad
  {
    [super viewDidLoad];
    self->map.delegate = self;
    CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
    UIScrollView  *scroll=[[UIScrollView alloc] initWithFrame:fullScreenRect];
    [self.view addSubview:scroll];
    scroll.contentSize=CGSizeMake(320,1000);
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:21.0000 longitude:78.0000 zoom:4.5];
    map = [GMSMapView mapWithFrame:CGRectMake(0,0, self.view.frame.size.width,390) camera:camera];
    [scroll addSubview:map];

    UITableView *tab = [[UITableView alloc]initWithFrame:CGRectMake(0, 410, self.view.bounds.size.width, 300) style:UITableViewStylePlain];
    [scroll addSubview:tab];

    tab.delegate = self;
    tab.dataSource = self;

   UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(mapLongPress:)];
   longPressGesture.minimumPressDuration = 1.5;
   [map addGestureRecognizer:longPressGesture];
}

-(void)mapLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
  {
    if(gestureRecognizer.state == UIGestureRecognizerStateBegan)
    {
      NSLog(@"%f",coordinate.latitude);
      NSLog(@"%f",coordinate.longitude);
    }
  }

这里的主要问题是" mapLongPress"我长按MapView后没有调用方法  任何人都可以帮助我。

1 个答案:

答案 0 :(得分:9)

您可以使用默认地图VIew长按事件

  /**
 * Called after a long-press gesture at a particular coordinate.
 *
 * @param mapView The map view that was pressed.
 * @param coordinate The location that was pressed.
 */
     - (void)mapView:(GMSMapView *)mapView
    didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate;