2014-03-28 35 views
1

我将一个GMSMapView添加到了UIScrollView,并且我添加了表视图到该UIScrollView。没有我的任务是,如果在地图上的任何位置长按我会得到该地址,并将该地址添加到表视图,我也想在该位置添加一个标记。在iOS中将长按手势识别器添加到Google地图中

我编写了下面的代码,用于将长按手势识别器添加到地图中,但它不起作用。

- (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后调用的。
任何人都可以帮助我。

+0

这是因为滚动视图不响应你的手势。试试这个。 http://stackoverflow.com/questions/1685956/uiscrollview-touchesbegan/17759373#17759373 – Rajneesh071

回答

9

您可以使用默认的MapView长按事件

/** 
* 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; 
+0

谢谢你的回答。但它不工作。 – iSuresh

+0

map = [GMSMapView mapWithFrame:CGRectMake(0,0,self.view.frame.size.width,390)camera:camera]; map.delegate = self; –

+0

是的,这是工作,非常感谢你。 – iSuresh

相关问题