Google maps iOS sdk get tapped overlay coordinates
Asked Answered
D

6

7

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!

Demona answered 25/5, 2014 at 16:55 Comment(2)
Did you find any solution? It's crazy the developers didn't create a didTapAtCoordinate for overlaysUnshaped
take a look at this answer https://mcmap.net/q/1480482/-get-lat-and-long-from-tapped-overlay-in-google-mapsTuggle
A
6

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);
}
Agoraphobia answered 10/9, 2014 at 19:26 Comment(1)
This is correct but this method will not fire on an overlay tap. OP likely has this setup but is searching for a way to fire on an overlay tap as well to get geocoords of overlay tap location.Atul
B
3

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.

Brouhaha answered 25/5, 2014 at 19:11 Comment(1)
yes, i can get marker position, but i'm interested in getting the position of GMSPolygon and GMSPolyline where they're tapped. the shape already exists when user taps on it. substantially, i want to zoom at that position at the tap event. (yes, i wasn't so explanatory..) thanks!Demona
U
1

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.

Unshaped answered 18/6, 2015 at 16:5 Comment(0)
G
1

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);
}
Griceldagrid answered 26/10, 2015 at 12:6 Comment(0)
A
0

So you can't get the didTapAtCoordinateMethod to fire for an overlay tap. However I did find a slightly dirty workaround.

Using an overlay to draw polylines, we need a way to recognize where a polyline was tapped. So when drawing polylines we can build them like this.

    //draw line
    GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
    polyline.strokeColor = [UIColor purpleColor];
    polyline.tappable = TRUE;
    polyline.map = self.googleMapView;
    polyline.title = routestring;

Where routestring is a built string from

routestring = [NSString stringWithFormat:@"%@/%@/%@",lat,lng,[annnotationobject objectForKey:@"linkId"]];

And lat and lng are string values of our coordinates. The last part is an ID for the polyline.

The routestring is storing the coordinates and an ID separated by '/' so that we can use component path of string to look them up later. This is assigned to the polylines title.

Now when the overlay is tapped:

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

NSString *path = overlay.title;

//Finding componentpaths of string
NSArray *pathparts = [path pathComponents];
NSString *lat = [pathparts objectAtIndex:0];
NSString *lng = [pathparts objectAtIndex:1];
NSString *linkID = [pathparts objectAtIndex:2];

//Here we are building a marker to place near the users tap location on the polyline.
GMSMarker *marker = [GMSMarker markerWithPosition:CLLocationCoordinate2DMake([lat doubleValue],[lng doubleValue])];
marker.title = overlay.title;
marker.snippet = @"ROUTE DATA";
marker.map = self.googleMapView;

//This will popup a marker window
[self.googleMapView setSelectedMarker:marker];

}

We can use component paths of the string we built(separated by "/")to get the latitude and longitude coordinates from the polyline. Then assign them a marker to popup information on the overlay item.

Atul answered 12/1, 2015 at 16:58 Comment(0)
A
0

You can subclass UITapGestureRecognizer and override its touchesEnded to retrieve the touch point, than retrieve the coordinate with GMSMapView's coordinateForPoint.

Subclass UITapGestureRecognizer and add it to your mapView:

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

TouchDownGestureRecognizer.h:

#import <UIKit/UIKit.h>    

@interface TouchGestureRecognizer : UITapGestureRecognizer
@property  GMSMapView       *mapView;
@end

TouchDownGestureRecognizer.m:

#import "TouchGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>

@implementation TouchGestureRecognizer

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
  [super touchesEnded:touches withEvent:event];

  UITouch *touch = touches.allObjects[0];
  CGPoint point = [touch locationInView:self.mapView];
  CLLocationCoordinate2D coord = [self.mapView.projection coordinateForPoint:point];
  NSLog(@"%f %f", coord.latitude, coord.longitude);
}

@end
Agoraphobia answered 1/7, 2015 at 21:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.