3

I added a GMSMapView to a UIScrollView and also i added Table View to that UIScrollView. No my task is if Long Press on any Location on the Map i will get that address and add that address to Table View and also i want to add a marker at that Location.

I write the Below code for adding long press gesture recognizer to the map but it is not working.

- (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);
    }
  }

The Main Problem here is "mapLongPress" method is not called after i long press on the MapView.
Can any one Help me please.

Suresh Peddisetti
  • 3,782
  • 3
  • 23
  • 26
  • this is because scrollview is not responding to your gesture. try this one. http://stackoverflow.com/questions/1685956/uiscrollview-touchesbegan/17759373#17759373 – Rajneesh071 Mar 28 '14 at 05:05

1 Answers1

9

You can use default MapVIew LongPress event

  /**
 * 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;
Sunny Shah
  • 12,990
  • 9
  • 50
  • 86
  • Thanks for u r answer.But it is not Working. – Suresh Peddisetti Mar 28 '14 at 04:52
  • map = [GMSMapView mapWithFrame:CGRectMake(0,0, self.view.frame.size.width,390) camera:camera]; map.delegate = self; – Sunny Shah Mar 28 '14 at 04:56
  • @SunnyShah check out my quest: https://stackoverflow.com/questions/46329911/google-maps-delegate-method-bug please same usage but my `didLongPress...` delegate never gets called while all other GMS del's do. Not sure what's happening... – Will Von Ullrich Sep 20 '17 at 19:22