Jayson Tamayo
Jayson Tamayo

Reputation: 2811

Show marker where user tapped in polyline

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!

Upvotes: 4

Views: 1668

Answers (2)

Himanshu Moradiya
Himanshu Moradiya

Reputation: 4815

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
}

Upvotes: 0

Jay Prakash
Jay Prakash

Reputation: 805

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!) {

}

Upvotes: 2

Related Questions