Search code examples
androidgoogle-mapsgoogle-polyline

How to get the specific data in click of polyline?


At the time of multiple data with the help of that I am plotting the polyline with lat and long value. Now when I'm plotting it, I'm to specific the line also because that the time of polyline click I should now the different because maybe the lat and long value will same but some when I will give specific value to that then I can know which lat and long is been click.

PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.addAll(coordinateList);
polylineOptions.width(10);
polylineOptions.color(Color.parseColor("#800080"));
Polyline line = mMap.addPolyline(polylineOptions);
line.setClickable(true);

coordinateList is the list of lat and long value. But I want to add one specific value to get the the value when it is been clicked.

@Override
public void onPolylineClick(Polyline polyline) {
  double latitude = polyline.getPoints().get(0).latitude;
  double longitude = polyline.getPoints().get(0).longitude;
}

In click I've come to know the lat and long

How I can set specific value and how I will get on click?


Solution

  • You can maintain a structure (a Map for example) to store the data associated to the Polylines. For example (storing a String for each Polyline):

    final Map<Polyline, String> polylines = new HashMap<>();
    
    PolylineOptions polylineOptions = new PolylineOptions();
    polylineOptions.addAll(coordinateList);
    polylineOptions.width(10);
    polylineOptions.color(Color.parseColor("#800080"));
    polylineOptions.clickable(true);
    
    polylines.put(mMap.addPolyline(polylineOptions), "Polyline data");
    
    mMap.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener() {
        @Override
        public void onPolylineClick(Polyline polyline) {
            Toast.makeText(MapsActivity.this, polylines.get(polyline), Toast.LENGTH_SHORT).show();
        }
    });