Google maps iOS sdk get tapped overlay coordinates

我只是一个虾纸丫 提交于 2019-12-01 06:22:07

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

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.

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

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!