buffer method

Polygon buffer(
  1. double distance, {
  2. DistanceUnit? unit,
  3. int steps = 40,
})

Returns a buffer Polygon of distance around the Point. The default unit is DistanceUnit.meters. The default steps is 40.

Example:

Point(Coordinate(1, 1)).buffer(13, {unit: Distance.feet}); // Polygon([Coordinate, ...])

Implementation

Polygon buffer(double distance, {DistanceUnit? unit, int steps = 40}) {
  // Calculate the buffer in the given unit (e.g., meters or feet)
  final double bufferRadiusMeters = convertDistance(
      distance, unit ?? DistanceUnits.meters, DistanceUnits.meters);

  // Create a circle as a buffer polygon
  final List<Coordinate> vertices = [];

  for (int i = 0; i < steps; i++) {
    final double angle = 2 * math.pi * i / steps;
    final double x =
        coordinate.longitude + bufferRadiusMeters * math.cos(angle);
    final double y =
        coordinate.latitude + bufferRadiusMeters * math.sin(angle);
    vertices.add(Coordinate(x, y));
  }

  // Close the circle
  vertices.add(vertices.first);

  return LineString(vertices).toPolygon();
}