Clickable polygon and polyline. Check if the is in the proximity of the polygon or on the polyline in SDK 4.x

Summary

This article explains how to detect user taps inside a polygon or on/near a polyline in HERE SDK 4.x (Android) by using MapView gesture listeners and custom geometric checks.

Applies To

• HERE SDK 4.x (Android)

• MapPolygon, MapPolyline

Use Case

Interactive map scenarios such as asset tracking, geofencing, or route interaction where polygons or polylines must respond to user taps.

Prerequisites

• A MapView rendered on screen

• A MapPolygon or MapPolyline added to the map

• Access to geometry vertices from the map items

Solution

A) Detect if a Tap Is Inside a Polygon

1. Register a tap listener on the MapView and convert screen coordinates to GeoCoordinates.

Code: Register Tap Listener

private void setTapGestureHandler(MapView mapView, MapPolygon polygon) {
 mapView.getGestures().setTapListener(new TapListener() {
 @Override
 public void onTap(@NonNull Point2D touchPoint) {
 GeoCoordinates geoCoordinates = mapView.viewToGeoCoordinates(touchPoint);
 if (geoCoordinates != null && isClickInsidePolygon(geoCoordinates, polygon)) {
 print("tapped", "Clicked inside the polygon");
 }
 }
 });
}



2. Implement a point-in-polygon check using a ray-casting algorithm.

Code: Point-in-Polygon Check

private boolean isClickInsidePolygon(GeoCoordinates point, MapPolygon polygon) {
 List points = polygon.getGeometry().vertices;
 boolean isInside = false;
 int p = points.size();
 for (int i = 0, j = p - 1; i < p; j = i++) {
 double xi = points.get(i).latitude;
 double yi = points.get(i).longitude;
 double xj = points.get(j).latitude;
 double yj = points.get(j).longitude;
 boolean intersect = ((yi > point.longitude) != (yj > point.longitude))
 && (point.latitude < (xj - xi) \* (point.longitude - yi) / (yj - yi) + xi);
 if (intersect) { isInside = !isInside; }
 }
 return isInside;
}



B) Detect if a Tap Is On or Near a Polyline

1. Register a tap listener for the polyline.

Code: Register Tap Listener for Polyline

private void setTapGestureHandlerPolyLine(MapView mapView, MapPolyline polyLine) {
 mapView.getGestures().setTapListener(new TapListener() {
 @Override
 public void onTap(@NonNull Point2D touchPoint) {
 GeoCoordinates geoCoordinates = mapView.viewToGeoCoordinates(touchPoint);
 if (geoCoordinates != null && isClickOnPolyline(geoCoordinates, polyLine)) {
 print("tapped", "Clicked on the polyline");
 }
 }
 });
}



2. Implement a distance-based proximity check.

Code: Polyline Proximity Check

private boolean isClickOnPolyline(GeoCoordinates clickedCoordinates, MapPolyline polyline) {
 double tolerance = 0.001;
 for (GeoCoordinates lineGeo : polyline.getGeometry().vertices) {
 if (clickedCoordinates.distanceTo(lineGeo) <= tolerance) {
 return true;
 }
 }
 return false;
}



Troubleshooting

• Increase tolerance if polyline taps are not detected.

• Ensure polyline geometry has sufficient vertices.

• Verify polygon geometry validity.

References

Ray-casting algorithm: Ray-casting algorithm (Rosetta Code)
HERE SDK Android examples (used for testing in original article):
HERE SDK Examples – MapItems (Android)