Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clickable Polylines Google Maps API Android

I would like to click a spot on a Google maps v2 android map. If the clicked point intersects a point on a polyline path, then display the polyline. I do not see any documented clickable events for polylines in android. I tried to extend the current Polyline object (marked final)

What other options do I have?

like image 327
Wayne Avatar asked Jan 19 '13 16:01

Wayne


3 Answers

You can use library: https://github.com/googlemaps/android-maps-utils

And detect clicks to polyline using next method (in OnMapClickListener):

  PolyUtil.isLocationOnPath(point, polyline.getPoints(), isGeodesic, tolerance);
like image 137
eanix Avatar answered Oct 06 '22 09:10

eanix


With the recent update of the maps api, v8.4, introduces clickable Polyline

As mentioned in the doc:

Use the OnPolylineClickListener to listen to click events on a clickable polyline. To set this listener on the map, call googleMap.setOnPolylineClickListener(...). When a user clicks on a polyline, you will receive an onPolylineClick(Polyline) callback.

gradle-dependency:

compile 'com.google.android.gms:play-services-maps:8.4.0'

implement callback: GoogleMap.OnPolylineClickListener

initialize Polyline:

Polyline polyline = googleMap.addPolyline(options);
polyline.setClickable(true);
...

receive events

@Override
public void onPolylineClick(Polyline polyline) {
     ....
}

Happy coding :)

like image 39
Rakeeb Rajbhandari Avatar answered Oct 06 '22 11:10

Rakeeb Rajbhandari


Register an OnMapClickListener. Determine if a given click is on your line yourself. If it is, do whatever it was you wanted to do in this case.

like image 2
CommonsWare Avatar answered Oct 06 '22 10:10

CommonsWare