Home » Android » android – Listen for click events on a LineLayer

android – Listen for click events on a LineLayer

Posted by: admin May 14, 2020 Leave a comment


Various lines are added to the map representing routes using the following code:

private LineLayer makeLineLayer(List<GeoPoint> routePoints, String title) {

    String sourceTitle = "line-layer-" + lineCount;

    List<Position> points = new ArrayList<>(routePoints.size());
    List<Feature> routes = new ArrayList<>(routePoints.size());
    for (GeoPoint point : routePoints) {
            points.add(Position.fromCoordinates(point.getLongitude(), point.getLatitude()));
    LineString route = LineString.fromCoordinates(points);

    Feature routeFeature = Feature.fromGeometry(route);
    routeFeature.addStringProperty("custom-line", "0");

    GeoJsonSource linesSource = new GeoJsonSource(

    LineLayer lineLayer = new LineLayer(title, sourceTitle);
        //Sets properties...

    return lineLayer;

LineLayer lineLayer = makeLineLayer(getRoutePoints());

I’d like to be able to determine when one of these lines is clicked. Currently, MapBox calls OnMapClick and passes in a LatLng object. I can then query for rendered features with the custom-line property using the following:

PointF pixel = mapboxMap.getProjection().toScreenLocation(point);
List<Feature> selectedKeys = mapboxMap.queryRenderedFeatures(pixel, Filter.has("custom-line"));

If selectedKeys then contains any returned Feature objects, I can query for their coordinates with .getGeometry(). Comparing those values with those from the LatLng object that was passed in, a rough estimate of which line was clicked can be determined. However, this is very inaccurate and troublesome when the line items are closely grouped.

How would one listen for click events on these line items?

How to&Answers:

According to the MapBox’s Android SDK Documentation.

There is no method to click on the LineLayer neither in the parent class i.e. Layer.

If you can convert your LineLayer to Polyline you can have an inbuilt method for clicking in Android.

Refer this link to get clarity on it.

This is the method details of it.
void onPolylineClick(@NonNull Polyline polyline)


Called when the user clicks on a polyline.



polyline – The polyline the user clicked on.