7

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?

Wayne
  • 73
  • 1
  • 3

5 Answers5

15

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);
eanix
  • 326
  • 3
  • 6
14

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 :)

Rakeeb Rajbhandari
  • 5,043
  • 6
  • 43
  • 74
2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    I ended up using dispatchTouchEvent. I needed to use map 'swipes' to highlight polylines instead of a single click. – Wayne Jan 31 '13 at 13:30
  • @Wayne: I'm impressed that `dispatchTouchEvent()` worked. I thought perhaps that would screw up map panning/zooming and the like. But, yes, `OnMapClickListener` is for simple taps only. – CommonsWare Jan 31 '13 at 13:50
  • 1
    @Wayne, could you elaborate a little bit about how you did this? – Nighto May 12 '14 at 19:10
  • the library you can use to detect whether the click was on the polyline was mentioned by @eanix on his answer. – Caio Iglesias Jan 14 '15 at 17:41
2

I had a similar issue where I could not process click events on polylines. I was using Xamarin for Android which is C# but the functionality is largely the same as the Android Java Libraries in this case.

In the end, I ended up doing what seemed to be the only option.

This involved processing all of the midpoints of my polylines(of which there were around 1300). On every OnMapClick, I took the LatLng of the click event and performed a distance formula between it and the midpoint of all polylines in the static List<PolylineOptions>. I then attached a map marker to the closest polyline.

From a tap on a polyline, it pops up a marker in about a quarter of a second.

I imagine the implemented marker click events from the Google Maps API work in a similar way.

Here is the for loop that handles finding the closest point to a click.

int i = 0;//create an indexer for the loop
double shortestDist = 100;//set an initial very large dist just to be safe
int myIndex = 0;//set variable that will store the running index of the closest point
foreach (PolylineOptions po in myPolylines) {
    var thisDist = Distance (point, midPoint (po.Points [0].Latitude, po.Points [0].Longitude, po.Points [1].Latitude, po.Points [1].Longitude));//calculate distance between point and midpoint of polyline
    if (thisDist < shortestDist) {
        shortestDist = thisDist;//remember current shortest distance
        myIndex = i;//set closest polyline index to current loop iteration
    }
    i++;
} 

I know it isn't the prettiest code but it gets the job done. I didn't see a real answer to this anywhere on the internet so here it is. It could probably be made more efficient by calculating the midpoints beforehand and storing them in an equally sized list and then not having to call the midpoint formula for each polyline on every map click but it works really fast already.

EDIT

I do my testing on a galaxy s3 by the way, so I think it's not too inefficient.

carterh062
  • 190
  • 10
0

If you are using com.google.android.gms:play-services-maps:8.4.0 then it includes polylines click listener

googleMap.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener() 
{
         @Override
         public void onPolylineClick(Polyline polyline)
         {
                  //do your work selected polyline
         }
});

PolylineOptions line = new PolylineOptions();
Polyline polyline = googleMap.addPolyline(line);
polyline.setClickable(true);
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
Sachin Tanpure
  • 1,068
  • 11
  • 12