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!
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.
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);
}
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With