2014-03-28 36 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

相關問題