4

I am drawing Polyline in my Google Maps. When the user tapped the line. The marker will show and the infowindow. But not where the user EXACTLY tapped it.

How do I know the coordinates (lat,long) where the user tapped in the line?

I think the function below is being overriden by the `didTap overlay' method

 func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
}

Thanks!

Jayson Tamayo
  • 2,741
  • 3
  • 49
  • 76

2 Answers2

2

First i want to say you have to create poly line tap able :

GMSPolyline *poly = [GMSPolyline polylineWithPath:path];
poly.strokeColor = [UIColor purpleColor];

poly.tappable = TRUE;  // this is important

poly.map = self.googleMapView;
poly.title = routestring;

Then use :

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

NSString *path = overlay.title;
NSArray *pathparts = [path pathComponents];
NSString *lat = [pathparts objectAtIndex:0]; //get lat
NSString *lng = [pathparts objectAtIndex:1]; // get lng
NSString *lnkID = [pathparts objectAtIndex:2];

// Create a marker and plot on Map

GMSMarker *mk = [GMSMarker arkerWithPosition:CLLocationCoordinate2DMake([lat doubleValue],[lng doubleValue])];
mk.title = overlay.title;
mk.snippet = @"ROUTE DATA";
mk.map = self.googleMapView;

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

If using swift use this method:

func mapView(mapView: GMSMapView!, didTapOverlay overlay: GMSOverlay!) {

}
Jay Prakash
  • 787
  • 6
  • 22
0

Here is code for converted into swift brother .

func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) {
    var path = overlay.title
    var pathparts = path.pathComponents()
    var lat = pathparts[0]
    var lng = pathparts[1]
    var lnkID = pathparts[2]
    var mk = GMSMarker.arker(withPosition: CLLocationCoordinate2DMake(lat.doubleValue, lng.doubleValue))
    mk.title = overlay.title
    mk.snippet = "ROUTE DATA"
    mk.map = self.googleMapView
    self.googleMapView.selectedMarker = mk
}
Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49
  • Hi on swift 3 iam getting error on "path.pathcomponents()". can you suggest me to solve this? – Anand3777 Nov 15 '16 at 06:50
  • 'pathComponents' is unavailable: Use pathComponents on URL instead. @Himanshu Moradiya and how to get the latitude and longitude? – Anand3777 Nov 15 '16 at 07:03
  • that answer was partially copied from here https://stackoverflow.com/a/27907060/9024807 – Nikaaner Dec 19 '19 at 22:34