PolyUtil

object PolyUtil

A utility class containing geometric calculations for polygons and polylines. This class provides methods for determining if a point is inside a polygon, on the edge of a polygon, simplifying polylines, and encoding/decoding polylines.

The methods in this class are designed to be used with the Google Maps Android API, and they operate on {@link LatLng} objects. The calculations can be performed using either geodesic (great circle) or rhumb (loxodromic) paths.

Functions

Link copied to clipboard
fun containsLocation(point: LatLng, polygon: List<LatLng>, geodesic: Boolean): Boolean

Computes whether the given point lies inside the specified polygon. The polygon is always considered closed, regardless of whether the last point equals the first or not. Inside is defined as not containing the South Pole -- the South Pole is always outside. The polygon is formed of great circle segments if geodesic is true, and of rhumb (loxodromic) segments otherwise.

fun containsLocation(latitude: Double, longitude: Double, polygon: List<LatLng>, geodesic: Boolean): Boolean

Overload of {@link #containsLocation(LatLng, List, boolean)} that takes latitude and longitude as separate arguments.

Link copied to clipboard
fun decode(encodedPath: String): List<LatLng>

Decodes an encoded path string into a sequence of LatLngs.

Link copied to clipboard
fun distanceToLine(p: LatLng, start: LatLng, end: LatLng): Double

Computes the distance on the sphere between the point p and the line segment start to end.

Link copied to clipboard
fun encode(path: List<LatLng>): String

Encodes a sequence of LatLngs into an encoded path string.

Link copied to clipboard

Returns true if the provided list of points is a closed polygon (i.e., the first and last points are the same), and false if it is not

Link copied to clipboard
fun isLocationOnEdge(point: LatLng, polygon: List<LatLng>, geodesic: Boolean, tolerance: Double = DEFAULT_TOLERANCE): Boolean

Computes whether the given point lies on or near the edge of a polygon, within a specified tolerance in meters. The polygon edge is composed of great circle segments if geodesic is true, and of Rhumb segments otherwise. The polygon edge is implicitly closed -- the closing segment between the first point and the last point is included.

Link copied to clipboard
fun isLocationOnPath(point: LatLng, polyline: List<LatLng>, geodesic: Boolean, tolerance: Double = DEFAULT_TOLERANCE): Boolean

Computes whether the given point lies on or near a polyline, within a specified tolerance in meters. The polyline is composed of great circle segments if geodesic is true, and of Rhumb segments otherwise. The polyline is not closed -- the closing segment between the first point and the last point is not included.

Link copied to clipboard
fun locationIndexOnEdgeOrPath(point: LatLng, poly: List<LatLng>, closed: Boolean, geodesic: Boolean, toleranceEarth: Double): Int

Computes whether (and where) a given point lies on or near a polyline, within a specified tolerance. If closed, the closing segment between the last and first points of the polyline is not considered.

Link copied to clipboard
fun locationIndexOnPath(point: LatLng, poly: List<LatLng>, geodesic: Boolean, tolerance: Double = DEFAULT_TOLERANCE): Int

Computes whether (and where) a given point lies on or near a polyline, within a specified tolerance. The polyline is not closed -- the closing segment between the first point and the last point is not included.

Link copied to clipboard
fun simplify(poly: MutableList<LatLng>, tolerance: Double): List<LatLng>

Simplifies the given poly (polyline or polygon) using the Douglas-Peucker decimation algorithm. Increasing the tolerance will result in fewer points in the simplified polyline or polygon.