Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google maps iOS sdk get tapped overlay coordinates

i'm working with Google maps iOS sdk. i want to get the coordinates of the touched point when user taps an overlay.

there is this delegate method:

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate

but it's not called when you tap an overlay or a marker.

can i call it programmatically (but coordinate parameter is missing-that's what i want..)? or get location from this:

- (void) mapView: (GMSMapView *) mapView  didTapOverlay: (GMSOverlay *) overlay

any suggestion's precious!

thanks!

like image 368
ateciufit15 Avatar asked May 25 '14 16:05

ateciufit15


4 Answers

If you just want to get the position of the marker, there is a position property

CLLocationCoordinate2D coord = marker.position;

When this delegate method gets called

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate

its for screen taps that have no markers or overlays from my expierience.

GMSOverlay is a bit different because its a super class to a GMSMarker. You just need to subclass GMSOverlay for your custom overlays and add a position property. When you create the overlay, say in didTapAtCoordinate, you can assign the position (GPS coord) in there.

like image 30
m33k Avatar answered Nov 16 '22 15:11

m33k


UPDATE 6/2/15

Just staple a UITapGestureRecognizer onto the map and then extract the coordinate from the touch point. Your didTapAtCoordinate and didTapAtOverlay will continue to fire as before.

  UITapGestureRecognizer *touchTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTouchTap:)];
  [self.view addGestureRecognizer:touchTap];

-(void)tapTouchTap:(UITapGestureRecognizer*)touchGesture
{
  CGPoint point = [touchGesture locationInView:self.view];
  CLLocationCoordinate2D coord = [self.mapView.projection coordinateForPoint:point];
  NSLog(@"%f %f", coord.latitude, coord.longitude);
}

ORIGINAL POST

You are likely missing two snippets of code. Adopt the GMSMapViewDelegate in your header file:

@interface Map : UIViewController <GMSMapViewDelegate>

You also need to set the delegate in your viewDidLoad:

self.mapView.delegate = self;

Now this should fire for you:

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
    NSLog(@"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);
}
like image 98
etayluz Avatar answered Nov 16 '22 15:11

etayluz


You could set the circle not tappable (default behaviour) and catch all the clicks on the map with the didTapAtCoordinate delegate.

Then when this event is triggered you could loop over all your circles to check if the user tapped inside one of the circles or outside.

like image 29
alexislg Avatar answered Nov 16 '22 13:11

alexislg


self.mapview.settings.consumesGesturesInView = NO;

Add this line in viewdidload or after allocating.

And then implement this delegate method.

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
    NSLog(@"%g",coordinate);
}


- (void)mapView:(GMSMapView *)mapView didTapOverlay:(GMSOverlay *)overlay
{

    GMSCircle *circle=(GMSCircle *)overlay;
    CLLocationCoordinate2D touchCoOrdinate= circle.position;
    NSLog(@"%g",touchCoOrdinate);
}
like image 1
bharathi kumar Avatar answered Nov 16 '22 14:11

bharathi kumar